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. Drawing in QT
Forum Update on Monday, May 27th 2025

Drawing in QT

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

    Hi All,

    I'm trying to understand how to draw in QT. I have a graph which looks like this:

    graph.JPG

    I need to draw this in qt and control slope of the trapezoid with a QSlider.

    It will be really helpful if someone can show me this with a simple example.

    Thanks in advance

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

      Hi
      Which part do you need help with ?
      You can use QPainter and
      https://doc.qt.io/qt-5/qtwidgets-painting-painterpaths-example.html
      for the trapezoid

      If i read here
      https://stackoverflow.com/questions/59373961/draw-and-control-rectangle-in-qt

      It seems you want the slider to adjust the 2 top points to be the same value according to a slider.

      You can do this by updating the points for the path and call update()

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

        Hi
        very basic example. I draw on pixmap and show in label.
        Its just to show as a concept. A custom control might be better.

        alt text

        void MainWindow::on_verticalSlider_valueChanged(int value)
        {
        
            int h = ui->label->height();
            int w = ui->label->width();
        
            QPixmap pix(w, h);
            QPainter paint(&pix);
            pix.fill( Qt::white );
            paint.setPen(QColor(0, 0, 0, 255));
        
            QPainterPath polygonPath;
            polygonPath.moveTo(30.0, h);
            int topy =  h - 100 - value;
            polygonPath.lineTo(60.0, topy);
            polygonPath.lineTo(160.0, topy );
            polygonPath.lineTo(260.0, h);
            polygonPath.closeSubpath();
        
            paint.drawPath(polygonPath);
        
            ui->label->setPixmap(pix);
        }
        

        test project
        https://www.dropbox.com/s/cctwcsadkhq5x2m/trapezoidTest.zip?dl=0

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

          @mrjj Thanks a lot. That's exact info i need to start. I really appreciate your input.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            viniltc
            wrote on last edited by
            #5

            @mrjj

            I need to show the Polygon path while the MainWindow is constructed with a default value and then it should change if I drag a Slider.
            In the example, the figure is constructed under on_verticalSlider_valueChanged slot itself , so I wont see anything until I move the Slider.

            If I move the polygon painting part under the MainWindow constructor, plot does not look right:

            //this gives totally wrong figure
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                int h = ui->label->height();
                int w = ui->label->width();
                QPixmap pix(w, h);
                QPainter paint(&pix);
                pix.fill( Qt::white );
                paint.setPen(QColor(0, 0, 0, 255));
            
            
                polygonPath.moveTo(30.0, h);
                int topy =  h - 100 ;
                polygonPath.lineTo(60.0, topy);
                polygonPath.lineTo(160.0, topy );
                polygonPath.lineTo(260.0, h);
                polygonPath.closeSubpath();
                paint.drawPath(polygonPath);
            
                ui->label->setPixmap(pix);
            }
            

            basically i need to have a default figure with int topy = h - 100 then if I change Slider int topy = h - 100 - value. What's the best way to implement this?

            mrjjM 1 Reply Last reply
            0
            • V viniltc

              @mrjj

              I need to show the Polygon path while the MainWindow is constructed with a default value and then it should change if I drag a Slider.
              In the example, the figure is constructed under on_verticalSlider_valueChanged slot itself , so I wont see anything until I move the Slider.

              If I move the polygon painting part under the MainWindow constructor, plot does not look right:

              //this gives totally wrong figure
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  int h = ui->label->height();
                  int w = ui->label->width();
                  QPixmap pix(w, h);
                  QPainter paint(&pix);
                  pix.fill( Qt::white );
                  paint.setPen(QColor(0, 0, 0, 255));
              
              
                  polygonPath.moveTo(30.0, h);
                  int topy =  h - 100 ;
                  polygonPath.lineTo(60.0, topy);
                  polygonPath.lineTo(160.0, topy );
                  polygonPath.lineTo(260.0, h);
                  polygonPath.closeSubpath();
                  paint.drawPath(polygonPath);
              
                  ui->label->setPixmap(pix);
              }
              

              basically i need to have a default figure with int topy = h - 100 then if I change Slider int topy = h - 100 - value. What's the best way to implement this?

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

              @viniltc

              Hi
              In the constructor, nothing has the right sizes yet.

              So just overwrite showEvent function and call the
              on_verticalSlider_valueChanged( 100);

              to have it show up with a value from start.

              1 Reply Last reply
              2
              • V Offline
                V Offline
                viniltc
                wrote on last edited by
                #7

                @mrjj

                How do I overwrite showEvent function, should I call showEvent in the constructor?
                I tried this:

                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                
                    QWidget::showEvent(parent); 
                
                    on_verticalSlider_valueChanged(100);
                
                }
                

                this gives error:

                mainwindow.cpp:11:24: error: cannot initialize a parameter of type 'QShowEvent *' with an lvalue of type 'QWidget *'
                qwidget.h:646:40: note: passing argument to parameter 'event' here
                
                jsulmJ mrjjM 2 Replies Last reply
                0
                • V viniltc

                  @mrjj

                  How do I overwrite showEvent function, should I call showEvent in the constructor?
                  I tried this:

                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                      QWidget::showEvent(parent); 
                  
                      on_verticalSlider_valueChanged(100);
                  
                  }
                  

                  this gives error:

                  mainwindow.cpp:11:24: error: cannot initialize a parameter of type 'QShowEvent *' with an lvalue of type 'QWidget *'
                  qwidget.h:646:40: note: passing argument to parameter 'event' here
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @viniltc said in Drawing in QT:

                  How do I overwrite showEvent function

                  Like any other:

                  void MainWindow::showEvent(QShowEvent *event)
                  {
                      QWidget::showEvent(event);
                  }
                  

                  https://www.programiz.com/cpp-programming/function-overriding

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  4
                  • V viniltc

                    @mrjj

                    How do I overwrite showEvent function, should I call showEvent in the constructor?
                    I tried this:

                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        QWidget::showEvent(parent); 
                    
                        on_verticalSlider_valueChanged(100);
                    
                    }
                    

                    this gives error:

                    mainwindow.cpp:11:24: error: cannot initialize a parameter of type 'QShowEvent *' with an lvalue of type 'QWidget *'
                    qwidget.h:646:40: note: passing argument to parameter 'event' here
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @viniltc
                    Hi
                    No, overwrite means to add that function to your class so
                    its called instead of the base class.

                    void MainWindow:showEvent(QShowEvent *event)
                    {
                    QMainWindow::showEvent(event); // call base  so we dont fux things up
                    on_verticalSlider_valueChanged(100);
                    }
                    
                    1 Reply Last reply
                    3

                    • Login

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