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);
    mb.setText(qs);
    mb.exec();
    } else {
    QMessageBox mb(this);
    QString qs("Loaded image " + fileName);
    mb.setText(qs);
    mb.exec();
    }
    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,

    Thanks



  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.