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.

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    What exactly do you mean by RAW ?

  • Its just the pixel values there's no header. It's simply the width x height of pixels given as an unsigned char*. Either way it doesn't display the pixmap I just had more success loading it from an image than the raw data.

  • 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.

  • Lifetime Qt Champion

    I would try with loading a known image from the disk.

  • I've attempted that as well. I placed the image in the directory with the ui files. Was that incorrect?

  • Hey, did you try not setting any format parameter (leaving it as auto) and see if it can show? Just


    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.

  • @Bonnie I'll try both of those tomorrow and let ya know how it goes.

  • Lifetime Qt Champion

    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.

  • Ok I added QCoreApplication::processEvents(); into the blocking event and all of the buttons are unblocked; however, the pixmap still did not display so I guess that was not the issue.

  • Lifetime Qt Champion

    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 ?

  • I fixed the issue. It turns out it was not fully Qt related. I did add a call to update() however.

  • Lifetime Qt Champion

    What was the issue ?

Log in to reply