![](/img/trans.png)
[英]QPainter::begin: Widget painting can only begin as a result of a paintEvent
[英]How to draw QImage on QScrollArea? Did this, but have some minor problems QPainter::begin: Widget painting can only begin as a result of a paintEvent
好的,這就是我要完成的工作:我想繪制QImage,以便在圖像過大的情況下窗口具有滾動條。 現在,我有這樣的事情:
#include "imagewidget.h"
#include <QImage>
#include <QPainter>
#include <QGridLayout>
#include <QLabel>
ImageWidget::ImageWidget(QWidget* parent)
: QWidget(parent)
{
m_image = QImage();
scrollArea = new QScrollArea(this);
QGridLayout *gridLayout = new QGridLayout(this);
imgDisplayLabel = new QLabel(this);
imgDisplayLabel->setPixmap(QPixmap::fromImage(m_image));
imgDisplayLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imgDisplayLabel->setScaledContents(true);
imgDisplayLabel->adjustSize();
scrollArea->setWidget(imgDisplayLabel);
gridLayout->addWidget(scrollArea,0,0);
setLayout(gridLayout);
}
void ImageWidget::paintEvent(QPaintEvent* event)
{
QPainter paint(this);
if(!m_image.isNull())
paint.drawImage(0,0, m_image);
imgDisplayLabel->setPixmap(QPixmap::fromImage(m_image));
imgDisplayLabel->adjustSize();
imgDisplayLabel->setScaledContents(true);
}
void ImageWidget::setImage(QImage im)
{
m_image = im;
update();
}
void ImageWidget::removeImage()
{
m_image = QImage();
update();
}
但是,它沒有給我想要的效果:
當我更換QPainter paint(this);
到QPainter paint(scrollArea);
我收到錯誤消息(或者,我猜這是警告): QPainter::begin: Widget painting can only begin as a result of a paintEvent
而QPainter::begin: Widget painting can only begin as a result of a paintEvent
但是我能夠運行該應用程序,並打開/關閉圖像。 因此,該程序實際上可以與此一起使用,但是它的錯誤消息使我感到困擾,我想知道如何擺脫它。 有了這個,上面的src代碼應用程序中只有一條更改的行可以正常工作並顯示圖像:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.