Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, June 2007
Problem with the menuactions


Message 1 in thread

Hi!

I have a problem with finding the menuactions within a menubar. I'm trying to 
load a menu from an .ui file and then right after this i want to connect the 
entrys to a slot (all to one). Tthe loading works good but if i try to get 
the Actions I'm running into a problem, I only get the menus and submenus but 
none of the actions that should be clickable.
Here the code i wrote:

bool UiHandling::loadMenu (QString filename, QString widgetName)
{
	if (filename.isEmpty())
		return (false);
	QUiLoader loader;
	QFile file(filename);
	if (!file.open(QFile::ReadOnly))
		return (false);
	
	QWidget *myWidget = loader.load(&file);
	menu = myWidget->findChild<QMenuBar*>(widgetName);
	if (!menu)
		return (false);
	file.close();

	QList<QAction *> allMenuActions = menu->findChildren<QAction *>();
	for (int i = 0; i < allMenuActions.size(); ++i) 
	{
qDebug() << "Action " << i << allMenuActions.at(i)->text();
		allMenuActions.at(i)->setObjectName(allMenuActions.at(i)->text());
		connect (allMenuActions.at(i), SIGNAL (triggered ()), this, SLOT 
(executeCommand()));
	}
	return (true);
}

I attached the .ui file wich is loaded here as well.
I hope you are able to help me.
Best regards,
Simon

Attachment:

Attachment: menu.ui
Description: application/designer


Message 2 in thread

On 18.06.07 19:51:54, Simon Schäfer wrote:
> I have a problem with finding the menuactions within a menubar. I'm trying to 
> load a menu from an .ui file and then right after this i want to connect the 
> entrys to a slot (all to one). Tthe loading works good but if i try to get 
> the Actions I'm running into a problem, I only get the menus and submenus but 
> none of the actions that should be clickable.
> Here the code i wrote:
> 
> bool UiHandling::loadMenu (QString filename, QString widgetName)
> {
> 	if (filename.isEmpty())
> 		return (false);
> 	QUiLoader loader;
> 	QFile file(filename);
> 	if (!file.open(QFile::ReadOnly))
> 		return (false);
> 	
> 	QWidget *myWidget = loader.load(&file);
> 	menu = myWidget->findChild<QMenuBar*>(widgetName);
>
> 	if (!menu)
> 		return (false);
> 	file.close();
> 
> 	QList<QAction *> allMenuActions = menu->findChildren<QAction *>();

Why don't you use the actions() function? A QMenu is a QWidget and thus
also has that available.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

On 18.06.07 21:04, Andreas Pakulat wrote:
> > 	QList<QAction *> allMenuActions = menu->findChildren<QAction *>();
>
> Why don't you use the actions() function? A QMenu is a QWidget and thus
> also has that available.
>
ok i didn't know that method but its not working either I just changed the 
findChildren line now i'm getting less then before. Now i only get the 
mainentries of the QMenuBar (File, Hans) but none of those within the main 
ones

so i tried to add a loop within the loop:
for (int i = 0; i < allMenuActions.size(); ++i) 
{
	qDebug() << "Action " << i << allMenuActions.at(i)->text();
	QList<QAction *> sub = allMenuActions.at(i)->menu()->actions();
	for (int j = 0; j < sub.size(); ++j)
		qDebug() << "SubAction " << j << sub.at(i)->text();
}

but this runs into an assert failure:

Action  0 "&File"
SubAction  0 "&New"
SubAction  1 "&New"
SubAction  2 "&New"
Action  1 "&Hans"
ASSERT failure in QList<T>::at: "index out of range", 
file /usr/include/qt4/QtCore/qlist.h, line 370

thanks so far but i think i need some more help

Simon

--
 [ signature omitted ] 

Message 4 in thread

On 18.06.07 21:49:46, Simon Schäfer wrote:
> On 18.06.07 21:04, Andreas Pakulat wrote:
> > > 	QList<QAction *> allMenuActions = menu->findChildren<QAction *>();
> >
> > Why don't you use the actions() function? A QMenu is a QWidget and thus
> > also has that available.
> >
> ok i didn't know that method but its not working either I just changed the 
> findChildren line now i'm getting less then before. Now i only get the 
> mainentries of the QMenuBar (File, Hans) but none of those within the main 
> ones

I guess you have to do all this because you don't know the names of the
actions in the .ui file right? Else it would be much easier.

> so i tried to add a loop within the loop:
> for (int i = 0; i < allMenuActions.size(); ++i) 
> {
> 	qDebug() << "Action " << i << allMenuActions.at(i)->text();
> 	QList<QAction *> sub = allMenuActions.at(i)->menu()->actions();
> 	for (int j = 0; j < sub.size(); ++j)
> 		qDebug() << "SubAction " << j << sub.at(i)->text();
> }
> 
> but this runs into an assert failure:
> 
> Action  0 "&File"
> SubAction  0 "&New"
> SubAction  1 "&New"
> SubAction  2 "&New"
> Action  1 "&Hans"
> ASSERT failure in QList<T>::at: "index out of range", 
> file /usr/include/qt4/QtCore/qlist.h, line 370
> 
> thanks so far but i think i need some more help

I think you need 5 minutes away from the monitor. In the last line you
access sub->at() with the wrong counting variable. It should be j there
but it is i.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

On 18.06.07 22:46 Andreas Pakulat wrote:
> I guess you have to do all this because you don't know the names of the
> actions in the .ui file right? Else it would be much easier.
yes knowing the names at runtime would be much easier but i need it the 
dynamic way, thats why i loading the .ui file right there without mocing it.
> I think you need 5 minutes away from the monitor. In the last line you
> access sub->at() with the wrong counting variable. It should be j there
> but it is i.
thats right now it works, stupid cut & paste error

thanks for helping so fast
Simon

--
 [ signature omitted ] 

Message 6 in thread

On 18.06.07 22:57:04, Simon Schäfer wrote:
> On 18.06.07 22:46 Andreas Pakulat wrote:
> > I guess you have to do all this because you don't know the names of the
> > actions in the .ui file right? Else it would be much easier.
> yes knowing the names at runtime would be much easier but i need it the 
> dynamic way, thats why i loading the .ui file right there without mocing it.

a .ui file doesn't need mocing, it only needs ui'ing ;)

> > I think you need 5 minutes away from the monitor. In the last line you
> > access sub->at() with the wrong counting variable. It should be j there
> > but it is i.
> thats right now it works, stupid cut & paste error

Good.

Andreas

-- 
 [ signature omitted ]