在QT中對(duì)于QDockWidget的resize()方法是無(wú)效的,因?yàn)镼DockWidget的大小是由其中包含的控件決定的。在manual中這樣說(shuō):
A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should beimplemented 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.
因此在程序初始化時(shí)不能簡(jiǎn)單地使用resize()等方法調(diào)整QDockWidget大小。
使用QMainWindow的程序在程序初始化時(shí)調(diào)整QDockWidget為上次程序記錄下來(lái)的大小的方法:
//MainWindow中讀取上次QDockWidget大小,在MainWindow的構(gòu)造函數(shù)中所有控件初始化完成后調(diào)用
voidMainWindow::ReadSettings()
{
QCoreApplication::setOrganizationName("GLZN");
QCoreApplication::setOrganizationDomain("glzn.com");
QCoreApplication::setApplicationName("IEDClient");
QSettings setting;
setting.beginGroup(QString::fromUtf8("Catalogue View"));
QSize size = setting.value(QString::fromUtf8("lastRuntimeSize"),QSize(500,500)).toSize();
setCatalogueViewSize(size);
setting.endGroup();
}
//MainWindow中記錄程序結(jié)束時(shí)QDockWidget的大小信息,在MainWindow的onCloseEvent()中調(diào)用。
voidMainWindow::WriteSettings()
{
QSettings setting;
setting.beginGroup(QString::fromUtf8("Catalogue View"));
QSize size = ui->dockWidget_Catalogue->size();
setting.setValue(QString::fromUtf8("lastRuntimeSize"),size);
//記錄下QDockWidget的原先的最大尺寸與最小尺寸
setting.setValue(QString::fromUtf8("oldMaxSize"),QSize(16777215,16777215));
setting.setValue(QString::fromUtf8("oldMinSize"),QSize(1,1));
setting.endGroup();
}
//ReadSettings中用于調(diào)整QDockWidget的函數(shù)
voidMainWindow::setCatalogueViewSize(QSizesize)
{
QDockWidget* dock = ui->dockWidget_Catalogue;
if (size.width()>= 0)
{
int nWidth = dock->width();
if (nWidth <size.width())
dock->setMinimumWidth(size.width());
else
dock->setMaximumWidth(size.width());
}
if (size.height()>= 0)
{
int nHeight = dock->height();
if (nHeight <size.height())
dock->setMinimumHeight(size.height());
else
dock->setMaximumHeight(size.height());
}
//如果只是設(shè)定最小寬度等信息會(huì)造成QDockWidget調(diào)整大小出現(xiàn)問(wèn)題
QTimer::singleShot(0,this,SLOT(onRestoreCatalogueView()));
//需要在構(gòu)造函數(shù)完成之后的事件循環(huán)中重新調(diào)整最小寬度等信息
return;
}
//MainWindow中響應(yīng)重新設(shè)置最小寬度等信息。
voidMainWindow::onRestoreCatalogueView()
{
QSettings setting;
QSize oldMaxSize = setting.value(QString::fromUtf8("oldMaxSize"),QSize(16777215,16777215)).toSize();
QSize oldMinSize = setting.value(QString::fromUtf8("oldMinSize"),QSize(1,1)).toSize();
ui->dockWidget_Catalogue->setMaximumSize(oldMaxSize);
ui->dockWidget_Catalogue->setMinimumSize(oldMinSize);
}
聯(lián)系客服