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

Jake Colman colman at ppllc.com
Fri Sep 17 18:45:39 CEST 2010


Scott,

Great information, thanks!

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

...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 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 Checkable Menu
   SAB> Items

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

   SAB> For a QMenu, you need to modify the ::indicator subcontrol
   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 (rather than
   SAB> custom painting), so you would be modifying the :checked and non-checked
   SAB> states of the menu.

   SAB> I would also look at the setStyleSHeet example that comes with 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 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 Menu Items

   SAB> I would like to restyle checkable menu items so that they display a
   SAB> small checkbox that is red when checked and clear when unchecked.  With
   SAB> Qt3 I used a QCustomMenuItem and a derived paint() method to precisely
   SAB> render the size of the checkbox and the desired behavior.  With Qt4 it
   SAB> appears that QStyleSheet is the way to go is ::indicator subcontrol.
   SAB> But, although I have reviewed the relevant docs I am stymied as 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 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

   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