[Qt-interest] Using QStyleSheet to Restyle Checkable Menu Items

Jake Colman colman at ppllc.com
Fri Sep 17 18:57:51 CEST 2010


It turns out, now that I have actually displayed a checkable QAction on
a menu, that the checkable item already uses a checkbox.  All I really
need to do is configure things so that the box turns red when checked.
Ideally, I'd also prefer if the checkbox had smaller dimensions.  Any
easy way to accomplish one or both of these?

>>>>> "SAB" == Scott Aron Bloom <Scott.Bloom at onshorecs.com> writes:

   SAB> Ive never done qstylesheets without an image.. it really does
   SAB> make life easier...

   SAB> AS to the QRC, IMO, its one of the top 5 new things to learn for
   SAB> Qt3 to 4 transitions.. It makes resource reployment so much
   SAB> easier...

   SAB> If youre not using QMake, you will just have to call the qrc
   SAB> compiler to generate the C++ code from the .qrc file.

   SAB> Scott

   SAB> -----Original Message-----
   SAB> From: qt-interest-bounces at trolltech.com
   SAB> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Jake Colman
   SAB> Sent: Friday, September 17, 2010 9:46 AM
   SAB> To: qt-interest at trolltech.com
   SAB> Subject: Re: [Qt-interest] Using QStyleSheet to Restyle Checkable Menu
   SAB> Items

   SAB> Scott,

   SAB> Great information, thanks!

   SAB> Is there a way to do this without images?  If not, how would I do this
   SAB> if I am not using a .qrc file or qmake to build my application?  A
   SAB> pointer to the relevant doc would suffice if this is a stupid question.

   SAB> ...Jake

>>>>> "SAB" == Scott Aron Bloom <Scott.Bloom at onshorecs.com> writes:

   SAB> After some further investigation... the stylesheet example has a
   SAB> checkbox modification example... Here is the relevant code from
   SAB> the stylesheet.

   SAB> QMenu::indicator {
   SAB> width: 13px;
   SAB> height: 13px;
   SAB> }

   SAB> QMenu::indicator:unchecked {
   SAB> image: url(:/images/checkbox_unchecked.png);
   SAB> }

   SAB> QMenu::indicator:unchecked:hover {
   SAB> image: url(:/images/checkbox_unchecked_hover.png);
   SAB> }

   SAB> QMenu::indicator:unchecked:pressed {
   SAB> image: url(:/images/checkbox_unchecked_pressed.png);
   SAB> }

   SAB> QMenu::indicator:checked {
   SAB> image: url(:/images/checkbox_checked.png);
   SAB> }

   SAB> QMenu::indicator:checked:hover {
   SAB> image: url(:/images/checkbox_checked_hover.png);
   SAB> }

   SAB> QMenu::indicator:checked:pressed {
   SAB> image: url(:/images/checkbox_checked_pressed.png);
   SAB> }

   SAB> Create a string containing that information (the size needs to
   SAB> be bigger to look good in the QMenu) create your images and put
   SAB> them in your .qrc file), then call qApp->setStyleSheet

   SAB> The 

   SAB> -----Original Message-----
   SAB> From: qt-interest-bounces at trolltech.com
   SAB> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Scott
   SAB> Aron Bloom
   SAB> Sent: Friday, September 17, 2010 7:43 AM
   SAB> To: qt-interest at trolltech.com
   SAB> Subject: Re: [Qt-interest] Using QStyleSheet to Restyle
   SAB> Checkable Menu
   SAB> Items

   SAB> First, I would play in the Designer, and use the "setStyleSheet"
   SAB> to get
   SAB> it to work...
   SAB> http://doc.trolltech.com/4.6/stylesheet-designer.html

   SAB> For a QMenu, you need to modify the ::indicator subcontrol
   SAB> 
   SAB> http://doc.trolltech.com/4.6/stylesheet-reference.html#list-of-sub-contr
   SAB> ols

   SAB> I would recommend, you have a checked and unchecked image
   SAB> (rather than
   SAB> custom painting), so you would be modifying the :checked and
   SAB> non-checked
   SAB> states of the menu.

   SAB> I would also look at the setStyleSHeet example that comes with
   SAB> Qt

   SAB> Scott
   SAB> -----Original Message-----
   SAB> From: qt-interest-bounces at trolltech.com
   SAB> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Jake
   SAB> Colman
   SAB> Sent: Friday, September 17, 2010 5:26 AM
   SAB> To: qt-interest at trolltech.com
   SAB> Subject: [Qt-interest] Using QStyleSheet to Restyle Checkable
   SAB> Menu Items

   SAB> I would like to restyle checkable menu items so that they
   SAB> display a
   SAB> small checkbox that is red when checked and clear when
   SAB> unchecked.  With
   SAB> Qt3 I used a QCustomMenuItem and a derived paint() method to
   SAB> precisely
   SAB> render the size of the checkbox and the desired behavior.  With
   SAB> Qt4 it
   SAB> appears that QStyleSheet is the way to go is ::indicator
   SAB> subcontrol.
   SAB> But, although I have reviewed the relevant docs I am stymied as
   SAB> to how
   SAB> to do this.  Can someone help me get started with something that
   SAB> approximates what I am looking to do?

   SAB> Thanks.

   SAB> ...Jake

   SAB> -- 
   SAB> Jake Colman | Director, Software Development
   SAB> Principia Partners LLC
   SAB> 101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755
   SAB> 9770
   SAB> t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221
   SAB> 8929
   SAB> e: colman at ppllc.com | w: www.principiapartners.com
   SAB> Credit technology innovation awards winner 2008 and 2009

   SAB> _______________________________________________
   SAB> Qt-interest mailing list
   SAB> Qt-interest at trolltech.com
   SAB> http://lists.trolltech.com/mailman/listinfo/qt-interest

   SAB> _______________________________________________
   SAB> Qt-interest mailing list
   SAB> Qt-interest at trolltech.com
   SAB> http://lists.trolltech.com/mailman/listinfo/qt-interest

   SAB> -- 
   SAB> Jake Colman | Director, Software Development
   SAB> Principia Partners LLC
   SAB> 101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755 9770
   SAB> t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221 8929
   SAB> e: colman at ppllc.com | w: www.principiapartners.com
   SAB> Credit technology innovation awards winner 2008 and 2009

   SAB> _______________________________________________
   SAB> Qt-interest mailing list
   SAB> Qt-interest at trolltech.com
   SAB> http://lists.trolltech.com/mailman/listinfo/qt-interest

-- 
Jake Colman | Director, Software Development
Principia Partners LLC
101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755 9770
t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221 8929
e: colman at ppllc.com | w: www.principiapartners.com
Credit technology innovation awards winner 2008 and 2009




More information about the Qt-interest-old mailing list