Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't paint points on my widget
Qt 6.12 LTS is out! See what's new in the release blog

Can't paint points on my widget

Scheduled Pinned Locked Moved Solved General and Desktop
39 Posts 3 Posters 15.8k Views 1 Watching
  • 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.
  • G graniteDev

    @mrjj Ah, ok

    So, liveFeedWidget is a widget I added to the PointViewer class's pointviewer.ui form file. Is it possible to access it from PointViewer and override it's paintEvent without my making another class and promoting it to that class? I don't like to make extra classes if I can avoid it, it makes the code harder to follow for those that come behind me. Self contained classes are best if it can be achieved.

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #14

    @graniteDev said in Can't paint points on my widget:

    liveFeedWidget

    But is it a custom control ?
    What type is it ?

    G 1 Reply Last reply
    0
    • mrjjM mrjj

      @graniteDev said in Can't paint points on my widget:

      liveFeedWidget

      But is it a custom control ?
      What type is it ?

      G Offline
      G Offline
      graniteDev
      wrote on last edited by
      #15

      @mrjj it's just a QWidget

      0_1526045360721_69e31ace-face-4a69-8f1e-9d381138b063-image.png

      mrjjM 1 Reply Last reply
      0
      • G graniteDev

        @mrjj it's just a QWidget

        0_1526045360721_69e31ace-face-4a69-8f1e-9d381138b063-image.png

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #16

        @graniteDev
        Ok, you could use QLabel and paint to a pixmap and assign pixmap
        to the label.
        Else only way is to add paintEvent to custom widget.
        PaintEvent is virtual and only way to override is to subclass.

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Sorry didnt spot it before.
          So goal is to paint from
          PointViewer to ui->liveFeedWidget
          or IS the ui->liveFeedWidget a PointViewer instance?

          If liveFeedWidget is completely other widget , then it need to have the paintEvent and not
          PointViewer.

          You cannot paint from other widget to other widgets.

          G Offline
          G Offline
          graniteDev
          wrote on last edited by
          #17

          @mrjj

          This still didn't work

          void PointViewer::paintEvent(QPaintEvent *)
          {
              QPen pen(Qt::green, 20, Qt::SolidLine, Qt::RoundCap);
              QPainter painter(this);
              painter.setPen(pen);
              painter.drawPoint(100,200);
          }
          

          I'm expecting, that at 20 pixels I should be able to see SOME dot on the screen, but I see nothing

          mrjjM 1 Reply Last reply
          0
          • G graniteDev

            @mrjj

            This still didn't work

            void PointViewer::paintEvent(QPaintEvent *)
            {
                QPen pen(Qt::green, 20, Qt::SolidLine, Qt::RoundCap);
                QPainter painter(this);
                painter.setPen(pen);
                painter.drawPoint(100,200);
            }
            

            I'm expecting, that at 20 pixels I should be able to see SOME dot on the screen, but I see nothing

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #18

            @graniteDev

            There should be a point at 100,200

            insert qDebug() to be sure its called.

            If you really dont want a custom widget for drawing, you can do like

            QPixmap pix(200, 200);
            pix.fill(Qt::blue);
            QPainter painter(&pix);
            painter.drawpoint(50, 50);
            ui->label->setPixmap(pix); 
            
            G 2 Replies Last reply
            2
            • mrjjM mrjj

              @graniteDev

              There should be a point at 100,200

              insert qDebug() to be sure its called.

              If you really dont want a custom widget for drawing, you can do like

              QPixmap pix(200, 200);
              pix.fill(Qt::blue);
              QPainter painter(&pix);
              painter.drawpoint(50, 50);
              ui->label->setPixmap(pix); 
              
              G Offline
              G Offline
              graniteDev
              wrote on last edited by
              #19

              @mrjj WE HAVE A DOT!!!!!

              UGH, I have no idea why the previous code did not work. But this looks like it does. I'm going to see if this works for me. I'm hoping that as it's updated with new data it does not flicker.

              mrjjM 1 Reply Last reply
              0
              • G graniteDev

                @mrjj WE HAVE A DOT!!!!!

                UGH, I have no idea why the previous code did not work. But this looks like it does. I'm going to see if this works for me. I'm hoping that as it's updated with new data it does not flicker.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #20

                @graniteDev
                \o/
                Long live the DOT.

                Qwidgets uses double buffer system internally so often there is no flicker.

                Did you use pixmap or paintEvent ?

                G 1 Reply Last reply
                0
                • mrjjM mrjj

                  @graniteDev

                  There should be a point at 100,200

                  insert qDebug() to be sure its called.

                  If you really dont want a custom widget for drawing, you can do like

                  QPixmap pix(200, 200);
                  pix.fill(Qt::blue);
                  QPainter painter(&pix);
                  painter.drawpoint(50, 50);
                  ui->label->setPixmap(pix); 
                  
                  G Offline
                  G Offline
                  graniteDev
                  wrote on last edited by
                  #21

                  @mrjj Crud, now I have a different problem, how do I draw different points with different sizes at the same time? Drawing many points of the same size is easy, that's readily handled, but I need to draw different sized points to denote how far or near a marker is that the point represents.

                  mrjjM 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @graniteDev
                    \o/
                    Long live the DOT.

                    Qwidgets uses double buffer system internally so often there is no flicker.

                    Did you use pixmap or paintEvent ?

                    G Offline
                    G Offline
                    graniteDev
                    wrote on last edited by
                    #22

                    @mrjj said in Can't paint points on my widget:

                    @graniteDev
                    \o/
                    Long live the DOT.

                    Qwidgets uses double buffer system internally so often there is no flicker.

                    Did you use pixmap or paintEvent ?

                    void PointViewer::paintMarkers(int x, int y, int z)
                    {
                        QPen pen(Qt::green, z, Qt::SolidLine, Qt::RoundCap);
                        QPixmap pix(800,600);
                        pix.fill(Qt::black);
                        QPainter painter(&pix);
                        painter.setPen(pen);
                        painter.drawPoint(x,y);
                        ui->pointViewerLabel->setPixmap(pix);
                    }
                    

                    and I got a large green dot about where I expected to

                    1 Reply Last reply
                    0
                    • G graniteDev

                      @mrjj Crud, now I have a different problem, how do I draw different points with different sizes at the same time? Drawing many points of the same size is easy, that's readily handled, but I need to draw different sized points to denote how far or near a marker is that the point represents.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #23

                      @graniteDev
                      Just include pointSize with the points.
                      like
                      struct PointData {
                      int x;
                      int y;
                      int z;
                      int DotSize
                      }

                      and give that to drawing routine.
                      And in paintEvent / for image, simply set penSize from data member
                      Its easy to handle as a struct as else u get many loose variables.

                      Since you clear the pixmap pr run.
                      pix.fill(Qt::black);
                      you need to draw all points each time. or keep pixmap as member in class and
                      draw on top each time.

                      where does the points come from ?

                      G 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @graniteDev
                        Just include pointSize with the points.
                        like
                        struct PointData {
                        int x;
                        int y;
                        int z;
                        int DotSize
                        }

                        and give that to drawing routine.
                        And in paintEvent / for image, simply set penSize from data member
                        Its easy to handle as a struct as else u get many loose variables.

                        Since you clear the pixmap pr run.
                        pix.fill(Qt::black);
                        you need to draw all points each time. or keep pixmap as member in class and
                        draw on top each time.

                        where does the points come from ?

                        G Offline
                        G Offline
                        graniteDev
                        wrote on last edited by
                        #24

                        @mrjj There is a tracking camera that provides live x-y-z data. I'll have to apply a transform to make it fit in my viewing space...but that feels like the easy part right now, as I already have ready access to that data.

                        I don't quite follow what your saying, won't that only draw one dot at a time with the specified size? Then clear, and paint another dot of a different size, etc?

                        I'm using the z data to determine size, but the issue as I'm looking at the pen, is I can't assign a size to the point, because the size is applied to the pen, and the pen is applied to the painter, not the individual points that "drawPoints" draws.

                        mrjjM 1 Reply Last reply
                        0
                        • G graniteDev

                          @mrjj There is a tracking camera that provides live x-y-z data. I'll have to apply a transform to make it fit in my viewing space...but that feels like the easy part right now, as I already have ready access to that data.

                          I don't quite follow what your saying, won't that only draw one dot at a time with the specified size? Then clear, and paint another dot of a different size, etc?

                          I'm using the z data to determine size, but the issue as I'm looking at the pen, is I can't assign a size to the point, because the size is applied to the pen, and the pen is applied to the painter, not the individual points that "drawPoints" draws.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #25

                          @graniteDev
                          maybe i didnt understand properly

                          Currently one x,y,z is painted. if you call
                          paintMarkers again, it will only show the last point.
                          If plan it to show many sets of x,y,z, you must handle that.
                          One options is to move pixmap to member and not clear it in
                          paintMarkers. that way you can draw over/keep the other points.
                          That is the goal correct ?
                          To draw many 3d points on same pixmap?
                          Alternatively, it should store the points and redraw all each time.

                          G 2 Replies Last reply
                          2
                          • mrjjM mrjj

                            @graniteDev
                            maybe i didnt understand properly

                            Currently one x,y,z is painted. if you call
                            paintMarkers again, it will only show the last point.
                            If plan it to show many sets of x,y,z, you must handle that.
                            One options is to move pixmap to member and not clear it in
                            paintMarkers. that way you can draw over/keep the other points.
                            That is the goal correct ?
                            To draw many 3d points on same pixmap?
                            Alternatively, it should store the points and redraw all each time.

                            G Offline
                            G Offline
                            graniteDev
                            wrote on last edited by
                            #26

                            @mrjj Yes that is the goal, to have many points on a member variable pixMap that are all a unique size and position as determined by their x-y-z values.

                            1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @graniteDev
                              maybe i didnt understand properly

                              Currently one x,y,z is painted. if you call
                              paintMarkers again, it will only show the last point.
                              If plan it to show many sets of x,y,z, you must handle that.
                              One options is to move pixmap to member and not clear it in
                              paintMarkers. that way you can draw over/keep the other points.
                              That is the goal correct ?
                              To draw many 3d points on same pixmap?
                              Alternatively, it should store the points and redraw all each time.

                              G Offline
                              G Offline
                              graniteDev
                              wrote on last edited by
                              #27

                              @mrjj Interesting, so I added this code to my header file:

                              QPixmap m_pix;
                              

                              This to my constructor:

                              PointViewer::PointViewer(QWidget *parent) :
                                  QWidget(parent),
                                  ui(new Ui::PointViewer),
                                  m_pix(800,600)
                              {
                                  ui->setupUi(this);
                              
                                  qDebug() << "test 11";
                              
                                  m_pix.fill(Qt::black);
                                  ui->pointViewerLabel->setPixmap(m_pix);
                              }
                              

                              And this to the paintMarkers function

                              void PointViewer::paintMarkers(int x, int y, int z)
                              {
                                  QPen pen(Qt::green, z, Qt::SolidLine, Qt::RoundCap);
                                  QPainter painter(&m_pix);
                                  painter.setPen(pen);
                                  painter.drawPoint(x,y);
                                  ui->pointViewerLabel->update();
                              }
                              

                              and then called paintMarkers() with a button

                              void PointViewer::on_pushButton_clicked()
                              {
                                  paintMarkers(200, 300, 50);
                              }
                              

                              and this did not work, so I'm not sure how to use a pixmap as a member variable and still get this to work. The pixmap did appear as a black 800x600 rectangle, so that part is working, but no green dot.

                              Thank you for you help btw!

                              mrjjM 1 Reply Last reply
                              0
                              • G graniteDev

                                @mrjj Interesting, so I added this code to my header file:

                                QPixmap m_pix;
                                

                                This to my constructor:

                                PointViewer::PointViewer(QWidget *parent) :
                                    QWidget(parent),
                                    ui(new Ui::PointViewer),
                                    m_pix(800,600)
                                {
                                    ui->setupUi(this);
                                
                                    qDebug() << "test 11";
                                
                                    m_pix.fill(Qt::black);
                                    ui->pointViewerLabel->setPixmap(m_pix);
                                }
                                

                                And this to the paintMarkers function

                                void PointViewer::paintMarkers(int x, int y, int z)
                                {
                                    QPen pen(Qt::green, z, Qt::SolidLine, Qt::RoundCap);
                                    QPainter painter(&m_pix);
                                    painter.setPen(pen);
                                    painter.drawPoint(x,y);
                                    ui->pointViewerLabel->update();
                                }
                                

                                and then called paintMarkers() with a button

                                void PointViewer::on_pushButton_clicked()
                                {
                                    paintMarkers(200, 300, 50);
                                }
                                

                                and this did not work, so I'm not sure how to use a pixmap as a member variable and still get this to work. The pixmap did appear as a black 800x600 rectangle, so that part is working, but no green dot.

                                Thank you for you help btw!

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #28

                                Hi
                                You are most welcome.
                                I think the error is
                                ui->pointViewerLabel->update();
                                as that would just make it draw the copy of pixmap it has.
                                you must call setPixmap again. (no need for update as that it will do it self)

                                G 1 Reply Last reply
                                1
                                • G Offline
                                  G Offline
                                  graniteDev
                                  wrote on last edited by
                                  #29

                                  oh...ok so
                                  ui->pointViewerLabel->update() doesn't work, it needs to be ui->pointViewerLabel->setPixmap(m_pix);

                                  mrjjM 1 Reply Last reply
                                  0
                                  • G graniteDev

                                    oh...ok so
                                    ui->pointViewerLabel->update() doesn't work, it needs to be ui->pointViewerLabel->setPixmap(m_pix);

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #30

                                    @graniteDev
                                    Yes as update will just make it draw the COPY of the m_pix u give it in ctor.

                                    1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      Hi
                                      You are most welcome.
                                      I think the error is
                                      ui->pointViewerLabel->update();
                                      as that would just make it draw the copy of pixmap it has.
                                      you must call setPixmap again. (no need for update as that it will do it self)

                                      G Offline
                                      G Offline
                                      graniteDev
                                      wrote on last edited by graniteDev
                                      #31

                                      @mrjj Ok that is working , but I can't get anymore than one dot to appear. How do I add points to draw with different pens? I tried this, just to see if it would work - ideally I'll need to iterate through a list of points in the future...

                                          QPen pen1(Qt::green, 50, Qt::SolidLine, Qt::RoundCap);
                                          QPen pen2(Qt::green, 10, Qt::SolidLine, Qt::RoundCap);
                                          QPainter painter(&m_pix);
                                          painter.setPen(pen1);
                                          painter.drawPoint(300,200);
                                          painter.setPen(pen2);
                                          painter.drawPoint(200,300);
                                          ui->pointViewerLabel->setPixmap(m_pix);
                                      

                                      but I only got the first dot.

                                      mrjjM 2 Replies Last reply
                                      0
                                      • G graniteDev

                                        @mrjj Ok that is working , but I can't get anymore than one dot to appear. How do I add points to draw with different pens? I tried this, just to see if it would work - ideally I'll need to iterate through a list of points in the future...

                                            QPen pen1(Qt::green, 50, Qt::SolidLine, Qt::RoundCap);
                                            QPen pen2(Qt::green, 10, Qt::SolidLine, Qt::RoundCap);
                                            QPainter painter(&m_pix);
                                            painter.setPen(pen1);
                                            painter.drawPoint(300,200);
                                            painter.setPen(pen2);
                                            painter.drawPoint(200,300);
                                            ui->pointViewerLabel->setPixmap(m_pix);
                                        

                                        but I only got the first dot.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #32

                                        @graniteDev
                                        Well lets first test if
                                        void PointViewer::on_pushButton_clicked()
                                        {
                                        paintMarkers(200, 300, 50);
                                        paintMarkers(210, 300, 50);
                                        paintMarkers(220, 300, 50);
                                        }
                                        does what we want. We could then add new paramter for size or color

                                        G 1 Reply Last reply
                                        1
                                        • G graniteDev

                                          @mrjj Ok that is working , but I can't get anymore than one dot to appear. How do I add points to draw with different pens? I tried this, just to see if it would work - ideally I'll need to iterate through a list of points in the future...

                                              QPen pen1(Qt::green, 50, Qt::SolidLine, Qt::RoundCap);
                                              QPen pen2(Qt::green, 10, Qt::SolidLine, Qt::RoundCap);
                                              QPainter painter(&m_pix);
                                              painter.setPen(pen1);
                                              painter.drawPoint(300,200);
                                              painter.setPen(pen2);
                                              painter.drawPoint(200,300);
                                              ui->pointViewerLabel->setPixmap(m_pix);
                                          

                                          but I only got the first dot.

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by mrjj
                                          #33

                                          @graniteDev
                                          Wont it be a green small dot in big green dot ?
                                          try with red
                                          QPen pen1(Qt::green, 50, Qt::SolidLine, Qt::RoundCap);
                                          QPen pen2(Qt::red, 10, Qt::SolidLine, Qt::RoundCap);
                                          QPainter painter(&m_pix);
                                          painter.setPen(pen1);
                                          painter.drawPoint(300,200);
                                          painter.setPen(pen2);
                                          painter.drawPoint(200,300);
                                          ui->pointViewerLabel->setPixmap(m_pix);

                                          G 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
                                          • Unsolved