Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. In case of drawcontrol function of QStyle class how can we draw control with pixmap?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
13 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    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.

    Pratik Agrawal

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      [[doc: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?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pratik041
        wrote on last edited by
        #3

        [quote author="Andre" date="1325572914"][[doc: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?[/quote]
        but if i will use QPainter for drawing like @drawpixmap()@ on widget.Does I have to take care of all the default functionalities of the widget?

        Pratik Agrawal

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pratik041
            wrote on last edited by
            #5

            [quote author="Andre" date="1325573677"]No, of course not. What is painted is just that: painted. The interactivity comes from the widget itself. [/quote]

            I was trying the following code in paintevent for checkbox
            @QPainter painter(this);

            QPixmap pixmap(":/ab_close.png");
            painter.drawPixmap (QRect(0,0,13,15),pixmap, QRect(0,0,13,15));
            

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

            Pratik Agrawal

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pratik041
                wrote on last edited by
                #7

                [quote author="Andre" date="1325576703"]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. [/quote]

                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?

                Pratik Agrawal

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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:
                  @
                  class Derived: public Base {
                  virtual void doStuff() {
                  Base::doStuff(); // <-- call to base class implementation first
                  //do whatever else you want to do
                  }
                  }
                  @

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    pratik041
                    wrote on last edited by
                    #9

                    I have inherited form Qcheckbox

                    Pratik Agrawal

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pratik041
                        wrote on last edited by
                        #11

                        [quote author="Andre" date="1325590427"]So... if you apply my advice to the paintEvent() method you reimplemented then, what do you get?[/quote]

                        I am getting the default widget
                        @I have used QcheckBox::paintevent(e); in paint event@

                        Pratik Agrawal

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            pratik041
                            wrote on last edited by
                            #13

                            [quote author="Andre" date="1325659958"]Could you post your whole implementation of your paintEvent()?[/quote]
                            this is my implementation of paintevent
                            @
                            void checkbox::paintEvent (QPaintEvent *e)
                            {
                            QCheckBox::paintEvent (e);
                            QPainter paint(this);
                            paint.drawPixmap (QRect(0,0,10,10), pixmap, QRect(0,0,10,10));

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

                            Pratik Agrawal

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved