Results 1 to 4 of 4

Thread: Problems with QActiongroup, QMenu, Qsettings

  1. #1
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt Jambi
    Platforms
    MacOS X Windows

    Default Problems with QActiongroup, QMenu, Qsettings

    Hi everyone,

    I have QMenu - LegendPositionSubMenu where I can choose the position of the legend on my plot, there are 4 QActions inside QActiongroup: right, left,top,bottom.
    I want to save the position in Qsettings, for this I wrote writeSettings():
    Qt Code:
    1. void CLegendMenu::writeSettings()
    2. {
    3. QSettings settings;
    4. settings.beginGroup("legendSettings");
    5. //add the value of CurrentPosition
    6. settings.setValue("position", CurrentPosition);
    7. settings.endGroup();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Current position is of the enum type Position.
    and I read it:
    Qt Code:
    1. void CLegendMenu::readSettings()
    2. {
    3. //read the currentPosition
    4. QSettings settings;
    5. settings.beginGroup("legendSettings");
    6.  
    7. if (settings.value("position")== 0)
    8. {
    9.  
    10. }
    11. else if (settings.value("position")== 1)
    12. {
    13. }
    14. settings.endGroup();
    15. }
    16. etc....
    To copy to clipboard, switch view to plain text mode 
    I am stucked here, because I want check action inside the menu according to the CurrentPosition value, so it can emit signal trigged(action) and for this I have already working connection.

    Thank you
    I don't know how to perform it

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problems with QActiongroup, QMenu, Qsettings

    If you want to display the CurrentPosition in the QAction/QMenu, then you need to build the QAction/QMenu dynamically as and when the user selects the QMenu
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt Jambi
    Platforms
    MacOS X Windows

    Default Re: Problems with QActiongroup, QMenu, Qsettings

    I want to save CurrentPosition when I close my application and then restore when I open it again using Qsettings. QMenu is building up every time I open the application.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problems with QActiongroup, QMenu, Qsettings

    See if ths example helps

    Qt Code:
    1. #include <QMenu>
    2. #include <QAction>
    3. #include <QSettings>
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QMenu menu("Position Menu");
    11. QSettings* settings = new QSettings("settings.ini", QSettings::IniFormat, &a);
    12.  
    13. settings->beginGroup("Position");
    14. bool up = settings->value("up", false).toBool();
    15. bool right = settings->value("right", false).toBool();
    16. bool down = settings->value("down", false).toBool();
    17. bool left = settings->value("left", false).toBool();
    18. settings->endGroup();
    19.  
    20. QAction* up_action = menu.addAction("up");
    21. up_action->setCheckable(true);
    22. up_action->setChecked(up);
    23.  
    24. QAction* right_action = menu.addAction("right");
    25. right_action->setCheckable(true);
    26. right_action->setChecked(right);
    27.  
    28. QAction* down_action = menu.addAction("down");
    29. down_action->setCheckable(true);
    30. down_action->setChecked(down);
    31.  
    32. QAction* left_action = menu.addAction("left");
    33. left_action->setCheckable(true);
    34. left_action->setChecked(left);
    35.  
    36. menu.show();
    37.  
    38. a.connect(&menu, SIGNAL(aboutToHide()), SLOT(quit()));
    39.  
    40. int ret = a.exec();
    41.  
    42. up = up_action->isChecked();
    43. right = right_action->isChecked();
    44. down = down_action->isChecked();
    45. left = left_action->isChecked();
    46.  
    47. settings->beginGroup("Position");
    48. settings->setValue("up", up);
    49. settings->setValue("right", right);
    50. settings->setValue("down", down);
    51. settings->setValue("left", left);
    52. settings->endGroup();
    53.  
    54. return ret;
    55. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Problems when styling QMenu with Qt Stylesheet
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2011, 15:34
  2. problems on the QMenu hide
    By xiongxiongchuan in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2010, 10:42
  3. Problems with QSettings
    By franco.amato in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2010, 06:20
  4. How do you add a QActionGroup to a QMenuBar?
    By grantbj74 in forum Newbie
    Replies: 1
    Last Post: 10th August 2009, 06:43
  5. QActionGroup question
    By maverick_pol in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2007, 19:34

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.