0
votes

I have a QMainWindow and a QDockWidget nested inside this.

I show some graphs, so the QDockWidget expands but the QMainWindow keeps it's initial size so i have to resize it using my mouse.

So, how can i make a QMainWindow resize to QDockWidget size every time?

2
If you need to resize main window to the size of containing dock window, why do you need dock window at all?vahancho
i have 2 docks side by side and the one expands.I need all the window to be expanded to this size,particularly it's height.igoutas

2 Answers

1
votes

It was easy at the end.

I take the Qsize of my QDockWidgets and i resize my QMainWIndow to this.

For example i have 2 QDockWidget side by side so what i do is

QSize siz =  Dock->size();
QSize siz2 =  Dock2->size();
resize(siz.width()+siz2.width(),siz.height);
0
votes

You might want to rewrite the resizeEvent function of the QDockWidget widget. For that you need to subclass QDockWidget.

class MYDockwidget : public QDockWidget
{
    Q_OBJECT
public:
    MYDockwidget(QWidget *parent = 0):
    QDockWidget(parent)
    {}

protected:
    void resizeEvent(QResizeEvent *event)
    {
        QDockWidget::resizeEvent(event);
        // Calulate Main window size here.
        // the main window is accesible
        // through the parent property.
    }
};

This approach works, but binds the QDockWidget's resizeEvent to the QMainWindow. The proper solution is to emit a signal when the size of the QDockWidget change.

For that you will need to define a custom signal and of course you want that signal with information about the event in question, hence our signal will be emited with a QSize argument.

class MYDockwidget : public QDockWidget
{
    Q_OBJECT

public:
    MYDockwidget(QWidget *parent = 0):
    QDockWidget(parent)
    {}

signals:
    void sizeChanged(QSize);
protected:
    void resizeEvent(QResizeEvent *event)
    {
        QDockWidget::resizeEvent(event);
        emit sizeChanged(event->size());
    }
};

After that you can write code like:

// Inside your main window.
public slots:
    void on_dock_size_changed(QSize)    

MYDockwidget *dock = new MYDockwidget(this);
connect(dock, SIGNAL(sizeChanged(QSize)), this, SLOT(on_dock_size_changed(QSize)));

void on_dock_size_changed(QSize size)
{
    // resize your main window here.
}

Disadvantage:

You will need to set the QDockWidget's properties by hand (programmatically) unless you manage your self to insert your custom widget as a QTDesigner plugin.