Results 1 to 2 of 2

Thread: Changing QStyleOptionButton (QCheckbox) to use StyleSheet not working????

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Changing QStyleOptionButton (QCheckbox) to use StyleSheet not working????

    HI,

    I am trying to use a style sheet to change a QStyleOptionButton to work as a QCheckBox but display with my own checked/unchecked indicators. I have had success setting the stylesheet on normal checkboxes but in this case I need to use a QStyleOptionButton and this doesnt seem to work? My code is as follows:

    Qt Code:
    1. //m_checkBoxRect is previously setup
    2. m_checkboxRect.setY(rect.height() / 2 - m_checkboxRect.height() / 2);
    3.  
    4. QCheckBox* styledCheckBox = new QCheckBox();
    5. styledCheckBox->setGeometry(m_checkboxRect);
    6. styledCheckBox->setCheckState(m_checkboxState);
    7. styledCheckBox->setStyleSheet(
    8. "QCheckedBox::indicator:unchecked { image: url(:/Resources/indicator_off.png);}"
    9. "QCheckedBox::indicator:checked { image: url(:/Resources/indicator_on.png);}"
    10. "QCheckedBox::indicator:indeterminate { image: url(:/Resources/indicator_partial.png);}"
    11. );
    12.  
    13. option.initFrom(styledCheckBox);
    14. option.rect = m_checkboxRect;
    15.  
    16. style()->drawControl(QStyle::CE_CheckBox, &option, painter, this);
    To copy to clipboard, switch view to plain text mode 

    All I seem to get back is an unchecked checkbox which doesn't respond to my mouse presses.

    Does anyone have any ideas as to why this wont work, or perhaps another route??

  2. #2
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Changing QStyleOptionButton (QCheckbox) to use StyleSheet not working????

    I managed to find a solution to this.

    By simply drawing the pixmap rather than the control element I am able to produce th desired results. The mouse press event for the parent widget can allow me to detect mouse clicks over the m_checkboxRect area and therefore maintain the checked state.

    Qt Code:
    1. switch (m_checkboxState)
    2. {
    3. case Qt::Checked:
    4. option.state = QStyle::State_On;
    5. indicatorPixmap = QPixmap(":/Resources/indicator_on.png");
    6. break;
    7. case Qt::Unchecked:
    8. option.state = QStyle::State_Off;
    9. indicatorPixmap = QPixmap(":/Resources/indicator_off.png");
    10. break;
    11. case Qt::PartiallyChecked:
    12. option.state = QStyle::State_NoChange;
    13. indicatorPixmap = QPixmap(":/Resources/indicator_partial.png");
    14. break;
    15. default:
    16. break;
    17. }
    18.  
    19. style()->drawItemPixmap(painter, m_checkboxRect, 0, indicatorPixmap );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Adding stylesheet and icon to QStyleOptionButton
    By riarioriu3 in forum Newbie
    Replies: 0
    Last Post: 8th August 2012, 15:12
  2. stylesheet change on changing object name
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2012, 19:10
  3. Replies: 7
    Last Post: 1st November 2011, 08:55
  4. Replies: 4
    Last Post: 2nd December 2010, 15:57
  5. QCheckBox::isChecked() property is not working...
    By Cutey in forum Qt Programming
    Replies: 10
    Last Post: 18th August 2008, 09:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.