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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM