在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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2020-03-25 C++ strcmp与strncmp的比较
2020-03-25 Qt 状态栏(statusbar)的使用