igoutas
igoutas

Reputation: 145

Resize QMainwindow to fit Qdockwidget size

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?

Upvotes: 0

Views: 972

Answers (2)

igoutas
igoutas

Reputation: 145

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);

Upvotes: 1

Raydel Miranda
Raydel Miranda

Reputation: 14360

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.

Upvotes: 0

Related Questions