qglwidget paint qimage resize fail

时间:2015-04-24 18:37:42

标签: c++ qt

I used the following code to display a QImage in QGLWidget:

class QGLCanvas : public QGLWidget
{
public:
    QGLCanvas(QWidget* parent = NULL);
    void setImage(const QImage& image);
protected:
    void paintEvent(QPaintEvent*);
private:
    QImage img;
};

QGLCanvas::QGLCanvas(QWidget* parent)
    : QGLWidget(parent)
{
}

void QGLCanvas::setImage(const QImage& image)
{
    img = image;
}

void QGLCanvas::paintEvent(QPaintEvent*)
{
    QPainter p(this);

    //Set the painter to use a smooth scaling algorithm.
    p.SetRenderHint(QPainter::SmoothPixmapTransform, 1);

    p.drawImage(this->rect(), img);
}

However when I resize the main window which cause the QGLWidget to resize. The actual painted QImage is not correctly resized. When the window shrink the painted QImage shrink more and when the window grows it also grows more. What's wrong with the above code?

0 个答案:

没有答案