Results 1 to 4 of 4

Thread: how to draw check box using qstyle in a qgraphicsitem

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to draw check box using qstyle in a qgraphicsitem

    hi friends,

    how to draw checkbox using qstyle in a qgraphicsitem .?
    i tried not to use qgraphicsproxy widget so i tried to use qstyle but i was facing a problem in setting the text color of the checkbox.

    please correct me if im wrong in this implementation ..

    Qt Code:
    1. class CheckBoxItem : public QGraphicsObject,public QGraphicsLayoutItem
    2. {
    3. public:
    4. CheckBoxItem(const QString & text,QGraphicsItem *parent =0);
    5. QString m_text;
    6. QRectF boundingRect() const;
    7. protected:
    8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    9.  
    10. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    11.  
    12.  
    13. QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
    14. void setGeometry(const QRectF &rect);
    15.  
    16.  
    17. private:
    18. bool m_mousePressed;
    19.  
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    in .cpp
    Qt Code:
    1. CheckBoxItem::CheckBoxItem(const QString &text, QGraphicsItem *parent)
    2. : QGraphicsObject(parent),m_mousePressed(false)
    3. {
    4. m_text = text;
    5. }
    6. void
    7. CheckBoxItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    8. {
    9. QFont font("Serif",8);
    10. painter->setFont(font);
    11.  
    12. const QStyle *style = QApplication::style();
    13.  
    14. opt.state |= m_mousePressed ? QStyle::State_On : QStyle::State_Off;
    15. opt.state |= QStyle::State_Enabled;
    16. // opt.palette = QPalette(Qt::red);
    17. opt.text = m_text;
    18. opt.rect = option->rect;
    19. opt.palette = QPalette(Qt::white);
    20.  
    21. style->drawControl(QStyle::CE_CheckBox,&opt,painter);
    22.  
    23. }
    24.  
    25. CheckBoxItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
    26. {
    27. return boundingRect().size();
    28. }
    29.  
    30. void
    31. CheckBoxItem::setGeometry(const QRectF &rect)
    32. {
    33. setPos(rect.topLeft());
    34. }
    To copy to clipboard, switch view to plain text mode 
    i dont know how to change the color and font size of the text in check box .. but check box is coming fine ..
    please help ..
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to draw check box using qstyle in a qgraphicsitem

    Why do you want to reinvent the wheel ? You can look for QML desktop elements since Qt 5.1

    And if you are not using QML, you can anyhow add a widget to QGraphicsScene

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to draw check box using qstyle in a qgraphicsitem

    you can anyhow add a widget to QGraphicsScene
    i want to add thousands of checkboxes in my view so i preffered not to go for QGraphicsWidget as to reduce load .. so i tried to make a checkbox graphicsitem with cachemode .
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to draw check box using qstyle in a qgraphicsitem

    QStyleOptionButton inherits QStyleOption. QStyleOption has fontMetrics. So, I guess, you can "feed" your own font metrics in CheckBoxItem:: paint. As for text color, QStyleOption has palette, so you just need to set proper color for QPalette::WindowText or other *Text.

    Other thing, you don't see you draw a check box's text using QStyle::drawControl with CE_CheckBoxLabel passed.
    Last edited by spirit; 23rd October 2013 at 07:11.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    wagmare (24th October 2013)

Similar Threads

  1. Draw smooth path in QGraphicsItem
    By karankumar1609 in forum Qt Programming
    Replies: 0
    Last Post: 10th July 2013, 04:35
  2. How to draw a Qgraphicsitem on a Qimage?
    By eternalthinker in forum Qt Programming
    Replies: 19
    Last Post: 15th February 2012, 15:39
  3. How to draw QGraphicsItem at the same location?
    By jeanremi in forum Qt Programming
    Replies: 13
    Last Post: 20th September 2011, 16:08
  4. Replies: 2
    Last Post: 14th July 2011, 12:28
  5. QStyle: draw QLineEdit like box
    By ickby in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2010, 18:42

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
  •  
Qt is a trademark of The Qt Company.