PDA

View Full Version : Convert from QAction to QWidget



duck182
24th January 2011, 17:15
Hi,

i'm getting the following error message while compiling:

error C2664: 'QTest::mouseClick' : cannot convert parameter 1 from 'QAction *' to 'QWidget *'



This is my coding:


QMenuBar* mb = pWidget->findChild<QMenuBar *>("menubar");
QAction* fileAction = mb->findChild<QAction *>("File");
QAction* newProject = fileAction->findChild<QAction *>("Create New Project...");
QTest::mouseClick( newProject, Qt::LeftButton );



Two questions:
1) I know that mouseClick expects a QWidget, but in the tutorial the use e.g. QLineEdit so i think this should work with QAction, too?!
2) Is there another possibility to get the children of an object? I think it's weird to get the children from a top-object this way..

kind regards

wysota
24th January 2011, 17:22
but in the tutorial the use e.g. QLineEdit so i think this should work with QAction, too?!
No, because QLineEdit inherits QWidget and QAction does not.

Call QAction::trigger() instead of simulating a click.

duck182
25th January 2011, 09:29
So it should look like this?

newProject->trigger();

wysota
25th January 2011, 09:36
if(newProject)
newProject->trigger();
else
qFatal("Action newProject not found");

duck182
25th January 2011, 12:39
Hi,

mh okay I did not get the correct objects..

But I'm not able to get QAction::trigger() working..
The QMenuBar contains a QMenu. The QMenu contains a QAction. But this QAction has no objectName.
So how do I get the correct pointer to the QAction-item?



QMenuBar* mb = pWidget->findChild<QMenuBar *>("menubar"); // works
QMenu* fileMenu = mb->findChild<QMenu *>("menuMenu"); // works

// Now i would like to get the pointer to the QAction.
QAction* newProject = fileMenu ->findChild<QAction *>("Create New Project..."); // this won't work because the QAction-object has no name


The goal is to simulate a mouseclick on this QAction.

nightghost
25th January 2011, 13:01
You can set the objectname with QObject::setObjectName()

duck182
25th January 2011, 13:10
I'm testing a complete application so I'm not able to give object names to these QActions.

wysota
25th January 2011, 13:13
Use QMenu::actionAt() or QWidget::actions().

duck182
26th January 2011, 09:40
Okay, thank you so far.

Now i get all actions and the submenu-objects correctly.
One questions remains :)
How can I call a specific QAction? At the moment I'm iterating the QList<QAction *> to get the ations.. If there are more than one I have to select one (which is obviously possible by comparing the text of these actions). But how can I choose one without iterating?

wysota
26th January 2011, 12:16
If your action is the fifth added to the widget, you can call:

menu->actions().at(4);
etc.

duck182
26th January 2011, 12:37
Oh okay, but not via the text-property or sth equal?

wysota
26th January 2011, 15:25
You can iterate over available actions and check their text if you want.