PDA

View Full Version : Enumerating QAction in a QMenuBar



cionci
18th February 2010, 11:12
I want to enumerate at runtime all QAction in a QMenuBar.
My first try was to enumerate QAction in a QMenu.



void MainWindow::buildAutoCompletion(QMenu * menu)
{
QList<QAction *> actions = menu->actions();
QList<QAction *>::const_iterator it = actions.begin();
for(; it != actions.end(); it++)
{
QAction *action = *it;
ui->plainTextEdit->setPlainText(ui->plainTextEdit->toPlainText() + action->text());
}
}

void MainWindow::on_pushButton_clicked()
{
buildAutoCompletion(ui->menuFile);
buildAutoCompletion(ui->menuModifica);
}
The application crashes when I click on the push button. If I try debugging, application hangs on menu->actions() (as I can see on the stack trace).

Qt 4.5.3 under Linux 64 bit.

Thanks

high_flyer
18th February 2010, 11:47
it looks like your menuFile object is not initialized...
Can you see in the debugger if its a valid pointer?

cionci
18th February 2010, 18:24
Sorry, but something strange happened...now the application works :confused: Anyway if I do a step to step debug gdb crashes :confused:

Any idea on how can I iterate over every QMenu in a QMenuBar ?

high_flyer
19th February 2010, 09:18
Your code should work provided that your menu is not empty.

cionci
19th February 2010, 10:05
My code enumerates QActions in a QMenu, but how can I enumerate QMenus in a QMenuBar ? Then for each QMenu I call my code above.

high_flyer
19th February 2010, 10:19
But why you would want to do that?
You either have existing menus (from an ui file) or, you want to create menus dynamically.
If you have existing menus, then these menus have actions in them, and you can have these actions as members of your window, and access them at any time.
If you create new ones at run time, again, when creating them you can store them in a vector or similar which you can return to at any time.
I am really not clear on why you need to iterate through the menus in the way you are trying....

cionci
19th February 2010, 20:28
I want to include all possible menu actions in a QCompleter, so in line edit I could execute an action only by writing it (using hints from autocompletion) and hitting Enter. I want to add all actions automatically.

cionci
20th February 2010, 08:25
I've done it ;)

void MainWindow::lineEditAutoCompletion()
{
QList<QMenu *> menus = ui->menuBar->findChildren<QMenu *>();
QList<QMenu *>::const_iterator it = menus.begin();
for(; it != menus.end(); it++)
{
if((*it)->title().size() > 0)
buildActionMap(*it);
}
QCompleter * completer = new QCompleter(actionMap.keys(), this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
ui->lineEdit->setCompleter(completer);
}

void MainWindow::buildActionMap(QMenu * menu)
{
QList<QAction *> actions = menu->actions();
QList<QAction *>::const_iterator it = actions.begin();
for(; it != actions.end(); it++)
{
if((*it)->text().size() > 0 && !(*it)->menu())
actionMap.insert((*it)->text(), *it);
}
}

void MainWindow::on_lineEdit_textChanged(QString )
{
QPalette pal;
if(actionMap.contains(ui->lineEdit->text()))
{
pal.setColor(QPalette::Text, Qt::darkGreen);
}
else
{
pal.setColor(QPalette::Text, Qt::red);
}
ui->lineEdit->setPalette(pal);
}

void MainWindow::on_lineEdit_returnPressed()
{
if(actionMap.contains(ui->lineEdit->text()))
{
actionMap[ui->lineEdit->text()]->activate(QAction::Trigger);
}
}