Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error messages from QPainter
Qt 6.11 is out! See what's new in the release blog

Error messages from QPainter

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    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, aborted

    What are the most likely causes?

    jsulmJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      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, aborted

      What are the most likely causes?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Perdrix said in Error messages from QPainter:

      What are the most likely causes?

      Probably issues in your code which you did not post...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3
        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by mchinand
          #4

          @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?

          1 Reply Last reply
          0
          • PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by Perdrix
            #5

            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.

            1 Reply Last reply
            0
            • PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by
              #6

              If I check the size of the picture object in its ctor I get a size of 100x30 which is very small - why wasn't it resized by the layout?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by Perdrix
                  #8

                  OK, so I thought about it and put that stuff into the resizeEvent handler (with a bit of code tweaking) and that seems to work very well indeed.

                  Thank you for that.
                  D.

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved