Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Can't get QPainter to draw a simple triangle
I can't get QPainter to draw a simple triangle. The example below shows the side marked red doesn't line up at either end. You will need to zoom to see clearly. (Qt 5.0.1, MacOS X 10.8.2)
int main(int argc, char *argv)
QApplication a(argc, argv);
class DrawLabel : public QLabel
DrawLabel(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
DrawLabel::DrawLabel(QWidget* parent) :
void DrawLabel::paintEvent(QPaintEvent *event)
// Draw a triangle
painter.drawLine(10, 10, 50, 10);
painter.drawLine(10, 10, 30, 10+depth);
painter.drawLine(30, 10+depth, 50, 10);
void DrawLabel::keyPressEvent(QKeyEvent *event)
case Qt::Key_Up: depth++; break;
case Qt::Key_Down: depth--; break;
case Qt::Key_Escape: depth = 10; break;
std::cerr << "Depth now: " << depth << "\n";
Edit: inlined the images for you; Andre
qxoz last edited by
Seems this is a Mac issue, on Windows your code works as it should.
ericai last edited by
the DrawLabel.cpp is miss #include<QPainter>bq.
It makes no difference on Mac.
DerManu last edited by
Unfortunately, QPainter has been very broken since Qt 4.8, see the following bug reports:
and many more related ones.
While the Qt developers have tried to fix this regression (after all, QPainter was working back in Qt4.7), they couldn't properly fix it until today.
I assume QPainter is just a deprecated class, in favor of the new QML for mobile consumer devices. So Desktop application writers who use QPainter need to stick with Qt 4.7, as this QPainter bug makes all newer versions unusable.
That's a shame. I had some similar problems when I was on 4.7.4 but it got worse when I moved to 5.