Important: Please read the Qt Code of Conduct -

Problem drawing a QPixmap on screen with a QPainter

  • So my code basically looks like this (where the code is placed in the constructor of a class that inherits from QWidget)

    QPainter p(this);
    QPixmap temp;
    QString fileName = "folder/art.png";
    if (temp.load(fileName) == false)
    QMessageBox mb(this);
    QString qs("Failed to load " + fileName);
    } else {
    QMessageBox mb(this);
    QString qs("Loaded image " + fileName);
    p.drawPixmap(200, 200, temp);

    The image loaded fine but won't draw on the screen. Have also tried using the repaint() method after the call to p.drawPixmap(...);

    Any help would be much appreciated,


  • To draw on a widget with your own code, you must reimplement "QWidget::paintEvent() ":/doc/qt-4.8/qwidget.html#paintEvent. You cannot draw in the constructor of a widget.

  • Thanks for the quick response. This is indeed my problem here.

Log in to reply