[Qt-interest] Porting Qt 3 to Qt 4

Scott Aron Bloom Scott.Bloom at onshorecs.com
Fri Sep 17 01:21:53 CEST 2010



-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Jake Colman
Sent: Thursday, September 16, 2010 3:07 PM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Porting Qt 3 to Qt 4

>>>>> "AS" == Andre Somers <andre at familiesomers.nl> writes:

   AS>   Op 2-9-2010 19:21, Jake Colman schreef:

   >> The reason I am using a QCustomMenuItem is because a checked menu
   >> item in Motif (at least in our configuration) is supposed to be
   >> displayed with a box that is red when checked and cleared when
   >> not.  Qt's implementation, at least in Qt3, displayed a check
   >> instead of this box.  So I extended QPopupMenu with a
   >> insertMotifCheckItem() method to create this custom menu item that
   >> knows how to paint itself accordingly.
   >> 
   >> Having explained all this, is there a better/different way to do
   >> this in Qt4?  Please remember that I am trying to change as little
   >> code as possible (at least for now) as part of this port.

   AS> Perhaps the easiest way to do this is to use a widget style
   AS> sheet. You can customize the appearance of a menu or menu item
   AS> using a widget style sheet. Drawing a red square instead of a
   AS> checkmark should be easy. You can style the check mark using the
   AS> ::indicator subcontrol. Though... one wonders what drives a
   AS> person in this day and age to use a Motif style ;-)

Andre,

I am finally getting back to this part of my application.  I'm having
trouble understanding the QAction-based paradigm and how it compared to
the insertItem-based paradigm in Qt3.  For example, menu items no longer
have an ID and findMenu() is no longer defined.  So how does one look up
a menu item?  Is it no longer needed?

As far as my original question is concerned, can you please give me some
more detail on how I can use the widget style sheet to achieve my
desired result?  I want to provide an API that allows the coder to
insert a menu item that would display the red square instead of a
checkmark.  The coder would also have to be able to find that item later
in order to check/uncheck it - or am I missing the paradigm here?

I'd appreciate any pointers - even if it is just a list of what docs
need to be read.

TIA!

...Jake

-----------------------
The QAction is much simpler to use.. In general, you can create them two
ways..

First, you can create them on the heap and store the pointer for later.
You are then responsible for the memory (which you can always use the Qt
parent/child relationship to handle)

You then call addAction( QAction * )  on any QWidget.  QMenu and
QToolBar have special addAction methods, however, if the action has a
keyboard shortcut, and its on a widget, it will get triggered when the
keyboard shortcut is typed.


For any given action, you can have shortcuts, icons, hover text,
checkable , checked, you then also connect to the QActions triggered
signal to execute the slot.

You can assign the SAME action to multiple containers.  For instance,
you can create the action, have it owned by the main window, then add it
to a tool bar and a menu of that main window.. You might also add it to
a context menu.

The great part, is you only need to set it to "checked" once, or add its
icon once.  

Another option, that I usually only use in transient actions on context
menus, is to use the addAction( QString ) which creates a QAction with
the container as the parent.  You should NOT add these actions to
another container widget.

So your notion of finding it, isn't necessary, since now you simply
store a pointer to it.


As to the Checkbox vs the Check mark, look at the QStyleSHeet classes to
set them on the QMenu item you have

Scott







More information about the Qt-interest-old mailing list