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. posting a resize event from a widget to another ...
QtWS25 Last Chance

posting a resize event from a widget to another ...

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.9k 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.
  • WotanW Offline
    WotanW Offline
    Wotan
    wrote on last edited by
    #1

    I want to create two widgets, when I change the size of the first one, I want it to change the size of the other one.
    I put my project in copy :
    [0_1523539695854_TestResizeEvent.zip](Uploading 100%)

    As you can see I create two widgets and when I resize widget1 or widget2 I send a message in stderr to see if the resize is detected and when a resize is done in widget1, I post the event in widget2 :

    void Widget1::resizeEvent(QResizeEvent *event)
    {
       qDebug() << "Widget 1 : resizeEvent";
    
       QCoreApplication::postEvent(m_pOtherWidget, event);
    }
    

    But in stderr, I never see the event on the widget2. Does anyone know why ?
    Thanks for your help ...

    JonBJ 1 Reply Last reply
    0
    • WotanW Wotan

      I want to create two widgets, when I change the size of the first one, I want it to change the size of the other one.
      I put my project in copy :
      [0_1523539695854_TestResizeEvent.zip](Uploading 100%)

      As you can see I create two widgets and when I resize widget1 or widget2 I send a message in stderr to see if the resize is detected and when a resize is done in widget1, I post the event in widget2 :

      void Widget1::resizeEvent(QResizeEvent *event)
      {
         qDebug() << "Widget 1 : resizeEvent";
      
         QCoreApplication::postEvent(m_pOtherWidget, event);
      }
      

      But in stderr, I never see the event on the widget2. Does anyone know why ?
      Thanks for your help ...

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Wotan said in posting a resize event from a widget to another ...:

      I put my project in copy :
      [0_1523539695854_TestResizeEvent.zip](Uploading 100%)

      Nobody will ever ben able to see anything in that :)

      I could well be wrong, but I'm not sure you're supposed to postEvent on an existing event like this directly to another widget (event object ownership/deletion etc.). If your principle works at all, did you try sendEvent() instead?

      WotanW 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        i was wondering if you have a pointer via m_pOtherWidget
        why not simply call resize() ?

        WotanW 1 Reply Last reply
        2
        • JonBJ JonB

          @Wotan said in posting a resize event from a widget to another ...:

          I put my project in copy :
          [0_1523539695854_TestResizeEvent.zip](Uploading 100%)

          Nobody will ever ben able to see anything in that :)

          I could well be wrong, but I'm not sure you're supposed to postEvent on an existing event like this directly to another widget (event object ownership/deletion etc.). If your principle works at all, did you try sendEvent() instead?

          WotanW Offline
          WotanW Offline
          Wotan
          wrote on last edited by
          #4

          @JonB

          OK I didn't get how to attach a file so here is my code :

          #include <QApplication>
          #include <QtWidgets>
          
          //******************************************************************************
          // Objet 2
          
          class Widget2 : public QWidget
          {
          public:
             Widget2(QWidget *parent = 0);
             virtual ~Widget2();
          
          protected:
             virtual void resizeEvent(QResizeEvent *event);
          };
          
          Widget2::Widget2(QWidget *parent){Q_UNUSED(parent)}
          
          Widget2::~Widget2(){}
          
          void Widget2::resizeEvent(QResizeEvent *event)
          {
             qDebug() << "Widget 2 : resizeEvent";
          }
          
          
          //******************************************************************************
          // Objet 1
          
          class Widget1 : public QWidget
          {
          public:
             Widget1(QWidget *parent = 0);
             virtual ~Widget1();
          
             void setOtherWidget(Widget2* pWidget);
          
          protected:
             virtual void resizeEvent(QResizeEvent *event);
          
          private:
             Widget2* m_pOtherWidget;
          };
          
          Widget1::Widget1(QWidget *parent){Q_UNUSED(parent)}
          
          Widget1::~Widget1(){}
          
          void Widget1::setOtherWidget(Widget2* pWidget)
          {
             m_pOtherWidget = pWidget;
          }
          
          void Widget1::resizeEvent(QResizeEvent *event)
          {
             qDebug() << "Widget 1 : resizeEvent";
          
             QCoreApplication::sendEvent(m_pOtherWidget, event);
          }
          
          //******************************************************************************
          // Main
          
          int main(int argc, char *argv[])
          {
             QApplication app(argc, argv);
             Widget1 wid1;
             Widget2 wid2;
             wid1.setOtherWidget(&wid2);
             wid1.show();
             wid2.show();
          
             return app.exec();
          }
          
          

          As you can see I have replaced postEvent by sendEvent and you are right it work with sendEvent.

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            i was wondering if you have a pointer via m_pOtherWidget
            why not simply call resize() ?

            WotanW Offline
            WotanW Offline
            Wotan
            wrote on last edited by
            #5

            @mrjj
            I simplify my project to present my problem. In fact, I have got a widget in another widget and when I resize the main widget I want it to change the size of the inside widget. If I use resize(), I end in an infinite loop.
            In fact even if I use resizeEvent, I end in an infinite loop. Right now, I don't know how to do it, the QLayout system was not offering a solution ...

            JonBJ 1 Reply Last reply
            0
            • WotanW Offline
              WotanW Offline
              Wotan
              wrote on last edited by
              #6

              I think there is a conflict between my resize event and the stretch I put in my layout.
              I think I am gonna rebuild everything from scratch.

              Thanks everyone for your helps ...

              1 Reply Last reply
              0
              • WotanW Wotan

                @mrjj
                I simplify my project to present my problem. In fact, I have got a widget in another widget and when I resize the main widget I want it to change the size of the inside widget. If I use resize(), I end in an infinite loop.
                In fact even if I use resizeEvent, I end in an infinite loop. Right now, I don't know how to do it, the QLayout system was not offering a solution ...

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Wotan
                Yes, infinite/recursive calling of resizing across the two widgets is likely to happen given what you are trying to do. This is not Qt's problem, it's yours. If you are going to have two widgets, each of which causes the resizing of the other, you are going to get a "ping-pong" effect because that is precisely what your logic does.

                So you need to start by rethinking exactly what you want to happen, i.e. when a resize to the second widget should not raise a resize back to the first widget, to stop the infinite loop. Two possibilities come to mind:

                • If your resizing "settles down", so that the two widgets have reached their correct resizes and do not need further adjustment, you don't need to raise another resize. Perhaps you should check the current size against the resized-size, and if they are the same stop raising resizes.

                • Do something in each widget's resizeEvent to recognise if it is the cause of the other widget's resize and do not propagate the resize back to the original widget if it is already the cause of the resize.

                1 Reply Last reply
                2

                • Login

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