[英]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
Ok, so here's what I want to accomplish: I want to draw QImage so the window would have scroll bars in case of the image turned out to be too big. 好的,这就是我要完成的工作:我想绘制QImage,以便在图像过大的情况下窗口具有滚动条。 For now one, I have sth like this:
现在,我有这样的事情:
#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();
}
However, it does not give me the effect that I want to have: 但是,它没有给我想要的效果:
When I change QPainter paint(this);
当我更换
QPainter paint(this);
to QPainter paint(scrollArea);
到
QPainter paint(scrollArea);
I have the error message (or, it's the warning I guess): QPainter::begin: Widget painting can only begin as a result of a paintEvent
but I'm able to run the application, and open / close images. 我收到错误消息(或者,我猜这是警告):
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
但是我能够运行该应用程序,并打开/关闭图像。 So, the program actually works with this, but it the error message bothers me and I would like to know how to get rid of it. 因此,该程序实际上可以与此一起使用,但是它的错误消息使我感到困扰,我想知道如何摆脱它。 With this only one changed line from the above src code app works and displays images as it should:
有了这个,上面的src代码应用程序中只有一条更改的行可以正常工作并显示图像:
Question is, where do you want to paint: on ImageWidget
, on imgDisplayLabel
, or on scrollArea
. 问题是,您想在哪里绘制:在
ImageWidget
, imgDisplayLabel
或scrollArea
。
If I interpret correctly, the warning is basically saying that, if you want to begin
a painter on a widget, you should do it in THE same widget's paint event. 如果我的解释正确,那么警告基本上是在说,如果您想在小部件上
begin
绘画,则应在同一小部件的paint事件中进行。
In Qt 4.8 documentation 在Qt 4.8文档中
QPainter::QPainter(QPaintDevice * device)
QPainter :: QPainter(QPaintDevice *设备)
Constructs a painter that begins painting the paint device immediately.
构造一个画家,该画家立即开始绘画绘画设备。
this means by calling the QPainter
constructor with a target device, it begin
s immediately. 这意味着通过使用目标设备调用
QPainter
构造函数,它将立即begin
。
So, try hijacking the scroll area's paint event. 因此,请尝试劫持滚动区域的绘画事件。
FYI, whenever you hijack a event in Qt, I recommend calling the base class's implementation first in you new implementation like this so the base class's behavior is preserved. 仅供参考,当你在Qt的劫持事件,我建议首先在您调用基类的实现新的实施像这样使基类的行为得以保留。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.