天天看点

Qt下设置停靠窗口的大小

A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on wether it is docked; a docked QDockWidget has no frame and a smaller title bar.(Come From English document).

上面的主要意思是说设置停靠窗口的大小不能在QDockWidget中设置,因为他们的改变取决于是否停靠。所以要设置停靠窗口的大小应该在QDockWidget上放置的窗体本s身中设置。

Example:

//set the width in the child form

//in the child  form constructor I set the width

#include "mywidget.h"

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    this->setMaximumWidth(400);
}
           

//in the main.cpp, I add a dock window of MyWidget instance into the main window

#include "mainwindow.h"
#include "mywidget.h"
#include <QApplication>
#include <QDockWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    MyWidget *widget = new MyWidget;

    QDockWidget *dockWidget = new QDockWidget;
    dockWidget->setWidget(widget);
    w.addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
    w.show();
    
    return a.exec();
}
           

//then you can run the program, you can drag the border of the dock window, you will find that only drag into 400.