Error messages from QPainter
-
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::save: Painter not active
QPainter::translate: Painter not active
QPainter::scale: Painter not active
QPainter::translate: Painter not active
QPainter::restore: Unbalanced save/restore
QPainter::end: Painter not active, abortedWhat are the most likely causes?
-
@Perdrix said in Error messages from QPainter:
What are the most likely causes?
Probably issues in your code which you did not post...
-
void DSSImageView::resizeEvent(QResizeEvent* e) { if (nullptr != pPixmap) { const QSize sz = e->size(); emit Image_resizeEvent(e); m_drawingPixmap = QPixmap(sz); const qreal pixWidth = pPixmap->width(); const qreal pixHeight = pPixmap->height(); const qreal hScale = static_cast<qreal>(sz.width()) / pixWidth; const qreal vScale = static_cast<qreal>(sz.height()) / pixHeight; m_scale = std::min(hScale, vScale); qreal xoffset = 0.0, yoffset = 0.0; if ((pixWidth * m_scale) < sz.width()) { xoffset = floor((sz.width() - (pixWidth * m_scale)) / 2.0); } if ((pixHeight * m_scale) < sz.height()) { yoffset = floor((sz.height() - (pixHeight * m_scale)) / 2.0); } m_origin = QPointF(xoffset, yoffset); displayRect.setTopLeft(m_origin); displayRect.setSize(QSizeF(pixWidth * m_scale, pixHeight * m_scale)); if (m_pToolBar) { qreal width(m_pToolBar->width()); qreal height(m_pToolBar->height()); QPoint point(sz.width() - width, sz.height() - height); m_pToolBar->move(point); } drawOnPixmap(); update(); } Inherited::resizeEvent(e); } void DSSImageView::drawOnPixmap() { ZFUNCTRACE_RUNTIME(); if (nullptr != pPixmap) { QPainter painter(&m_drawingPixmap); QPalette palette{ QGuiApplication::palette() }; QBrush brush{ palette.dark() }; painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.fillRect(rect(), brush); painter.save(); painter.translate(m_origin); painter.scale(m_zoom * m_scale, m_zoom * m_scale); painter.translate(-m_origin); // // Draw the rectangle of interest at the origin location // painter.drawPixmap(m_origin, *pPixmap, rectOfInterest); painter.restore(); // // Has the user enabled four corners mode? // if (m_fourCorners) { paintFourCorners(painter); } else if (nullptr != pOverlayPixmap) { // // Draw the overlay image in the same way that we drew the main image // painter.save(); painter.translate(m_origin); painter.scale(m_zoom * m_scale, m_zoom * m_scale); painter.translate(-m_origin); // // Finally draw the rectangle of interest at the origin location // painter.drawPixmap(m_origin, *pOverlayPixmap, rectOfInterest); painter.restore(); } painter.end(); } }
m_drawingPixmap is a class member that is created when the class is instantiated
-
@Perdrix said in Error messages from QPainter:
const QSize sz = e->size();
I would check to make sure that the size is not zero when this is called (i.e., that you are not trying to draw to a pixmap that has a size of (0,0)). Also, what is your
Image_resizeEvent
signal connected to? -
That makes sense - I will look into that.
The ImageView widget is part of a dialog/Ui object created by the source file below (specifically the object called "picture"):
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>StackingDlg</class> <widget class="QWidget" name="StackingDlg"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>711</width> <height>870</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>711</width> <height>870</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>16777215</height> </size> </property> <property name="windowTitle"> <string notr="true"/> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QSplitter" name="splitter"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <widget class="QWidget" name="layoutWidget"> <layout class="QVBoxLayout" name="verticalLayout_1"> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QLabel" name="information"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>275</width> <height>30</height> </size> </property> <property name="styleSheet"> <string notr="true">background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(138, 185, 242, 0), stop:1 rgba(138, 185, 242, 255))</string> </property> <property name="text"> <string notr="true"/> </property> </widget> </item> <item> <widget class="QToolButton" name="fourCorners"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>38</width> <height>38</height> </size> </property> <property name="maximumSize"> <size> <width>64</width> <height>64</height> </size> </property> <property name="styleSheet"> <string notr="true">border: none</string> </property> <property name="text"> <string notr="true">...</string> </property> <property name="icon"> <iconset resource="../resources.qrc"> <normaloff>:/stacking/4corners.png</normaloff>:/stacking/4corners.png</iconset> </property> <property name="iconSize"> <size> <width>38</width> <height>38</height> </size> </property> </widget> </item> <item> <widget class="QSlider" name="horizontalSlider"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>170</width> <height>0</height> </size> </property> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> </widget> </item> </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <widget class="DSSImageView" name="picture" native="true"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>300</width> <height>200</height> </size> </property> </widget> </item> </layout> </item> </layout> </widget> <widget class="QWidget" name="layoutWidget_2"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QLabel" name="listInfo"> <property name="minimumSize"> <size> <width>0</width> <height>25</height> </size> </property> <property name="styleSheet"> <string notr="true">background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(138, 185, 242, 0), stop:1 rgba(138, 185, 242, 255))</string> </property> <property name="text"> <string notr="true"/> </property> </widget> </item> <item row="1" column="0"> <widget class="QTableView" name="tableView"> <property name="dragDropMode"> <enum>QAbstractItemView::DropOnly</enum> </property> <property name="selectionMode"> <enum>QAbstractItemView::ExtendedSelection</enum> </property> <property name="selectionBehavior"> <enum>QAbstractItemView::SelectRows</enum> </property> <property name="sortingEnabled"> <bool>true</bool> </property> <attribute name="verticalHeaderVisible"> <bool>false</bool> </attribute> </widget> </item> <item row="2" column="0"> <widget class="QTabWidget" name="tabWidget"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Minimum"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>25</height> </size> </property> <property name="tabPosition"> <enum>QTabWidget::South</enum> </property> <property name="currentIndex"> <number>0</number> </property> <widget class="QWidget" name="tab"> <attribute name="title"> <string>Main Group</string> </attribute> </widget> </widget> </item> </layout> </widget> </widget> </item> </layout> </widget> <customwidgets> <customwidget> <class>DSSImageView</class> <extends>QWidget</extends> <header>dssimageview.h</header> <container>1</container> </customwidget> </customwidgets> <resources> <include location="../resources.qrc"/> </resources> <connections/> </ui>
that I would have expected to be autosized when the object is created in the ctor of StackingDlg:
StackingDlg::StackingDlg(QWidget* parent) : QDialog(parent), ui(new Ui::StackingDlg), initialised(false) { ui->setupUi(this);
but when the dlg was displayed, it appears that no resize event had ever been sent to the picture object (I thought that was always done after a setVisible(true);) which I issued not long after the ctor from StackingDlg. If I then manually resize the problem goes away.
-
Hi,
So long as the widget is not shown, it has no physical size hence the constructor is the wrong place to grab sizes of components.