PDA

View Full Version : Problems with QActiongroup, QMenu, Qsettings



Mint87
4th January 2013, 12:04
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():


void CLegendMenu::writeSettings()
{
QSettings settings;
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:


void CLegendMenu::readSettings()
{
//read the currentPosition
QSettings settings;
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

Santosh Reddy
4th January 2013, 12:11
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

Mint87
4th January 2013, 12:28
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.

Santosh Reddy
4th January 2013, 14:07
See if ths example helps



#include <QMenu>
#include <QAction>
#include <QSettings>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QMenu menu("Position Menu");
QSettings* settings = new QSettings("settings.ini", QSettings::IniFormat, &a);

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;
}