Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot set stylesheet within method body, but can inside constructor
Forum Updated to NodeBB v4.3 + New Features

Cannot set stylesheet within method body, but can inside constructor

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ejm1
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you sure the pixmap you are setting is valid ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ejm1
        wrote on last edited by
        #3

        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.

        97b7bc7f-b894-472e-9277-187eb5c61893-image.png

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          Did you try to let QPixmap operate a format conversion ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ejm1
            wrote on last edited by ejm1
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What exactly do you mean by RAW ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ejm1
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  ejm1
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      ejm1
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Bonnie
                        wrote on last edited by Bonnie
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          ejm1
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • E Offline
                              E Offline
                              ejm1
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                ejm1
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  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 ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0
                                  • E Offline
                                    E Offline
                                    ejm1
                                    wrote on last edited by
                                    #17

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

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      What was the issue ?

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved