Adding/resizing a background image to centralWidget in Qt - c++

I'm new to Qt. I searched for my question on multiple sites, but I couldn't find an answer. How can I add an image to centralWidget?
I tried:
MainWindow w;
w.centralWidget()->setStyleSheet("image: url(image)");
it worked fine but the image isn't stretched for the entire window/widget. How can I resize the image through setStyleSheet?
I want to display an image in the background and not the gray color, when the application opens. I tried changing the color. That worked, but it doesn't look good with buttons and labels.

If you want it to stretch for the entire widget use background-image, but I'm guessing what you probably want is an image that expands in one direction, either vertically or horizontally, in that case use background-position to fix it to one of the borders of your widget:
image: url(:/path/to/image);
background-position: bottom left;

Related

Color the Image setting on QProgressBar

Actually I set an image on a QProgressBar and want to change the color of image according to selected part of QProgressBar.
I tried covering my image with QProgressBar color but that fills the image, but as my image is not a rectangle it color the whole QProgressBar and not just the image.
Is it possible to change color of image only using qt (with C++)
You are looking for Qt Style Sheet Documentation. You can customize your own QProgressBar, and add your own setProgress() function which should update the value property. You could modify the image background, the border, margins... depending of the value of the current value.
See QSS examples for more detail: Example Style Sheet

Qt - QGraphicsScene is overlapping images

So I was following this tutorial, https://deadbird.fr/?p=800, to obtain blur behind windows in qt.. Made some changes like request a capture() when a resize event is scheduled and added a timer to update every 200ms the image with QTimer so when for example a window is minimized the background image is updated. So far so good..
But then I tested the timer and I noticed this: https://www.youtube.com/watch?v=WixIShA48jg
As you can see in the sixth second of the video the QGraphicsScene is overlapping the images, even with scene->clear();
The other problem I have is the color. I changed the background color of the dialog to blue but only the borders are blue. I also tried to put the QGraphicsView in a QFrame with transparency and blue color but no luck. Why is that?
You can find the source code in the tutorial site. My changes are visible in the video. The video is also in 720p.

Interactively editing an existing rectangle on a QPixmap?

I'm trying to creat a Dicom GUI Toolkit where the user selects some dicom images and the image of first dicom image from the selected ones will be shown. Then the user clicks on the image and the image pops out with bigger image window. In this shown bigger image, the image will consist of a red colored rectangle that contains necessary regions of the Dicom image while the unnecessary region is outside the rectangle. The user should then have the option to change the rectangle by mouse.
Until now, I have been able to show the big dicom image with the rectangle in it using QLabel which is by the following code snippets.
void MainWindow::showBigImage()
{
QPixmap bigimage;
bigimage.load(imageName.c_str());
QPainter painter(&bigimage);
painter.setPen(Qt::red);
QRectF rect(xmin, ymin, xmax, ymax);
painter.drawRect(rect);
QSize bigsize = ui->bigImageLabel->size();
ui->bigImageLabel->setPixmap(bigimage.scaled(bigsize, Qt::IgnoreAspectRatio, Qt::FastTransformation));
ui->bigImageLabel->show();
}
and the big image on the app looks like the following:
Can you please suggest me how I should now make the rectangle editable by the user where the user can set the existing red rectangle as per his or her wish?
I also tried similar thing using QGraphicsView and QGraphicsScene with the following code:
void MainWindow::showBigImage()
{
QGraphicsScene* scene = new QGraphicsScene;
scene->addPixmap(bigimage);
ui->bigImageView->setScene(scene);
ui->bigImageView->show();
}
And this code gives me the following look:
As you can see, I could not fit the image to the boundaries of QGraphicsView, could you suggest me how to do it? Could you also suggest me how to add the red rectangle(that I showed in the example using QLabel) on the QGraphicsView without adding the rectangle on the QPixmap?
In order to get the red selection rectangle, Qt provides the class QRubberBand. The docs state:
The QRubberBand class provides a rectangle or line that can indicate a selection or a boundary.
By subclassing the image object and implementing the mouse handling functions, to create the rubber band on mousePressEvent, update its position on mouseMoveEvent and grab its final rect on mouseReleaseEvent, the QRubberBand will simplify the problem.
If you want the QRubberBand to show all the time, just create it when you display the enlarged image and don't hide it on releasing the mouse button.
As for displaying the image in the QGraphicsView, the code you displayed doesn't set the geometry of the QGraphicsScene and QGraphicsView, so you're seeing a border. If you don't want that, you should set them accordingly. Also note that QGraphicsView has a function fitInView, which you could use, after having retrieved an area from the QRubberBand, in order to zoom into the selected area.

Specific QPushButton style

How can I customize the look of a QPushButton or QToolButton to look something like elementaryos's webpage "buttons"?
All I really want is the characteristic image position and the text on it's side, maybe if i'm lucky i can also get a border like that, but i don't really need the little description below the title :)
Can i do it only with StyleSheets, or do i have to subclass QPushButton/QAbstractButton/Something like that? I already searched everywhere but didn't found that level of customization without things like painting something in a fixed place, which is exactly what i don't want.
EDIT:
I really would like a solution that would get me a customizable button, not a fixed image one, something in the tracks of
MainWindowButton(QString(title), /*opt*/QString(description), QImage(icon));
There are a number of approaches that may work.
You might first consider trying to compose a solution with a normal QPushButton with a QVBoxLayout on it. You could add three QLabels; one for the title text, one for the caption text and one for the image. Some CSS could probably be used to render the background image of the button for up and down and more CSS to style the text in the two labels and position the image on the third but you would then find that the labels don't shift down when the button is clicked.
I think the best solution involves direct painting. You could do this by sub classing a QWidget and overriding the paintEvent(). Render everything for the up state and shift everything over and down a bit for the down state.
You could achieve this without sub classing by rendering the up and down states to a QImage and styling a QPushbutton with them using CSS.
There are a number of combinations of these approaches too.
You can set the background as white with the icon in the right corner with some creative use of the border-image stylesheet property where the bottom border is as tall as the icon and the top, left and right borders are 0 pixels wide. You'll need to make a custom image that basically looks like the icon with a couple pixels width on the left and top that are white
http://doc.qt.digia.com/qq/qq20-qss.html#theboxmodel
The text you may have to do overriding paintEvent.

Customize QScrollBar intersection using stylesheet Qt

I’ve already customize my horizontal and vertical scrollbars using a stylesheet no problem.
But there still an annoying tiny area which remains blank :
The intersection of an horizontal and vertical bar. A small rectangle.
How could I change its color ? (Using stylesheets )
Thank you !
Qt 4.7.1 on Mac OSX Snow Leopard
Ps: Even on the Qt stylesheet example it’s still white.
I realise this is an old question, but I found a better solution.
QAbstractScrollArea::corner {
background: somecolor;
}
Or, to hide it, use:
background: transparent;
By default, the scroll area corner will be painted with the Window palette. Unfortunately, you cannot change the Window palette using only stylesheets. However, what you can do is create a dummy widget and set it to be displayed in the corner area with QAbstractScrollArea::setCornerWidget(QWidget *widget), and then use the stylesheet to change the color of that widget.