January 3, 2012

pratik041 pratik041
Lab Rat
346 posts

In case of drawcontrol function of QStyle class how can we draw control with pixmap?

 

Suppose i want to draw a custom checkbox where i want to draw image in case of default check box without affecting the checkbox text so how can we use drawcontrol class of Qstyle for that? or is their any other function for drawing it.

 Signature 

Pratik Agrawal

12 replies

January 3, 2012

Andre Andre
Robot Herder
6396 posts

QPainter is used for drawing on widgets (and other painting surfaces). Did you check its documentation? Did you notice the methods for rendering text and for rendering QPixmaps and QImages?

January 3, 2012

pratik041 pratik041
Lab Rat
346 posts

Andre wrote:
QPainter is used for drawing on widgets (and other painting surfaces). Did you check its documentation? Did you notice the methods for rendering text and for rendering QPixmaps and QImages?

but if i will use QPainter for drawing like
  1. drawpixmap()
on widget.Does I have to take care of all the default functionalities of the widget?

 Signature 

Pratik Agrawal

January 3, 2012

Andre Andre
Robot Herder
6396 posts

No, of course not. What is painted is just that: painted. The interactivity comes from the widget itself.

January 3, 2012

pratik041 pratik041
Lab Rat
346 posts
Andre wrote:
No, of course not. What is painted is just that: painted. The interactivity comes from the widget itself.

I was trying the following code in paintevent for checkbox

  1. QPainter painter(this);
  2.  
  3.     QPixmap pixmap(":/ab_close.png");
  4.     painter.drawPixmap (QRect(0,0,13,15),pixmap, QRect(0,0,13,15));

when i am using the function
  1. this->settext("dfdf")
in constructor of checkbox class the text is not displaying only the image which i have set is coming.

 Signature 

Pratik Agrawal

January 3, 2012

Andre Andre
Robot Herder
6396 posts

Of course, because you are not drawing the text. If you reimplement the paintEvent function, then that is the paintEvent function that will be called, not the original one that also takes care of drawing the text. From the top of my head, QStyle does offer methods to render this text already, but otherwise it is just a matter of rendering it yourself using the same painter. Alternatively, you could try to just call the base class implementation of the render method, and only then draw your pixmap on top of that.

January 3, 2012

pratik041 pratik041
Lab Rat
346 posts
Andre wrote:
Of course, because you are not drawing the text. If you reimplement the paintEvent function, then that is the paintEvent function that will be called, not the original one that also takes care of drawing the text. From the top of my head, QStyle does offer methods to render this text already, but otherwise it is just a matter of rendering it yourself using the same painter. Alternatively, you could try to just call the base class implementation of the render method, and only then draw your pixmap on top of that.

Sorry but this i didn’t understand “Alternatively, you could try to just call the base class implementation of the render method, and only then draw your pixmap on top of that” can you please explain it?

 Signature 

Pratik Agrawal

January 3, 2012

Andre Andre
Robot Herder
6396 posts

You subclassed a another class (a QStyle derived class, I gather), and you have reimplemented one of its methods. Right?

In general, if you want the base class (the class you subclassed or derived from) method to actually do whatever it was doing before you dediced to subclass and reimplement that method, you need to call it’s implementation from your own implementation.

There is a special syntax to do that:

  1. class Derived: public Base {
  2.   virtual void doStuff() {
  3.       Base::doStuff(); // <-- call to base class implementation first
  4.       //do whatever else you want to do
  5.   }
  6. }

January 3, 2012

pratik041 pratik041
Lab Rat
346 posts

I have inherited form Qcheckbox

 Signature 

Pratik Agrawal

January 3, 2012

Andre Andre
Robot Herder
6396 posts

So… if you apply my advice to the paintEvent() method you reimplemented then, what do you get?

January 4, 2012

pratik041 pratik041
Lab Rat
346 posts
Andre wrote:
So… if you apply my advice to the paintEvent() method you reimplemented then, what do you get?

I am getting the default widget

  1. I have used QcheckBox::paintevent(e); in paint event

 Signature 

Pratik Agrawal

January 4, 2012

Andre Andre
Robot Herder
6396 posts

Could you post your whole implementation of your paintEvent()?

January 4, 2012

pratik041 pratik041
Lab Rat
346 posts

Andre wrote:
Could you post your whole implementation of your paintEvent()?

this is my implementation of paintevent
  1.  void checkbox::paintEvent (QPaintEvent *e)
  2. {
  3.     QCheckBox::paintEvent (e);
  4.      QPainter paint(this);
  5.      paint.drawPixmap (QRect(0,0,10,10), pixmap, QRect(0,0,10,10));
  6.      
  7. }

Here i am getting both the image and the default widget as output.

 Signature 

Pratik Agrawal

 
  ‹‹ [SOLVED] Building Qt Statically Windows (Setup wrong?)      [SOLVED] how to setup tabs inside the dockwidget ››

You must log in to post a reply. Not a member yet? Register here!