Results 1 to 7 of 7

Thread: 4.2 saveState

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 4.2 saveState

    Pieced together some quick POC code, but I believe it covers the major points. QDockWidgets do not appear to be saving state correctly with QMainWindow::saveState. Results vary with dock resizing, floating, and when not visible. I did not see this behavior in 4.1.4 open source, and tried to put together a quick poc rather then the large amount of code from my project for confirmation.

    I am not using MiniGW and am unclear if this is a problem with patches to the open source version, 4.2 changes, my horrid programing, or some change I am missing. The test I ran with the code example was on a WinXP system.

    main.h
    Qt Code:
    1. #ifndef _MAIN_H_
    2. #define _MAIN_H_
    3.  
    4. #include <QMainWindow>
    5. #include <QDockWidget>
    6. #include <QTextEdit>
    7.  
    8. class MainWindow : public QMainWindow {
    9.  
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow();
    14.  
    15. void readSettings();
    16. void writeSettings();
    17.  
    18. QTextEdit *textEdit;
    19.  
    20.  
    21. protected:
    22. void closeEvent(QCloseEvent *event);
    23.  
    24. };
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. #include "main.h"
    5.  
    6. MainWindow::MainWindow() {
    7.  
    8. textEdit = new QTextEdit;
    9. setCentralWidget(textEdit);
    10.  
    11. d1 = new QDockWidget(tr("Dock1"));
    12. d1->setObjectName(tr("d1"));
    13.  
    14. d2 = new QDockWidget(tr("Dock2"));
    15. d2->setObjectName(tr("d2"));
    16.  
    17. d3 = new QDockWidget(tr("Dock3"));
    18. d3->setObjectName(tr("d3"));
    19.  
    20. addDockWidget(Qt::RightDockWidgetArea, d1);
    21. addDockWidget(Qt::LeftDockWidgetArea, d2);
    22. addDockWidget(Qt::TopDockWidgetArea, d3);
    23.  
    24.  
    25. readSettings();
    26. }
    27.  
    28. void MainWindow::readSettings() {
    29.  
    30. QSettings settings("poc.ini",QSettings::IniFormat);
    31.  
    32. settings.beginGroup("session");
    33. restoreState(settings.value("Layout").toByteArray());
    34. QRect rect = settings.value("MainWindowGeometry",
    35. QRect(0, 0, 780, 520)).toRect();
    36. move(rect.topLeft());
    37. resize(rect.size());
    38. settings.endGroup();
    39. }
    40.  
    41. void MainWindow::writeSettings() {
    42.  
    43. QSettings settings("poc.ini",QSettings::IniFormat);
    44.  
    45. settings.beginGroup("session");
    46. settings.setValue("MainWindowGeometry", geometry());
    47.  
    48. settings.remove("Layout");
    49. settings.setValue("Layout",saveState());
    50. settings.endGroup();
    51.  
    52. }
    53.  
    54. void MainWindow::closeEvent(QCloseEvent *event)
    55. {
    56. writeSettings();
    57. event->accept();
    58. }
    59.  
    60. int main(int argc, char *argv[]) {
    61.  
    62. QApplication app(argc, argv);
    63. MainWindow mainwindow;
    64. mainwindow.show();
    65.  
    66. return app.exec();
    67. }
    To copy to clipboard, switch view to plain text mode 

    Some things you can try are:
    1. If you close a dock, and reload it will not remember it is closed.
    1a. Now if you had a dock nested in another and closed it, QMAinWindow::saveState will remember it was closed
    2. Moving a dock to another position will be remember.
    2a.Move a dock to another position and then close another, with 3 dockwidgets can cause all to be reset to default positioning.
    3. Float one dock and close another, will cause all 3 to reset to default positioning.
    Last edited by merlvingian; 16th October 2006 at 02:43. Reason: Added some tests to try

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.