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. attach Qslider to a paint event

attach Qslider to a paint event

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 548 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.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    Hi all,

    I have a paint event implemented as follows:

    void MainWindow::paintEvent(QPaintEvent *e)
    {
        QPainter painter(this);
        QPen pointpen(Qt::black);
        pointpen.setWidth(5);
        QPen linepen(Qt::red);
        linepen.setWidth(2);
    
    
        QPoint p1;
        p1.setX(400);
        p1.setY(200);  //--> set QSlider value
    
        QPoint p2;
        p2.setX(500);
        p2.setY(100); //---> set QSlider value
    
        painter.setPen(linepen);
    
    
        painter.drawLine(p1,p2);
    
        painter.setPen(pointpen);
        painter.drawPoint(p1);
        painter.drawPoint(p2);
    }
    

    in this, I'm setting fixed y values to p1 and p2.
    How do I set dynamic values to y from a QSlider?

    Thanks in advance

    ODБOïO 1 Reply Last reply
    0
    • V viniltc

      Hi all,

      I have a paint event implemented as follows:

      void MainWindow::paintEvent(QPaintEvent *e)
      {
          QPainter painter(this);
          QPen pointpen(Qt::black);
          pointpen.setWidth(5);
          QPen linepen(Qt::red);
          linepen.setWidth(2);
      
      
          QPoint p1;
          p1.setX(400);
          p1.setY(200);  //--> set QSlider value
      
          QPoint p2;
          p2.setX(500);
          p2.setY(100); //---> set QSlider value
      
          painter.setPen(linepen);
      
      
          painter.drawLine(p1,p2);
      
          painter.setPen(pointpen);
          painter.drawPoint(p1);
          painter.drawPoint(p2);
      }
      

      in this, I'm setting fixed y values to p1 and p2.
      How do I set dynamic values to y from a QSlider?

      Thanks in advance

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @viniltc hi,
      you can move p1 and p2 to member variables section, then just connect your sliders valueChanged signal to a method where you modify p1 and p2

      V 1 Reply Last reply
      3
      • V Offline
        V Offline
        viniltc
        wrote on last edited by viniltc
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • ODБOïO ODБOï

          @viniltc hi,
          you can move p1 and p2 to member variables section, then just connect your sliders valueChanged signal to a method where you modify p1 and p2

          V Offline
          V Offline
          viniltc
          wrote on last edited by
          #4

          @LeLev

          Thanks for your feedback.

          Here I can now vary p1.y and p2.y with QSlider, but as I'm calling:

          painter.drawLine(p1,p2);
          

          I expect realtime change of height of line as I change the Slider. But this is stuck at one value and does not change

          void MainWindow::paintEvent(QPaintEvent *e)
          {
          
              p1.setX(400);
              p2.setX(500);
          
              QPainter painter(this);
              QPen pointpen(Qt::black);
              pointpen.setWidth(5);
              QPen linepen(Qt::red);
              linepen.setWidth(2);
              painter.setPen(linepen);
          
              painter.drawLine(p1,p2); //----> can't see realtime chnage as I change Slider
          
              painter.setPen(pointpen);
              painter.drawPoint(p1);
              painter.drawPoint(p2);
          }
          
          void MainWindow::changeP1value(int value)
          {
              p1.setY(value);
              p2.setY(value);
          }
          
          //connection in class constructor
          connect(ui->verticalSlider, &QSlider::valueChanged, this, &MainWindow::changeP1value);
          

          Can you spot an error?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @viniltc said in attach Qslider to a paint event:
            Hi

            void MainWindow::changeP1value(int value)
            {
            p1.setY(value);
            p2.setY(value);
            update(); // make it repaint so it uses the new values
            }

            1 Reply Last reply
            3
            • V Offline
              V Offline
              viniltc
              wrote on last edited by
              #6

              @mrjj Thanks a lot, now it works fine! :)

              mrjjM 1 Reply Last reply
              1
              • V viniltc

                @mrjj Thanks a lot, now it works fine! :)

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

                @viniltc
                Hi
                Well you did good job of following @LeLev advice so it was just a very minor detail
                but its good to note that when you change member values,
                you must call update() for it to repaint as it won't detect you change them.

                1 Reply Last reply
                2

                • Login

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