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():
Code:
void CLegendMenu::writeSettings()
{
settings.beginGroup("legendSettings");
//add the value of CurrentPosition
settings.setValue("position", CurrentPosition);
settings.endGroup();
}
Current position is of the enum type Position.
and I read it:
Code:
void CLegendMenu::readSettings()
{
//read the currentPosition
settings.beginGroup("legendSettings");
if (settings.value("position")== 0)
{
}
else if (settings.value("position")== 1)
{
}
settings.endGroup();
}
etc....
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
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
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.
Re: Problems with QActiongroup, QMenu, Qsettings
See if ths example helps
Code:
#include <QMenu>
#include <QAction>
#include <QSettings>
#include <QApplication>
int main(int argc, char *argv[])
{
QMenu menu
("Position Menu");
settings->beginGroup("Position");
bool up = settings->value("up", false).toBool();
bool right = settings->value("right", false).toBool();
bool down = settings->value("down", false).toBool();
bool left = settings->value("left", false).toBool();
settings->endGroup();
QAction* up_action
= menu.
addAction("up");
up_action->setCheckable(true);
up_action->setChecked(up);
QAction* right_action
= menu.
addAction("right");
right_action->setCheckable(true);
right_action->setChecked(right);
QAction* down_action
= menu.
addAction("down");
down_action->setCheckable(true);
down_action->setChecked(down);
QAction* left_action
= menu.
addAction("left");
left_action->setCheckable(true);
left_action->setChecked(left);
menu.show();
a.connect(&menu, SIGNAL(aboutToHide()), SLOT(quit()));
int ret = a.exec();
up = up_action->isChecked();
right = right_action->isChecked();
down = down_action->isChecked();
left = left_action->isChecked();
settings->beginGroup("Position");
settings->setValue("up", up);
settings->setValue("right", right);
settings->setValue("down", down);
settings->setValue("left", left);
settings->endGroup();
return ret;
}