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. Draw shape with Qt pushbutton
Forum Updated to NodeBB v4.3 + New Features

Draw shape with Qt pushbutton

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 420 Views 2 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.
  • M Offline
    M Offline
    Moon4Walker
    wrote on last edited by
    #1

    First of all, I'm new to qt.
    I have a program that reads data from a file and draws a shape using qpaintevent. My question is, I want my function that draws a shape to be active when I press the button using a pushbutton u signal slot.

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        int w = width(), h= height();
        int xx1= w/2;
        int yy1= h/2;
        int xx2= w/2;
        int yy2= h/2;
    
        QFile file("C:/Users/Osman/Desktop/emir_kodlari.txt"); //UPDATE!
    
        if(!file.open(QFile::ReadOnly | QFile::Text)){
            QMessageBox::warning(this,"Error", "File not open");
        }
    
        QTextStream in(&file);
    
            rotate=0;
    
            QPainter painter(this) ;
            painter.setPen(QPen(Qt::black,7)); //set the color and width of line.
    
            while (!in.atEnd()) {
    
                QString line = in.readLine().trimmed();
    
               if(line.mid(0,1) == "f"){
    
                 number= ((line.mid(1, line.length())).trimmed()).toInt();
    
                 // Create the line object
                 QLineF* angleLine= new QLineF();
    
                 // Set the first point
                 angleLine->setP1(QPointF(xx1,yy1).toPoint());
    
                 angleLine->setAngle(rotate);
                 angleLine->setLength(number);
    
                 // Draw the line
                 painter.drawLine( angleLine->toLine() );
    
                 xx1 = angleLine->x2();
                 yy1 = angleLine->y2();
    
                 //qDebug()<< "number:" << number;
    
                }
    
              else if(line.mid(0,1) == "r"){
    
                   rotate += ((line.mid(1, line.length())).trimmed()).toInt();
                  // qDebug()<< "rotate:" << rotate;
               }
               else if(line=="RESET"){ //after loop-> L 4[F 100 R 90] R10
                   xx1=xx2;
                   yy1=yy2;
               }
    
                else{
                    qDebug()<< "empty line...";
                }
    
            }
            file.close();
    
    }
    
    JonBJ 1 Reply Last reply
    0
    • M Moon4Walker

      First of all, I'm new to qt.
      I have a program that reads data from a file and draws a shape using qpaintevent. My question is, I want my function that draws a shape to be active when I press the button using a pushbutton u signal slot.

      void MainWindow::paintEvent(QPaintEvent *event)
      {
          int w = width(), h= height();
          int xx1= w/2;
          int yy1= h/2;
          int xx2= w/2;
          int yy2= h/2;
      
          QFile file("C:/Users/Osman/Desktop/emir_kodlari.txt"); //UPDATE!
      
          if(!file.open(QFile::ReadOnly | QFile::Text)){
              QMessageBox::warning(this,"Error", "File not open");
          }
      
          QTextStream in(&file);
      
              rotate=0;
      
              QPainter painter(this) ;
              painter.setPen(QPen(Qt::black,7)); //set the color and width of line.
      
              while (!in.atEnd()) {
      
                  QString line = in.readLine().trimmed();
      
                 if(line.mid(0,1) == "f"){
      
                   number= ((line.mid(1, line.length())).trimmed()).toInt();
      
                   // Create the line object
                   QLineF* angleLine= new QLineF();
      
                   // Set the first point
                   angleLine->setP1(QPointF(xx1,yy1).toPoint());
      
                   angleLine->setAngle(rotate);
                   angleLine->setLength(number);
      
                   // Draw the line
                   painter.drawLine( angleLine->toLine() );
      
                   xx1 = angleLine->x2();
                   yy1 = angleLine->y2();
      
                   //qDebug()<< "number:" << number;
      
                  }
      
                else if(line.mid(0,1) == "r"){
      
                     rotate += ((line.mid(1, line.length())).trimmed()).toInt();
                    // qDebug()<< "rotate:" << rotate;
                 }
                 else if(line=="RESET"){ //after loop-> L 4[F 100 R 90] R10
                     xx1=xx2;
                     yy1=yy2;
                 }
      
                  else{
                      qDebug()<< "empty line...";
                  }
      
              }
              file.close();
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Moon4Walker said in Draw shape with Qt pushbutton:

      I have a program that reads data from a file and draws a shape using qpaintevent.

      You certainly do not want your code inside a paintEvent()!

      I would suggest you put a QGraphicsView on your MainWindow and use QGraphicsScene to do your drawing work.

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @Moon4Walker said in Draw shape with Qt pushbutton:

        I have a program that reads data from a file and draws a shape using qpaintevent.

        You certainly do not want your code inside a paintEvent()!

        I would suggest you put a QGraphicsView on your MainWindow and use QGraphicsScene to do your drawing work.

        M Offline
        M Offline
        Moon4Walker
        wrote on last edited by
        #3

        @JonB so can i know the reason for this?

        mrjjM 1 Reply Last reply
        0
        • M Moon4Walker

          @JonB so can i know the reason for this?

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

          @Moon4Walker
          Hi
          When used in paint event, it will read the same file over and over. paintEvent is called pretty often and it's simply not
          a good design to do all every time it redraws.

          So It's better to split into a read data in function and one that draws the same. (could be paint event)

          M 1 Reply Last reply
          2
          • mrjjM mrjj

            @Moon4Walker
            Hi
            When used in paint event, it will read the same file over and over. paintEvent is called pretty often and it's simply not
            a good design to do all every time it redraws.

            So It's better to split into a read data in function and one that draws the same. (could be paint event)

            M Offline
            M Offline
            Moon4Walker
            wrote on last edited by
            #5

            @mrjj I did not fully understand what you are saying. Sorry for my bad English.

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

              Hi,

              What @mrjj suggests is to load the data only once and then reuse it in your paintEvent.

              Since the data does not change. Just read the file in your class constructor.

              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
              1

              • Login

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