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. Trouble using QPainter within paintEvent in QWidget
QtWS25 Last Chance

Trouble using QPainter within paintEvent in QWidget

Scheduled Pinned Locked Moved General and Desktop
qpainterpainteventqwidget
9 Posts 2 Posters 5.0k 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.
  • C Offline
    C Offline
    CamelFrog
    wrote on last edited by
    #1

    Hello,

    I have trouble understanding how to paint anything in a QWidget using Qpainter within a paintEvent. So here is my trial viewer:

    myviewer.h :

    class MyViewer : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    
    	MyViewer();
    	void paintEvent(QPaintEvent *);
    
    private:
    	QWidget *mainWidget;
    };
    

    implementation:

    MyViewer.cpp :

    MyViewer::MyViewer() : mainWidget(NULL)
    {
    	QWidget *mainWidget = new QWidget(this);//Used as a QPaintDevice.	
    	setWindowTitle(tr("Image Viewer"));
    	setCentralWidget(mainWidget);
    }
    
    void MyViewer::paintEvent(QPaintEvent *)
    {	
    
    	QPainter painter(mainWidget);
    	painter.drawArc(50, 30, 150, 150, 0, 16 * 360);
    	painter.drawArc(200, 80, 100, 100, 0, 16 * 360);
    	painter.drawArc(300, 130, 50, 50, 0, 16 * 360);
    
    }
    

    I'm only trying to reproduce this example, but in a QWidget and not directly in a QMainWindow.

    Things break at the line:
    QPainter painter(mainWidget);

    with these errors:

    ASSERT: "pd !=0" in file painting\qpainter.cpp, line 1468
    QWidget::repaint: Recursive repaint detected
    ASSERT: "pd !=0" in file painting\qpainter.cpp, line 1468

    Any idea ?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't paint like that on another widget. The painEvent is valid for the current widget only

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CamelFrog
        wrote on last edited by
        #3

        Hi again SGaist!
        By "current" do you mean "active"? I see in the documentation that there are "active" windows, but fail to see "current" windows. If yes, does the use of "QWidget::activateWindow" would make my QWidget "current" and suffice for QPainter?

        Thanks

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          No, I mean current, inactive widgets are also painted (e.g. the grayish color of a selection).

          The proper way to paint a widget is what the example is showing: paint in the subclass

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CamelFrog
            wrote on last edited by SGaist
            #5

            Ok, I got it working. So as you suggested I wrote a subclass inheriting QWidget :

            PaintWidget.h

            class PaintWidget : public QWidget // QWidget used as the QPaintDevice.
            {
            public:
            
            	PaintWidget();
            	~PaintWidget();
            	void paintEvent(QPaintEvent *);
            };
            

            where paintEvent is implemented by starting with QPainter painter(this);.

            Then this class is used in myviewer.h instead of the QWidget as a private property.

             class MyViewer : public QMainWindow
            {
                Q_OBJECT
            
            public:
            
                MyViewer();
                void paintEvent(QPaintEvent *);
            
            private:
                PaintWidget *mainWidget;
            }; 
            

            The point of all this is to draw different images by using Qsliders, where a modified image will be drawn at each change of position of the Qsliders.
            Nonetheless, still too inexperienced with all this, I must ask, does the use of a QMainWindow, and QPainter within a QWidget-based subclass, within the QMainWindow, will allow theuse of Qsliders ? Or will I only be able to add QSliders with, say, QMdiArea ?

            Thanks

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              No, you can use QSlider where you want. However from your questions, you should take the time to go trough Qt's examples and demo, you'll see there what you can accomplish.

              What property should your slider act upon ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                CamelFrog
                wrote on last edited by
                #7

                Yes i'm still experimenting with the examples and demo out there.

                The QSliders will act on the thresholding parameters (min and max of the input image data that are usually 32 bits per channel), and possibly a gamma scaling factor. These are the image properties that we (in my group) need to manipulate often, for this i'm experimenting with Qt to assess its responsiveness.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by SGaist
                  #8

                  Then don't hesitate to play, there are several imaging software that uses Qt for that kind of task, take for example Krita (painting) or digiKam (photo management)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  C 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Then don't hesitate to play, there are several imaging software that uses Qt for that kind of task, take for example Krita (painting) or digiKam (photo management)

                    C Offline
                    C Offline
                    CamelFrog
                    wrote on last edited by
                    #9

                    @SGaist
                    Ok, will do! Thanks for the advice!

                    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