Cannot set stylesheet within method body, but can inside constructor
-
Hi,
This problem extends to other functionality of my application, but I think the root problem is generally the same. I am attempting to set the Pixmap for a label using ui->labelName->setPixmap(pixmap); however, nothing displays. So I thought I'd try setting the background of the label to red. That didn't work either, but when I copied the same line into the constructor for the window I the background was red. I'm not sure whats going on here. I went into debug mode and checked if function gets called and furthermore if my Pixmap gets defined. All of these lines execute according to the debugger, but nothing visibly changes. The Qlabel remains blank unless I move the code to the constructor. However, I can't simply set the pixmap in the constructor as the pixmap changes throughout the life cycle of my application so I cannot set it once and be done.
-
Hi,
Are you sure the pixmap you are setting is valid ?
-
I'm fairly certain it's valid. In debug mode the width, height, pixel depth all that get set. The creation returns a bool right? I'll add my code here while I investigate that.
There are some objects from a library in there, but I check that the QImage is created with the right properties. I also save the QImage to disk and it was what I expected, so I'm pretty sure the QImage is fine.
-
Did you try to let QPixmap operate a format conversion ?
-
There is no change. I set the format to MonoOnly and nothing changed. If I load the Pixmap from data using any format it will break it as the incoming format is RAW which Qt cannot handle. Which is why I turned to loading directly from a Qimage even if it's slowler.
-
What exactly do you mean by RAW ?
-
I'm not sure the problem is even with the pixmap. That's why I attempted to change the style sheet of the label in there too. The style sheet does not get changed despite the code executing. Whereas, if I change the stylesheet in the constructor that code works. So something beyond my understanding is going on here.
-
I would try with loading a known image from the disk.
-
Hey, did you try not setting any format parameter (leaving it as auto) and see if it can show? Just
QPixmap::fromImage(qt_g_image)
And if you try to test with a image from the disk, better to load it by the full path, then it doesn't matter where you put it.
-
Use the full path to the test image. If you use a relative path it won't work because your executable is not built in the same folder as your sources.
-
I think I figured out why the code doesn't work. The function calling this method is activated by a slot in my application. However, this slot is essentially a while(true) loop that doesn't stop acquiring pictures until I press the space bar. I just noticed while acquiring images none of the other buttons on my GUI are clickable, which makes sense since that loop is still running. I thought that Qt slots were asynchronous, but I guess I'm understanding something wrong?? If anyone has ideas as to how I can fix this because I can't simply throw the function in another thread of execution.
-
Asynchronous does not mean that several threads are running in parallel. A while loop will always "block" the execution of the thread it runs in inside the body of the loop.
Did you check with the full path to a test image like I suggested ?
-
What was the issue ?