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.9k 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 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
              • mrjjM mrjj

                @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 Offline
                G Offline
                graniteDev
                wrote on last edited by
                #34

                @mrjj OH yes, I must have had the same exact point specified, as I tried that before and it didn't work, however this code yielded

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

                0_1526050320415_d2784871-88a0-4299-a315-a086edce69a7-image.png

                mrjjM 1 Reply Last reply
                0
                • G graniteDev

                  @mrjj OH yes, I must have had the same exact point specified, as I tried that before and it didn't work, however this code yielded

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

                  0_1526050320415_d2784871-88a0-4299-a315-a086edce69a7-image.png

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

                  @graniteDev
                  Ok super.
                  So now i wondering if we could do

                  void PointViewer::on_pushButton_clicked()
                  {
                  paintMarkers(200, 300, 50, Qt::red , 32);
                  paintMarkers(250, 250, 40, Qt::blue,128 );
                  paintMarkers(300, 200, 30, QColor(255,0,0), 64);
                  }

                  so header/function be
                  void PointViewer::paintMarkers(int x, int y, int z, QColor dotColor, int dotSize)

                  would that be good enough ?
                  and u need to use them of course

                  void paintMarkers(int x, int y, int z, QColor dotColor, int dotSize)  {
                  QPen pen(dotColor, dotSize, Qt::SolidLine, Qt::RoundCap);
                  // could we use z to adjust dotSize?
                  QPainter painter(&m_pix);
                  painter.setPen(pen);
                  painter.drawPoint(x,y);
                  ui->pointViewerLabel->setPixmap(m_pix);
                  
                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @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 Offline
                    G Offline
                    graniteDev
                    wrote on last edited by
                    #36

                    @mrjj Wow, ok this is working!! Thank you again so much for the help in understanding how to use these features.

                    I tried calling m_pix.fill(Qt::black); to clear m_pix to black again before painting new dots and this worked fine. However is this the most effective/efficient means of resetting m_pix before painting the new dots? I'll be dealing with live data at about ~50hz so I want to make this is as efficient as possible so that it looks live and fairly smooth to the user.

                    mrjjM 1 Reply Last reply
                    0
                    • G graniteDev

                      @mrjj Wow, ok this is working!! Thank you again so much for the help in understanding how to use these features.

                      I tried calling m_pix.fill(Qt::black); to clear m_pix to black again before painting new dots and this worked fine. However is this the most effective/efficient means of resetting m_pix before painting the new dots? I'll be dealing with live data at about ~50hz so I want to make this is as efficient as possible so that it looks live and fairly smooth to the user.

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

                      @graniteDev
                      Well fill is pretty fast and setPixmap(m_pix) even if it makes copy should be pretty fast too due to implicit sharing. But the downside of using this image approach is that it will be slightly more heavy than a custom paint event. However, since the pixmap acts as a caches you get free speed
                      upgrade since you dont need to draw all points.
                      But say u make pixmap 1920x1080, this might be too heavy so also depends on image size.
                      ( it takes time for QLabel to draw pixmap)
                      Also on small pc its far more heavy than say on big fat i7 desktop one.

                      so you might need a custom widget with paintEvent for optimal performance but depending on how many points and how big image must be, this might also work just fine even not the most
                      efficient way.

                      G 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @graniteDev
                        Well fill is pretty fast and setPixmap(m_pix) even if it makes copy should be pretty fast too due to implicit sharing. But the downside of using this image approach is that it will be slightly more heavy than a custom paint event. However, since the pixmap acts as a caches you get free speed
                        upgrade since you dont need to draw all points.
                        But say u make pixmap 1920x1080, this might be too heavy so also depends on image size.
                        ( it takes time for QLabel to draw pixmap)
                        Also on small pc its far more heavy than say on big fat i7 desktop one.

                        so you might need a custom widget with paintEvent for optimal performance but depending on how many points and how big image must be, this might also work just fine even not the most
                        efficient way.

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

                        @mrjj Ok, I'll test this first. If it's too slow, I'll try the custom class, although I'm concerned there as before in our attempts I couldn't get a paint event to occur on even PointViewer class.

                        mrjjM 1 Reply Last reply
                        0
                        • G graniteDev

                          @mrjj Ok, I'll test this first. If it's too slow, I'll try the custom class, although I'm concerned there as before in our attempts I couldn't get a paint event to occur on even PointViewer class.

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

                          @graniteDev
                          Well if it is too slow. come back and ill help you get paintEvent running.
                          Its not complicated and we can use almost same code except not using pixmap.
                          Would take maybe 10 mins to make it a custom control and promote the widget you have in UI to be
                          the new drawDots class.

                          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