Re: QPopupMenu with dynamic adding/removing of toggled QActions possible?

Fabian Knopf <[email protected]> Wed, 02 Feb 2005 09:50:10 +0100
Newsgroups gmane.comp.mozilla.devel.qt
Message-ID <[email protected]>
Martin Baumg=E4rtner wrote:
> Hi,
>=20
> as a newcomer to the Qt library I'd like to pour out the following=20
> question to the community:
>=20
> Can anybody help me with an appropriate technique in order to implement=
=20
> a QPopupMenu which can be dynamically grown/shrinked by adding/removing=
=20
> items of class QAction.
>=20
> It's important for me to use the toggle facility of the QAction class,=20
> because there seems to be no suitable alternative, e.g. to add menu=20
> items directly using the QPopupMenu::insertItem( ... ) functions.
>=20
> // ---------- CODE START ------------
>=20
> ...
>=20
> QPopupMenu *popup_menu =3D new QPopupMenu( this );
>=20
> QAction* new_item =3D new QAction( this, "new_item" );
> new_item->setToggleAction( TRUE );
> new_item->setOn( FALSE );
> new_item->setText( "item_1" );
> new_item->addTo( popup_menu );
>=20
> ...
>=20
> // ------------ CODE END ------------
>=20
>=20
> Using this concept I don't see any chance to get a valid (int) id that =
I=20
> can give to the new_item. And that would be what I'd need to use the=20
> QPopupMenu::removeItem( id ) for deleting items - or am I wrong?
>=20
> The inherited QPopupMenu::setItemChecked( int id, bool checked ) seems=20
> to be another idea, but I think this is not an easy solution for my=20
> problem, because then I need to manage some more signal/slot-connection=
s=20
> in order to realize mouse/keyboard events.
>=20
> Is there a better technique for this task? Or do I have to manage a=20
> separate std::vector<QAction*> in order to use the QAction::removeFrom(=
=20
> QWidget *w ) for deletion?
>=20
> Thanks a lot in advance for any idea!!!!
>=20
> Martin
which version of qt do you have?