The background image is not drawn QWidget - c++

The application has 2 windows. One thing is MainWindow, for which a png image is set as the background through QtStyleSheet. In the class itself, the attributes Qt::WA_TranslucentBackground and Qt::FramelessWindowHint. The second is QWidget, which serves to configure the application. The problem is that in Qt Creator QWidget background image displayed normally, and when you start the application, it is empty. Images used as a background are in the resource file.
(Sorry there is not enough rating to attach images)
Here is an example:
So the background looks like in Qt Creator
And so when you start the application
What could be the problem, and how to find its cause?

Related

QPushButton in UI form file: image doesn't show when adding resource, but shows when setting stylesheet

I have a Qt widgets project which defines dialog boxes with Qt Designer UI files. In one dialog box I include a QPushButton and I added an image by going to "QAbstractButton"->"Icon" and setting the file to "normal on". Afterwards I've noticed that, even though that image is rendered in Qt Designer, it does not show up when I compile and run the project.
However, the same exact resource is rendered in the button if I set it using Qt's stylesheets.
qproperty-icon: url(:/Resources/icn_MyIcon.png)
I'd much prefer to not use stylesheets for simple things like adding an image to a button. Does anyone have any idea why setting resources with Qt Designer fails but setting stylesheets works?

Qt widget blurred semitransparent background on Linux

Working with widgets, c++ and Linux
need something kind of:
this
but no borders and custom title.
Search for a few days, but nothing.
For now, there is a widget with setWindowFlags(Qt::FramelessWindowHint); and a peace of qss for semitransparent background. How can I blur it? Is is possible at all?
I do not think this can be done with Qt. Blurring can be done using https://doc.qt.io/qt-5/qgraphicsblureffect.html but it is only limited to the widgets painted by Qt. Which the underlying background is not, even if you manage to make your widgets transparent or semitransparent. Painting the background is always the business of the operating system (or window manager) and not the business of your application Qt.
You can certainly try to do some extreme hacking like grabbing the active screen before your window is displayed (see How to capture the current screen image using Qt?) then getting certain rectangle content of the image, which corresponds to the background of your window, then paint it blurred to the background of your application and then update it everytime you move or resize your window... But anyway, even if you manage this, this background will be static and not dynamic.
I recommend that you abandon the idea of blurry background and leave this function to the window manager and the operating system.

Qt: Bad window icon quality

I'm using Qt 5.10.1. I created a window icon resource and applied to Qt application like following:
a.setWindowIcon(QIcon(":/icons/resources/logo_icon.png"));
The size of logo_icon.png is 256*256.
The result is this:
The icon is a little blurrly, which is not what I expected. For comparison, following is the window icon of GIMP in which I designed the icon:
I tried various sizes of the image from 16*16 to 256*256, there was no luck. Changing image format to ico from png also didn't work.
What should I do to render a clearer window icon?
Maybe in the ui file of your window there is a iconSize value that makes your icon scale not very well when rendered by the window manager. This blurry effect occurred to me sometimes because the size was 15 px instead of 16 (or another power of 2).
Try to open the .ui file of your main window in Design mode and look under the QMainWindow group in the widget properties panel to set the iconSize to something like 16.
You can also set the icon file from there, using the windowIcon property under the QWidget group.

White screen observed while launching the QGraphicsView application

Our application uses Qt's Graphics View framework to load the html pages. QGraphicsWebView loads local html page which is black background. But always observed the white screen while launching the application. I have tried setting black background for both QGraphicsView and QGraphicsScene. Nothing worked for me.
Here's the sample code for your reference.
MainWindow which inherited from QMainWindow class
mGraphicsScene = new QGraphicsScene(this);
mGraphicsView = new QGraphicsView(mGraphicsScene);
mGraphicsView->setViewport(new QGLWidget(this));
mGraphicsWebView = new QGraphicsWebView;
mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/"));
mGraphicsScene->addItem(mGraphicsWebView);
setCentralWidget(mGraphicsView);
Is there any way to avoid white screen of the application?
Best Regards,
Pratap
Try next. Why did you see white? Because item already added, but page not loaded, so you see white(blank) item without page. Set to your scene some black pixmap, connect loadFinished signal to special slot, where you add item to your scene. In this case scene will be black, but when page will be loaded, your slot will add this on scnen and you will see only page.
mGraphicsScene = new QGraphicsScene(this);
mGraphicsScene->addItem(new QGraphicsPixmapItem(QPixmap("G:/2/qt.jpg")));
mGraphicsView = new QGraphicsView(mGraphicsScene);
mGraphicsView->setViewport(new QGLWidget(this));
mGraphicsWebView = new QGraphicsWebView;
mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/"));
connect(mGraphicsWebView,SIGNAL(loadFinished(bool)),this,SLOT(slotLoaded()));
//mGraphicsScene->addItem(mGraphicsWebView);
mGraphicsView->resize(1000,700);
mGraphicsView->show();
Slot:
void MainWindow::slotLoaded()
{
mGraphicsScene->addItem(mGraphicsWebView);
}
For example black pixmap which was created by code:
QPixmap black(1000,700);
black.fill(Qt::black);
mGraphicsScene = new QGraphicsScene(this);
mGraphicsScene->addItem(new QGraphicsPixmapItem(black));
When application start:
As you can see, all is black, when page was loaded:
As you can see, it is normal page. It is not very beautiful because I use fast settings and resize window and so on, but you set graphicsview as central widget, do it will be more beautiful.
Thank you very much for the response.
I have tried your solution and also observed white screen while launching the application on Windows
I found the culprit is mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/")); This is blocking all other widgets on the scene. So I have added a singleShot timer and kept this statement under that.
//QTimer::singleShot(100, this, SLOT(loadUrl())); Then it works fine.
Please let me know if you have any better idea.

how to attach image to gui created by using Qt creator

How to put an image on the GUI created by Qt?
The south west part of the screen is empty so I want a picture to be put there on a click of a button but I am unable to use QPixmap and setPixmap. Please help me with this !
Use QLabel in Qt Creator
Then Go to Properties of QLabel
Go to Pixmap
Select the file
Its done.
You can create a QWidget or a QFrame at the place you want to add an image. You can then use the style sheet to set a background-image on that picture. You might want to add your image to your ressources (.qrc).
Using QtDesigner is a good idea for this kind of task.
EDIT :
Here is an easy way to do that without bothering with QPixMap.
QWidget *frame = new QWidget(this);
frame->setGeometry(x, y, width, height);
frame->setStyleSheet("background-image: url(:/path/to/image.png)");
The : specifies that you want to use the path in your Qt resources. The first two lines are not needed if you define this QWidget using QtDesigner.
Also, don't forget to import your resources.qrc (however you called it) file (including your resources) and add this to your .pro :
RESOURCES = resources.qrc
You should use a label (QLabel). You can add a label to your form and edit its pixmap property in Qt Designer (you will be able to choose one of images you've added to project resources). Also you can set an image on QLabel programmatically using setPixmap().