Qt 程序初始化时QDockWidget大小的调整方法

  在QT中对于QDockWidget的resize()方法是无效的,因为QDockWidget的大小是由其中包含的控件决定的。在manual中这样说:

  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.

  因此在程序初始化时不能简单地使用resize()等方法调整QDockWidget大小。

  使用QMainWindow的程序在程序初始化时调整QDockWidget为上次程序记录下来的大小的方法:

//MainWindow中读取上次QDockWidget大小,在MainWindow的构造函数中所有控件初始化完成后调用

 1 void MainWindow::ReadSettings()
 2 
 3 {
 4     QCoreApplication::setOrganizationName("GLZN");
 5     QCoreApplication::setOrganizationDomain("glzn.com");
 6     QCoreApplication::setApplicationName("IEDClient");
 7     QSettings setting;
 8     setting.beginGroup(QString::fromUtf8("Catalogue View"));
 9     QSize size = setting.value(QString::fromUtf8("lastRuntimeSize"),QSize(500,500)).toSize();
10     setCatalogueViewSize(size);
11     setting.endGroup();
12 }
13 //MainWindow中记录程序结束时QDockWidget的大小信息,在MainWindow的onCloseEvent()中调用。
14 
15 void MainWindow::WriteSettings()
16 
17 {
18     QSettings setting;
19     setting.beginGroup(QString::fromUtf8("Catalogue View"));
20     QSize size = ui->dockWidget_Catalogue->size();
21     setting.setValue(QString::fromUtf8("lastRuntimeSize"),size);
22     //记录下QDockWidget的原先的最大尺寸与最小尺寸
23     setting.setValue(QString::fromUtf8("oldMaxSize"),QSize(16777215,16777215));
24     setting.setValue(QString::fromUtf8("oldMinSize"),QSize(1,1));
25     setting.endGroup();
26 }
27 //ReadSettings中用于调整QDockWidget的函数
28 
29 void MainWindow::setCatalogueViewSize(QSize size)
30 
31 {
32     QDockWidget* dock = ui->dockWidget_Catalogue;
33     if (size.width()>= 0)
34     {
35         int nWidth = dock->width();
36         if (nWidth <size.width())
37             dock->setMinimumWidth(size.width());
38         else
39             dock->setMaximumWidth(size.width());
40     }
41     if (size.height()>= 0)
42     {
43         int nHeight = dock->height();
44         if (nHeight <size.height())
45             dock->setMinimumHeight(size.height());
46         else
47             dock->setMaximumHeight(size.height());
48     }
49     //如果只是设定最小宽度等信息会造成QDockWidget调整大小出现问题
50     QTimer::singleShot(0,this,SLOT(onRestoreCatalogueView()));
51     //需要在构造函数完成之后的事件循环中重新调整最小宽度等信息
52     return;
53 }
54 //MainWindow中响应重新设置最小宽度等信息。
55 
56 void MainWindow::onRestoreCatalogueView()
57 
58 {
59     QSettings setting;
60     QSize oldMaxSize = setting.value(QString::fromUtf8("oldMaxSize"),QSize(16777215,16777215)).toSize();
61     QSize oldMinSize = setting.value(QString::fromUtf8("oldMinSize"),QSize(1,1)).toSize();
62     ui->dockWidget_Catalogue->setMaximumSize(oldMaxSize);
63     ui->dockWidget_Catalogue->setMinimumSize(oldMinSize);
64 }
原文地址:https://www.cnblogs.com/ybqjymy/p/14577214.html