PDA

View Full Version : QPainter usage for simple 2D objects.



geowaverider
20th January 2012, 15:39
Hi

I have an application that reads simple data defining a 2D profile made up of lines and arcs.

I have all the logic in place in my QT app to allow input/output/modification/validation of this data.

What I'm having an issue with is creating a 2D display of this profile.

I've created the Mainwindow of this application in QTDesigner and have added a QGraphicsView. I then have created a QGraphicsScene and set this scene in my QGraphicsView. I then use a QPainterPath to created the profile. The problem is that there isn't a way to create arc segments in a QPainterPath only full circles(ellipses). So I've been researching a different solution...

I have noticed elsewhere that I can create a QPainter (which has all of the 2D types I require) and add the 2D objects and then use the QGraphicsView Render Public Function to display the objects. However I can't get anything to display....

I've done lots of research and there is plenty of advice given but none specific to my requirement and I'm stuck.

I've added the click SLOT that draws the objects. I haven't included the ui file but it only contains a QGraphicsView and a PushButton which when clicked runs the attached code.

void MainWindow::on_arcButton_clicked()
{
QRect viewport = ui->graphicsView->viewport()->rect();
QPixmap pix(viewport.width(), viewport.height());
QPainter qp;
qp.begin(&pix);
QPen pen(Qt::black);
pen.setWidth(4);
qp.setPen(pen);
qp.drawLine(0,0,10,15);
qp.drawLine(10,10,20,20);
qp.end();
ui->graphicsView->render(&qp);
}

Apologies for the code directly in the message.

Thanks

Brian