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 ..