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,这样在图像太大的情况下窗口会有滚动条。首先,我有这样的东西:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #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(); } |
但是,它并没有给我想要的效果:
当我把
型
问题是,你想在哪里画画:在
如果我解释正确,警告基本上是说,如果你想让一个小部件上的油漆工来油漆,你应该在同一个小部件的油漆事件中这样做。
在Qt 4.8文件中
QPainter::QPainter(QPaintDevice * device)
Constructs a painter that begins painting the paint device
immediately.
号
这意味着,通过使用目标设备调用
所以,尝试劫持滚动区域的绘画事件。
仅供参考,每当您劫持qt中的事件时,我建议在新实现中首先调用基类的实现,这样可以保留基类的行为。