一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

  在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 }
复制代码

 

posted on   一杯清酒邀明月  阅读(2260)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!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)的使用
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示