0

I created a GUI width Qt Creator (Qt 5.0.1), of course also making use of layouts. For aesthetical reasons, I would like a QPushButton to be of the same width as another QPushButton placed in some other corner of the GUI. This other button changes the size dynamically when changing the window size, which is desired behavior.

Is there a way to (dynamically) link the sizes of these buttons without changing the layouts? If possible I'd like to avoid fixed sizes.

Chris
  • 981
  • 4
  • 14
  • 29

2 Answers2

2

You can override the resizeEvent of first and send signal(with size) to second.

Ashot
  • 10,807
  • 14
  • 66
  • 117
  • I hoped that there was a more "built-in" solution, but finally I did it as you suggested and it works fine. – Chris Sep 27 '13 at 12:54
0

I would propose the following solution (without sub-classing button class). Actually the code bellow can be used for synchronizing of any widgets, not only QPushButtons.

The SizeSynchronizer class:

/// Synchronizes the given widget's size with other's - one that the SizeSynchronizer installed on.
class SizeSynchronizer : public QObject
{
public:
    SizeSynchronizer(QWidget *w)
        :
            m_widget(w)
    {}

    bool eventFilter(QObject *obj, QEvent *ev)
    {
        if (m_widget) {
            if (ev->type() == QEvent::Resize) {
                QResizeEvent *resizeEvent = static_cast<QResizeEvent *>(ev);
                m_widget->resize(resizeEvent->size());
            }
        }
        return QObject::eventFilter(obj, ev);
    }
private:
    QWidget *m_widget;
};

Simple demonstration of class usage - synchronize two buttons:

int main(int argc, char *argv[])
{
    [..]
    // First button will be synchronized with the second one, i.e. when second
    // resized, the first one will resize too.
    QPushButton pb1("Button1");
    QPushButton pb2("Button2");

    // Create synchronizer and define the button that should be synchronized.
    SizeSynchronizer sync(&pb1);
    pb2.installEventFilter(&sync);

    pb2.show();
    pb1.show();
    [..]
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Thanks a lot for you suggestion! I tried out your code, but unfortunately it does not work. I placed a qDebug output in the eventFilter method and I found out that it does not get called when I resize the window. I created the GUI using the form editor, could this be a reason? – Chris Sep 27 '13 at 09:41
  • Yes I did, even with the wrong button I should see a debug out since all buttons get re-sized when changing the window size... – Chris Sep 27 '13 at 10:24
  • @Chris, "even with the wrong button I should see a debug out" - that is not true. You will see it only when you resize widget the event filter installed to. In my sample code it happens, when you resize "Button2". – vahancho Sep 27 '13 at 10:30
  • Maybe there is a misunderstanding or I did not express myself properly. When I re-size the window with the mouse, basically all buttons are re-sized, too, since they are stacked in layouts. So when I play with the window size I should see something anyway. – Chris Sep 27 '13 at 10:58