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. QFrame does not display in UI properly
Forum Updated to NodeBB v4.3 + New Features

QFrame does not display in UI properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 1.5k Views
  • 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.
  • D Offline
    D Offline
    Dummie1138
    wrote on 16 Nov 2022, 11:31 last edited by Dummie1138
    #1

    I have some code that places a QFrame and gives it the size of a QRect.

        QRect *lineRect = new QRect(20, 160, 400, 10);
        QFrame *mainLine = new QFrame(this);
        mainLine->setFrameRect(*lineRect);
        mainLine->setFrameStyle(QFrame::Raised);
        qDebug() << *lineRect;
        qDebug() << mainLine->frameRect();
    

    I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
    And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required.

    J J 2 Replies Last reply 16 Nov 2022, 11:33
    0
    • D Dummie1138
      16 Nov 2022, 11:31

      I have some code that places a QFrame and gives it the size of a QRect.

          QRect *lineRect = new QRect(20, 160, 400, 10);
          QFrame *mainLine = new QFrame(this);
          mainLine->setFrameRect(*lineRect);
          mainLine->setFrameStyle(QFrame::Raised);
          qDebug() << *lineRect;
          qDebug() << mainLine->frameRect();
      

      I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
      And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 16 Nov 2022, 11:33 last edited by jsulm
      #2

      @Dummie1138 said in QFrame is unused:

      And right now, the issue is that mainLine-> seems not to affect any change to mainLine

      What does this mean please?
      Where is this code located?
      You also did not call show() on mainLine to actually show it...

      If you read https://doc.qt.io/qt-6/qframe.html#frameRect-prop you will see this:
      "Setting the rectangle doesn't cause a widget update".
      I suggested to you to use setGeometry() in your other thread...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Dummie1138
        16 Nov 2022, 11:31

        I have some code that places a QFrame and gives it the size of a QRect.

            QRect *lineRect = new QRect(20, 160, 400, 10);
            QFrame *mainLine = new QFrame(this);
            mainLine->setFrameRect(*lineRect);
            mainLine->setFrameStyle(QFrame::Raised);
            qDebug() << *lineRect;
            qDebug() << mainLine->frameRect();
        

        I have a warning that mainLine is not used, which I'm assuming means none of the functions are actually affecting mainLine.
        And right now, the issue is that mainLine-> seems not to affect any change to mainLine. I am not sure why that might be happening. Please let me know if more information is required.

        J Offline
        J Offline
        JonB
        wrote on 16 Nov 2022, 11:41 last edited by JonB
        #3

        @Dummie1138 said in QFrame is unused:

        I have a warning that mainLine is not used

        That would be very surprising given the code you show. Can you show this warning being issued and the code it is against to verify what you say? For example, maybe if you have a member variable also named mainLine it might report if that is never used (I don't know whether it does), but I don't see how it would from the extract you actually show....

        D 1 Reply Last reply 16 Nov 2022, 12:13
        0
        • J JonB
          16 Nov 2022, 11:41

          @Dummie1138 said in QFrame is unused:

          I have a warning that mainLine is not used

          That would be very surprising given the code you show. Can you show this warning being issued and the code it is against to verify what you say? For example, maybe if you have a member variable also named mainLine it might report if that is never used (I don't know whether it does), but I don't see how it would from the extract you actually show....

          D Offline
          D Offline
          Dummie1138
          wrote on 16 Nov 2022, 12:13 last edited by Dummie1138
          #4

          @JonB @jsulm Here is more code, thanks for your patience and time.

          NewConfig::NewConfig(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::NewConfig)
          {
              setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint);
              ui->setupUi(this);
              this->move(0, 0);
          
              //Build the main line.
              QFrame *mainLine = new QFrame(this);
              QRect lineRect = QRect(20, 160, 400, 40);
              mainLine->setFrameRect(*lineRect);
              //mainLine->setGeometry(20, 160, 400, 40);  //This was added based on jsulm's suggestions. I've disabled it for this post.
              mainLine->setLineWidth(5);
              mainLine->setFrameStyle(QFrame::Raised);
          
              qDebug() << lineRect;
              qDebug() << mainLine->frameRect();
              mainLine->show();
          }
          

          The output is:

          QRect(20,160 400x40)
          QRect(0,0 400x40)
          

          I have also restarted Qt Creator and noticed the warning having disappeared. Again, thank you for your time and patience, and please let me know if more information is required.

          J 1 Reply Last reply 16 Nov 2022, 12:19
          0
          • D Dummie1138
            16 Nov 2022, 12:13

            @JonB @jsulm Here is more code, thanks for your patience and time.

            NewConfig::NewConfig(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::NewConfig)
            {
                setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint);
                ui->setupUi(this);
                this->move(0, 0);
            
                //Build the main line.
                QFrame *mainLine = new QFrame(this);
                QRect lineRect = QRect(20, 160, 400, 40);
                mainLine->setFrameRect(*lineRect);
                //mainLine->setGeometry(20, 160, 400, 40);  //This was added based on jsulm's suggestions. I've disabled it for this post.
                mainLine->setLineWidth(5);
                mainLine->setFrameStyle(QFrame::Raised);
            
                qDebug() << lineRect;
                qDebug() << mainLine->frameRect();
                mainLine->show();
            }
            

            The output is:

            QRect(20,160 400x40)
            QRect(0,0 400x40)
            

            I have also restarted Qt Creator and noticed the warning having disappeared. Again, thank you for your time and patience, and please let me know if more information is required.

            J Offline
            J Offline
            JonB
            wrote on 16 Nov 2022, 12:19 last edited by JonB
            #5

            @Dummie1138 said in QFrame is unused:

            QRect lineRect = QRect(20, 160, 400, 40);
            mainLine->setFrameRect(*lineRect);
            

            For a start the above will not compile....

            mainLine->show();

            You are attempting to show a QFrame here, but you are inside the constructor of a QDialog (which itself is not shown at this point). Widgets should not be show()n in widget constructors. Where do you actually show the dialog?

            You have just added:

            The output is:

            How do you get output from code which as shown will not compile?

            D 1 Reply Last reply 16 Nov 2022, 12:22
            0
            • J JonB
              16 Nov 2022, 12:19

              @Dummie1138 said in QFrame is unused:

              QRect lineRect = QRect(20, 160, 400, 40);
              mainLine->setFrameRect(*lineRect);
              

              For a start the above will not compile....

              mainLine->show();

              You are attempting to show a QFrame here, but you are inside the constructor of a QDialog (which itself is not shown at this point). Widgets should not be show()n in widget constructors. Where do you actually show the dialog?

              You have just added:

              The output is:

              How do you get output from code which as shown will not compile?

              D Offline
              D Offline
              Dummie1138
              wrote on 16 Nov 2022, 12:22 last edited by Dummie1138
              #6

              @JonB Sorry, that *lineRect is a mistype. The proper one is just "lineRect".

              My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.

              J 1 Reply Last reply 16 Nov 2022, 12:24
              0
              • D Dummie1138
                16 Nov 2022, 12:22

                @JonB Sorry, that *lineRect is a mistype. The proper one is just "lineRect".

                My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.

                J Offline
                J Offline
                JonB
                wrote on 16 Nov 2022, 12:24 last edited by JonB
                #7

                @Dummie1138
                Since you are typing your code in and not pasting it, I will leave it to others answer. Too many years of replying to people who do not paste their code and after we all work on it discover it isn't actually what their code reads....

                D 1 Reply Last reply 16 Nov 2022, 12:27
                0
                • J JonB
                  16 Nov 2022, 12:24

                  @Dummie1138
                  Since you are typing your code in and not pasting it, I will leave it to others answer. Too many years of replying to people who do not paste their code and after we all work on it discover it isn't actually what their code reads....

                  D Offline
                  D Offline
                  Dummie1138
                  wrote on 16 Nov 2022, 12:27 last edited by
                  #8

                  @JonB I see. Again, thank you for your time and patience.

                  J 1 Reply Last reply 16 Nov 2022, 12:48
                  0
                  • D Dummie1138
                    16 Nov 2022, 12:27

                    @JonB I see. Again, thank you for your time and patience.

                    J Offline
                    J Offline
                    JonB
                    wrote on 16 Nov 2022, 12:48 last edited by
                    #9

                    @Dummie1138
                    You have responded politely, and I accept that :) Honestly, it's really important to actually copy & paste code when you want help, you are not the first person to have made a mistake when typing in instead, it's too unreliable!

                    I don't understand what it is that you do or do not see when/where?

                    My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.

                    Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?

                    I have not used QFrame. If @jsulm says above

                    I suggested to you to use setGeometry() in your other thread...

                    I would be looking into that if I were you :)

                    D 1 Reply Last reply 16 Nov 2022, 13:36
                    0
                    • J JonB
                      16 Nov 2022, 12:48

                      @Dummie1138
                      You have responded politely, and I accept that :) Honestly, it's really important to actually copy & paste code when you want help, you are not the first person to have made a mistake when typing in instead, it's too unreliable!

                      I don't understand what it is that you do or do not see when/where?

                      My intention is to be able to show this QFrame in my ui file, NewConfig, as a bar, if this helps.

                      Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?

                      I have not used QFrame. If @jsulm says above

                      I suggested to you to use setGeometry() in your other thread...

                      I would be looking into that if I were you :)

                      D Offline
                      D Offline
                      Dummie1138
                      wrote on 16 Nov 2022, 13:36 last edited by
                      #10

                      @JonB I will make sure to copy code instead of typing it, thanks for the advice.

                      Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?
                      I know the QFrame is run-time code, my issue is that the QFrame is not visible during run-time.

                      I have modified my code to this following, switching to setGeometry.

                          QFrame *mainLine = new QFrame(this, Qt::Widget);
                          mainLine->setGeometry(20, 160, 400, 40);
                          mainLine->setLineWidth(5);
                          mainLine->setFrameStyle(QFrame::Raised);
                      
                          qDebug() << mainLine->frameRect();
                          mainLine->show();
                      

                      This is the location on my ui where the QFrame is supposed to be:
                      4e14ebc5-ef2e-4188-927e-697fb230d317-image.png
                      (Sadly I cannot show the entire thing. I hope this remains somewhat helpful.)

                      And this is what my output looks like.

                      QRect(0,0 400x40)
                      

                      Again, please let me know if more information is required.

                      D 1 Reply Last reply 16 Nov 2022, 14:16
                      0
                      • D Dummie1138
                        16 Nov 2022, 13:36

                        @JonB I will make sure to copy code instead of typing it, thanks for the advice.

                        Are you expecting to see the QFrame visible when you are in the Designer?? That will not happen, this is run-time code. You would only see it when you show the dialog at runtime. Are you doing so, and is that when it is shown wrong somehow?
                        I know the QFrame is run-time code, my issue is that the QFrame is not visible during run-time.

                        I have modified my code to this following, switching to setGeometry.

                            QFrame *mainLine = new QFrame(this, Qt::Widget);
                            mainLine->setGeometry(20, 160, 400, 40);
                            mainLine->setLineWidth(5);
                            mainLine->setFrameStyle(QFrame::Raised);
                        
                            qDebug() << mainLine->frameRect();
                            mainLine->show();
                        

                        This is the location on my ui where the QFrame is supposed to be:
                        4e14ebc5-ef2e-4188-927e-697fb230d317-image.png
                        (Sadly I cannot show the entire thing. I hope this remains somewhat helpful.)

                        And this is what my output looks like.

                        QRect(0,0 400x40)
                        

                        Again, please let me know if more information is required.

                        D Offline
                        D Offline
                        Dummie1138
                        wrote on 16 Nov 2022, 14:16 last edited by
                        #11

                        This issue has been resolved.

                        By adding this line to the QFrame, the QFrame was finally displayed.

                        mainLine->setParent(this);
                        

                        The reason for the output of

                        qDebug << mainLine->frameRect();
                        

                        placing the location of the Rect at 0,0 is not due to a lack of response but because the Rect is always parented to the QFrame, which means it's location relative to the QFrame is always 0,0.

                        J 1 Reply Last reply 16 Nov 2022, 14:28
                        0
                        • D Dummie1138
                          16 Nov 2022, 14:16

                          This issue has been resolved.

                          By adding this line to the QFrame, the QFrame was finally displayed.

                          mainLine->setParent(this);
                          

                          The reason for the output of

                          qDebug << mainLine->frameRect();
                          

                          placing the location of the Rect at 0,0 is not due to a lack of response but because the Rect is always parented to the QFrame, which means it's location relative to the QFrame is always 0,0.

                          J Offline
                          J Offline
                          JonB
                          wrote on 16 Nov 2022, 14:28 last edited by JonB
                          #12

                          @Dummie1138 said in QFrame does not display in UI properly:

                          mainLine->setParent(this);

                          Well, I truly do not see how this could make any difference! I have tried it in my copy of your code I am playing with and it certainly makes no difference. Since you already had QFrame *mainLine = new QFrame(this) that has already done the mainLine->setParent(this); you claim makes it work!

                          Would you care to show your complete, working code for this frame/dialog? :)

                          D 1 Reply Last reply 16 Nov 2022, 14:43
                          0
                          • J JonB
                            16 Nov 2022, 14:28

                            @Dummie1138 said in QFrame does not display in UI properly:

                            mainLine->setParent(this);

                            Well, I truly do not see how this could make any difference! I have tried it in my copy of your code I am playing with and it certainly makes no difference. Since you already had QFrame *mainLine = new QFrame(this) that has already done the mainLine->setParent(this); you claim makes it work!

                            Would you care to show your complete, working code for this frame/dialog? :)

                            D Offline
                            D Offline
                            Dummie1138
                            wrote on 16 Nov 2022, 14:43 last edited by Dummie1138
                            #13

                            @JonB And now it doesn't work again.

                            Current code, as much as I can show:

                            NewConfig::NewConfig(QWidget *parent) :
                                QDialog(parent),
                                ui(new Ui::NewConfig)
                            {
                                setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint);
                                ui->setupUi(this);
                                this->move(0, 0);
                            
                                //Build the main line.
                                QFrame *frontLine = new QFrame(this);
                                frontLine->setGeometry(0, 0, 400, 40);
                                frontLine->setLineWidth(10);
                                frontLine->setFrameStyle(QFrame::Raised);
                                frontLine->setParent(this);  //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this).
                            
                                frontLine->show();
                                QLabel *labelOne = new QLabel("REEEE", this);
                                labelOne->move(200, 200);
                            }
                            

                            Edit:

                            frontLine->setStyleSheet("border: 10px solid #0000FF;")
                            

                            was added to the QFrame code. It now displays. Quite the silly oversight on my behalf.

                            J 1 Reply Last reply 16 Nov 2022, 15:00
                            0
                            • D Dummie1138
                              16 Nov 2022, 14:43

                              @JonB And now it doesn't work again.

                              Current code, as much as I can show:

                              NewConfig::NewConfig(QWidget *parent) :
                                  QDialog(parent),
                                  ui(new Ui::NewConfig)
                              {
                                  setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint);
                                  ui->setupUi(this);
                                  this->move(0, 0);
                              
                                  //Build the main line.
                                  QFrame *frontLine = new QFrame(this);
                                  frontLine->setGeometry(0, 0, 400, 40);
                                  frontLine->setLineWidth(10);
                                  frontLine->setFrameStyle(QFrame::Raised);
                                  frontLine->setParent(this);  //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this).
                              
                                  frontLine->show();
                                  QLabel *labelOne = new QLabel("REEEE", this);
                                  labelOne->move(200, 200);
                              }
                              

                              Edit:

                              frontLine->setStyleSheet("border: 10px solid #0000FF;")
                              

                              was added to the QFrame code. It now displays. Quite the silly oversight on my behalf.

                              J Offline
                              J Offline
                              JonB
                              wrote on 16 Nov 2022, 15:00 last edited by JonB
                              #14

                              @Dummie1138 said in QFrame does not display in UI properly:

                              frontLine->setParent(this); //I was working under the impression that the (this) at QFrame initialization is the same as setParent(this).

                              Indeed, as I said that was already the case! As can be verified by preceding this line with:

                              Q_ASSERT(frontLine->parent() == this);

                              So that setParent() can be removed!

                              Nor is the frontLine->show(); needed, and I would not put it into this constructor personally.

                              frontLine->setStyleSheet("border: 10px solid #0000FF;")

                              Yep, that made all the difference to visibility! :)

                              1 Reply Last reply
                              0

                              1/14

                              16 Nov 2022, 11:31

                              • Login

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