Hello,
I'm trying to make a menu with items that are checkable and send them to one slot:
This seemed to work but crashes the dialog when it's closed.
Diagram
::Diagram(QWidget *parent
) :{
planetsMenu = menuBar->addMenu("Planets");
for (int i = 0; i < 16; i++)
{
planet1Act
[i
] = new QAction(planetname
[i
],
this);
planet1Act[i]->setCheckable(true);
mapper->setMapping(planet1Act[i], i);
connect(planet1Act[i], SIGNAL(toggled(bool)), mapper, SLOT(map()));
planetsMenu->addAction(planet1Act[i]);
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
}
//---------------------------------------------
void Diagram::toggleplanet(int n)
{
QDebug()<< planet1Act[n]->isChecked()<<n;
}
//---------------------------------------------
Diagram::Diagram(QWidget *parent) :
QDialog(parent)
{
menuBar = new QMenuBar(this);
planetsMenu = menuBar->addMenu("Planets");
QSignalMapper *mapper = new QSignalMapper(this);
for (int i = 0; i < 16; i++)
{
planet1Act[i] = new QAction(planetname[i], this);
planet1Act[i]->setCheckable(true);
mapper->setMapping(planet1Act[i], i);
connect(planet1Act[i], SIGNAL(toggled(bool)), mapper, SLOT(map()));
planetsMenu->addAction(planet1Act[i]);
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
}
//---------------------------------------------
void Diagram::toggleplanet(int n)
{
QDebug()<< planet1Act[n]->isChecked()<<n;
}
//---------------------------------------------
To copy to clipboard, switch view to plain text mode
This works but crashes afterwards, what am I doing wrong?
If I don't use an array for the actions:
for (int i = 0; i < 16; i++)
{
planet1Act
= new QAction(planetname
[i
],
this);
planet1Act->setCheckable(true);
mapper->setMapping(planet1Act, i);
connect(planet1Act, SIGNAL(toggled(bool)), mapper, SLOT(map()));
planetsMenu->addAction(planet1Act);
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
}
//---------------------------------------------
void Diagram::toggleplanet(int n)
{
// how to get to the checked state of qaction number n ??
}
for (int i = 0; i < 16; i++)
{
planet1Act = new QAction(planetname[i], this);
planet1Act->setCheckable(true);
mapper->setMapping(planet1Act, i);
connect(planet1Act, SIGNAL(toggled(bool)), mapper, SLOT(map()));
planetsMenu->addAction(planet1Act);
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
}
//---------------------------------------------
void Diagram::toggleplanet(int n)
{
// how to get to the checked state of qaction number n ??
}
To copy to clipboard, switch view to plain text mode
This works fine but then I don't know how I can see if the action is checked or not in toggleplanet() since the slot can only have one int or object etc. I don't know how to get to the corresponding action from that one int.
If it were checkboxes or something, I could get to them in toggelplanet() but how do I get to the actions?
Thanks for helping.
Bookmarks