c++ - 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, here's want accomplish: want draw qimage window have scroll bars in case of image turned out big. one, have sth 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, not give me effect want have:

enter image description here

when change qpainter paint(this); qpainter paint(scrollarea); have error message (or, it's warning guess): qpainter::begin: widget painting can begin result of paintevent i'm able run application, , open / close images. so, program works this, error message bothers me , know how rid of it. 1 changed line above src code app works , displays images should:

enter image description here

question is, want paint: on imagewidget, on imgdisplaylabel, or on scrollarea.

if interpret correctly, warning saying that, if want begin painter on widget, should in the same widget's paint event.

in qt 4.8 documentation

qpainter::qpainter(qpaintdevice * device)

constructs painter begins painting paint device immediately.

this means calling qpainter constructor target device, begins immediately.

so, try hijacking scroll area's paint event.

fyi, whenever hijack event in qt, recommend calling base class's implementation first in new implementation this base class's behavior preserved.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -