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



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



  • @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?


  • Qt Champions 2017

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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.