I am trying to draw line.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QPainter painter(&w);
painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
painter.drawLine(0, 0, 200, 200);
return a.exec();
}
But there is nothing painting on the window. What is wrong?
You can not paint outside of the paintEvent()
function, at least on Windows and Mac OS. However you can override your MainWindow
class' paintEvent()
function to draw the line there. For example:
class Widget : public QWidget
{
protected:
void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
painter.drawLine(0, 0, 200, 200);
}
};
And the usage:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
[..]