Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Why there is a line on my dialog?
Forum Updated to NodeBB v4.3 + New Features

Why there is a line on my dialog?

Scheduled Pinned Locked Moved Solved Qt 6
11 Posts 4 Posters 923 Views 3 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.
  • C Offline
    C Offline
    Christina123
    wrote on 6 Apr 2021, 20:24 last edited by
    #1

    Hi, when I add the line QObject::connect(&w, &window::resized, &receiverWindow, &Receiver::resizeWindow); to my main.cpp, there is a line appearing on my window which is undesired. I wonder why is this. What I want to do here is to connect my window to receiver so that when window is resized, the receiver will automatically resize as well.
    before (2).JPG
    After (2).JPG

    //connection in main.cpp
    QObject::connect(&w, &window::resized, &receiverWindow, &Receiver::resizeWindow);
    
    //emit a signal when the window is resized
    void window::resizeEvent(QResizeEvent *e)
    {
        Q_UNUSED(e);
        windowSize -> setWidth(width());
        windowSize -> setHeight(height());
        emit resized(windowSize);
    }
    
    //a slot which resizes the receiver's window size
    void Receiver::resizeWindow(QSize* size)
    {
        resize(*size);
    }
    
    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Apr 2021, 21:02 last edited by
      #2

      In resizeEvent use the size that comes with the event: e->size() instead of width()/height().

      When overriding events don't forget to call base implementation or you might experience glitches.

      QSize is the same size as a pointer so it's no less performant and a lot safer to pass it by value, or, if you don't want to do that, then by const reference.

      C 1 Reply Last reply 6 Apr 2021, 21:51
      3
      • C Chris Kawa
        6 Apr 2021, 21:02

        In resizeEvent use the size that comes with the event: e->size() instead of width()/height().

        When overriding events don't forget to call base implementation or you might experience glitches.

        QSize is the same size as a pointer so it's no less performant and a lot safer to pass it by value, or, if you don't want to do that, then by const reference.

        C Offline
        C Offline
        Christina123
        wrote on 6 Apr 2021, 21:51 last edited by Christina123 4 Jun 2021, 21:53
        #3
        void window::resizeEvent(QResizeEvent *e)
        {
            Q_UNUSED(e);
            QSize windowSize = e->size();
            emit resized(windowSize);
        }
        void Receiver::resizeWindow(QSize size)
        {
            resize(size);
        }
        

        Despite changing the code to the code above, the line still appears on the window. It seems that by simply connecting these two dialogs will result in the appearance of line which I have no clue why is that.

        1 Reply Last reply
        0
        • C Online
          C Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on 6 Apr 2021, 22:21 last edited by
          #4

          You skipped the middle part :) Call base implementation of resizeEvent. I don't know the base implementation, but I would suspect it causes update and subsequent repaint of the window. Your receiver probably has a position slightly offset from the top and if you don't call base resizeEvent that part is not repainted.

          Also e is now used so you don't need Q_UNUSED anymore.

          1 Reply Last reply
          2
          • C Offline
            C Offline
            Christina123
            wrote on 7 Apr 2021, 09:31 last edited by
            #5

            Even though I mark the function in the class as void resizeEvent(QResizeEvent *e) override; which I think is what you mean by overriding the base class , there is still a line appearing on the screen.

            M 1 Reply Last reply 7 Apr 2021, 09:47
            0
            • C Christina123
              7 Apr 2021, 09:31

              Even though I mark the function in the class as void resizeEvent(QResizeEvent *e) override; which I think is what you mean by overriding the base class , there is still a line appearing on the screen.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Apr 2021, 09:47 last edited by mrjj 4 Jul 2021, 09:48
              #6

              @Christina123

              Hi
              The override keyword just makes the compiler complain if you do not override anything . (misspelled name, parameters wrong etc)

              You still need to call the "original" code which we talk about as " Call base implementation"

              In code it would be something like

              void window::resizeEvent(QResizeEvent *e)
              {
              QSize windowSize = e->size();
              emit resized(windowSize);
              QMainWindow::resizeEvent(e); // calling the code we "overrode"
              }

              This assumes that your class window is a subclass of QMainWindow.
              Else the " QMainWindow::" part should be replaced with the real base class name.

              1 Reply Last reply
              2
              • C Offline
                C Offline
                Christina123
                wrote on 7 Apr 2021, 10:26 last edited by
                #7

                Thank you for your reply. Despite that I call the base implementation the line still appears...

                /*My code now*/
                void window::resizeEvent(QResizeEvent *e)
                {
                    QSize windowSize = e->size();
                    emit resized(windowSize);
                    QDialog::resizeEvent(e);
                }
                
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 8 Apr 2021, 06:50 last edited by
                  #8

                  Can you explain a little more what you are trying to achieve? Are your window and Receiver objects two separate windows? Or is one contained within the other?

                  And from which Qt classes are window and Receiver derived?

                  C 1 Reply Last reply 8 Apr 2021, 10:34
                  0
                  • S SimonSchroeder
                    8 Apr 2021, 06:50

                    Can you explain a little more what you are trying to achieve? Are your window and Receiver objects two separate windows? Or is one contained within the other?

                    And from which Qt classes are window and Receiver derived?

                    C Offline
                    C Offline
                    Christina123
                    wrote on 8 Apr 2021, 10:34 last edited by
                    #9

                    @SimonSchroeder Hi, window and sender are two separate dialogs and there is no parent-child relation between them. However, they are connected through signals and slots. What I want to do here is when I resize the window dialog, which is the main interface between the user and the program, the receiver dialog will be automatically resized. The problem here is that a line suddenly appears on my window dialog not my receiver's when I add this line QObject::connect(&w, &Sender::resized, &receiverWindow, &Receiver::resizeWindow); to my code. Please tell me if you need more code to debug this. Thank you :)

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christina123
                      wrote on 8 Apr 2021, 19:56 last edited by
                      #10

                      I was being very stupid here, the line appears on my screen is actually a QRect which I accidently draw it in paintevent. Sorry for taking up your guys time to solve this issue.

                      M 1 Reply Last reply 8 Apr 2021, 20:01
                      4
                      • C Christina123
                        8 Apr 2021, 19:56

                        I was being very stupid here, the line appears on my screen is actually a QRect which I accidently draw it in paintevent. Sorry for taking up your guys time to solve this issue.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 8 Apr 2021, 20:01 last edited by
                        #11

                        @Christina123

                        Well it was good you found it. No worries.
                        Calling the base version is ultra important in most cases when using Qt so
                        if you walk away with that knowledge extra, it was all worth it :)

                        1 Reply Last reply
                        1

                        1/11

                        6 Apr 2021, 20:24

                        • Login

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