Draw shape with Qt pushbutton
-
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(); }
-
@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 yourMainWindow
and useQGraphicsScene
to do your drawing work. -
@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)