[pyqt] How to copy the content of a QPopupMenu ?

K

kobayashi

Dear all,

For a particuliar case, I need to copy all the items of
an existing menu (including the separators and sub-menus)
in a new empty one ... Does someone knows how to do that ?
I've tried :
item = menu_orig.findItem(item_id)
menu_new.insertItem(item, item_id)
but it fails with :
TypeError: argument 1 of QMenuData.insertItem() has an invalid type

Thanks in advance,

Cheers,

K.
 
D

Diez B. Roggisch

Dear all,

For a particuliar case, I need to copy all the items of
an existing menu (including the separators and sub-menus)
in a new empty one ... Does someone knows how to do that ?
I've tried :
item = menu_orig.findItem(item_id)
menu_new.insertItem(item, item_id)
but it fails with :
TypeError: argument 1 of QMenuData.insertItem() has an invalid type

From the docs:

"""
QMenuItem * QMenuData::findItem ( int id ) const
Returns the menu item with identifier id, or 0 if there is no item with this
identifier.
Note that QMenuItem is an internal class, and that you should not need to
call this function. Use the higher level functions like text(), pixmap()
and changeItem() to get and modify menu item attributes instead.
See also indexOf().

"""

Diez
 
K

kobayashi

Dear Diez,

Thanks ... I've already read the doc :)

In my case, I would like to copy an item from
a popup menu to another popup menu ... Of course,
I can create a new item with same name, same pixmap
and so on but I don't know how to execute the same code
when my new item is activated ... And I don't know how
to treat the separators (Is there a way to distinguish a
real item from a separator ?) And what about the sub-menus ...

In fact, all items (usual items, separators and sub-menus) are
.... items. So I was wondering if it is a way to copy an item
(more precisely to get an item then to insert it in another menu)

Cheers,

E.A.
 
D

Diez B. Roggisch

Dear Diez,

Thanks ... I've already read the doc :)

In my case, I would like to copy an item from
a popup menu to another popup menu ... Of course,
I can create a new item with same name, same pixmap
and so on but I don't know how to execute the same code
when my new item is activated ... And I don't know how
to treat the separators (Is there a way to distinguish a
real item from a separator ?) And what about the sub-menus ...

In fact, all items (usual items, separators and sub-menus) are
... items. So I was wondering if it is a way to copy an item
(more precisely to get an item then to insert it in another menu)

Your approach at whole must be changed. You can't just insert the old items
somewhere else - they have a 1:n parent-relation that would be changed -
either crashing, or simply removing your item from your old menu.

What you need is a different means to create the items - some
factory/function that gets passed a QPopupMenu and adds all the items,
together with the connectrions and so on. You then invoke that on the other
menu.

Diez
 
D

David Boddie

(e-mail address removed) wrote: > In my case, I would like to copy an
item from > a popup menu to another popup menu ... Of course, > I can
create a new item with same name, same pixmap > and so on but I don't
know how to execute the same code > when my new item is activated ...
And I don't know how > to treat the separators (Is there a way to
distinguish a > real item from a separator ?) And what about the
sub-menus ... You might want to use QAction to define individual items
that can be activated. That way you can just reuse the same actions in
both menus or move them from one to another. Submenus will probably
take more effort to move. In some ways, it might be useful to keep some
sort of data structure around that you can use as a template for the
menu, and regenerate the menu from that. > In fact, all items (usual
items, separators and sub-menus) are > ... items. So I was wondering if
it is a way to copy an item > (more precisely to get an item then to
insert it in another menu) Not with one single operation as far as I
can see, unless you're using QAction, and even then it will only work
for normal menu items. Aside: The Qt 4 action system probably makes it
a lot easier to do what you're trying to do, though that's not a lot of
help if your project is based on Qt 3. :-/ David
 
D

David Boddie

Well, the Google web interface didn't quite work as expected there.
Here's a version of the previous message containing just my advice.


You might want to use QAction to define individual items that can be
activated. That way you can just reuse the same actions in both menus
or move them from one to another. Submenus will probably take more
effort to move. In some ways, it might be useful to keep some sort of
data structure around that you can use as a template for the menu, and
regenerate the menu from that.


Aside: The Qt 4 action system probably makes it a lot easier to do what
you're trying to do, though that's not a lot of help if your project is
based on Qt 3. :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,171
Latest member
EllaHolmwo

Latest Threads

Top