Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. "ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values" even though the arguments seem right
Qt 6.11 is out! See what's new in the release blog

"ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values" even though the arguments seem right

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 2.0k Views
  • 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.
  • O Offline
    O Offline
    Opara
    wrote on last edited by
    #1

    MRE:

    from PySide6 import QtWidgets as qtw
    from PySide6 import QtGui as qtg
    from PySide6 import QtCore as qtc
    
    
    class Delegate(qtw.QAbstractItemDelegate):
        def paint(self, painter, option, index):
            text = index.model().data(
                index, qtc.Qt.ItemDataRole.DisplayRole
            )
    
            text_rect = qtc.QRect(
                option.rect.left(),
                option.rect.top(),
                option.rect.width(),
                200
            )
    
            painter.drawText(text_rect, 0, text, text_rect)
    
        def sizeHint(self, option, index):
            return qtc.QSize(200, 200)
    
    
    class Model(qtc.QAbstractListModel):
        def __init__(self) -> None:
            super().__init__()
            self._texts = ["text" for _ in range(10)]
    
        def rowCount(self, _) -> int:
            return 10
    
        def data(self, index, role):
            if not index.isValid():
                return None
    
            if role == qtc.Qt.ItemDataRole.DisplayRole:
                return self._texts[index.row()]
            
            return None
    
    class MainWindow(qtw.QMainWindow):
        def __init__(self):
            super().__init__()
    
            delegate = Delegate(self)
            self._model = Model()
            self._view = qtw.QListView(self)
            self._view.setModel(self._model)
            self._view.setItemDelegate(delegate)
    
            self.setCentralWidget(self._view)
            self.show()
    
    
    app = qtw.QApplication()
    mw = MainWindow()
    app.exec()
    

    Error:

    ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values:
      PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect(230, 10, 0, 200), 0, '', PySide6.QtCore.QRect(230, 10, 0, 200))
    Found signature:
      PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect, int, str, PySide6.QtCore.QRect)
    

    One of the signatures I get on VSCode for painter.drawText:

    def drawText(r: PySide6.QtCore.QRect, flags: int, text: str, br: PySide6.QtCore.QRect) -> None
    

    The provided signatures and the passed arguments match, don't they? Could this be a bug in PySide6?
    The code causing the error is in the paint method in the Delegate class.

    JonBJ 1 Reply Last reply
    0
    • O Opara

      MRE:

      from PySide6 import QtWidgets as qtw
      from PySide6 import QtGui as qtg
      from PySide6 import QtCore as qtc
      
      
      class Delegate(qtw.QAbstractItemDelegate):
          def paint(self, painter, option, index):
              text = index.model().data(
                  index, qtc.Qt.ItemDataRole.DisplayRole
              )
      
              text_rect = qtc.QRect(
                  option.rect.left(),
                  option.rect.top(),
                  option.rect.width(),
                  200
              )
      
              painter.drawText(text_rect, 0, text, text_rect)
      
          def sizeHint(self, option, index):
              return qtc.QSize(200, 200)
      
      
      class Model(qtc.QAbstractListModel):
          def __init__(self) -> None:
              super().__init__()
              self._texts = ["text" for _ in range(10)]
      
          def rowCount(self, _) -> int:
              return 10
      
          def data(self, index, role):
              if not index.isValid():
                  return None
      
              if role == qtc.Qt.ItemDataRole.DisplayRole:
                  return self._texts[index.row()]
              
              return None
      
      class MainWindow(qtw.QMainWindow):
          def __init__(self):
              super().__init__()
      
              delegate = Delegate(self)
              self._model = Model()
              self._view = qtw.QListView(self)
              self._view.setModel(self._model)
              self._view.setItemDelegate(delegate)
      
              self.setCentralWidget(self._view)
              self.show()
      
      
      app = qtw.QApplication()
      mw = MainWindow()
      app.exec()
      

      Error:

      ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values:
        PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect(230, 10, 0, 200), 0, '', PySide6.QtCore.QRect(230, 10, 0, 200))
      Found signature:
        PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect, int, str, PySide6.QtCore.QRect)
      

      One of the signatures I get on VSCode for painter.drawText:

      def drawText(r: PySide6.QtCore.QRect, flags: int, text: str, br: PySide6.QtCore.QRect) -> None
      

      The provided signatures and the passed arguments match, don't they? Could this be a bug in PySide6?
      The code causing the error is in the paint method in the Delegate class.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Opara said in "ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values" even though the arguments seem right:

      PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect, int, str, PySide6.QtCore.QRect)

      I don't actually see this on page https://doc.qt.io/qtforpython/PySide6/QtGui/QPainter.html. [Actually I do find it, but it is mis-documented!] Does it make any difference if you remove the second QRect parameter?

      O 1 Reply Last reply
      0
      • JonBJ JonB

        @Opara said in "ValueError: 'PySide6.QtGui.QPainter.drawText' called with wrong argument values" even though the arguments seem right:

        PySide6.QtGui.QPainter.drawText(PySide6.QtCore.QRect, int, str, PySide6.QtCore.QRect)

        I don't actually see this on page https://doc.qt.io/qtforpython/PySide6/QtGui/QPainter.html. [Actually I do find it, but it is mis-documented!] Does it make any difference if you remove the second QRect parameter?

        O Offline
        O Offline
        Opara
        wrote on last edited by
        #3

        @JonB

        [Actually I do find it, but it is mis-documented!]

        Now that you mention it, the documentation for that drawText function does mention a boundingRect parameter but the function definition doesn't have it. Looks like the documentation might have just been copied from https://doc.qt.io/qt-6/qpainter.html#drawText-3 without any changes made, apart from the code.

        Does it make any difference if you remove the second QRect parameter?

        It works as one would expect with the second QRect removed.

        JonBJ 1 Reply Last reply
        1
        • O Opara

          @JonB

          [Actually I do find it, but it is mis-documented!]

          Now that you mention it, the documentation for that drawText function does mention a boundingRect parameter but the function definition doesn't have it. Looks like the documentation might have just been copied from https://doc.qt.io/qt-6/qpainter.html#drawText-3 without any changes made, apart from the code.

          Does it make any difference if you remove the second QRect parameter?

          It works as one would expect with the second QRect removed.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Opara
          Indeed, it looks like something strange might be going on for that overload. The C++ shows it as being optional. I do not know why your original Python code was rejected. You might want to report this as a bug.

          1 Reply Last reply
          1
          • CristianMaureiraC Offline
            CristianMaureiraC Offline
            CristianMaureira
            wrote on last edited by
            #5

            You can remove the last argument, and the code will work, but as Jon was mentioning, better to open a bug report. Can you please submit an issue into https://bugreports.qt.io/projects/PYSIDE with the same code? I think the problem lies in the signature selection, because we do apply some magic on top to use drawText with that signature and the optional argument:

                  <modify-function signature="drawText(QRect,int,QString,QRect*)">                                
                     <modify-argument index="4">                                                                   
                       <remove-argument/>                                                                          
                       <remove-default-expression/>                                                                
                     </modify-argument>                                                                            
                     <modify-argument index="return">                                                              
                       <replace-type modified-type="QRect"/>                                                       
                     </modify-argument>                                                                            
                     <inject-code class="target" position="beginning">                                             
                       <insert-template name="fix_args,QRect*"/>                                                   
                     </inject-code>                                                                                                                   
                   </modify-function> 
            
            1 Reply Last reply
            1

            • Login

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