Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1628

Can not show QStyledItemDelegate correctly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Out of scope
    • Icon: Not Evaluated Not Evaluated
    • None
    • 6.0.1, 6.1.2
    • PySide
    • None
    • macOS Big Sur & windows 10
      PYTHON 3.9
    • macOS, Windows

      from PySide6 import QtCore, QtGui, QtWidgets
      from PySide6.QtWidgets import *
      import sys
      from PySide6.QtCore import Qt
      
      data = [("1", "Baharak", 10), ("2", "Darwaz", 60),
              ("3", "Fays abad", 20), ("4", "Ishkashim", 80),
              ("5", "Jurm", 100)]
      
      
      class ProgressDelegate(QtWidgets.QStyledItemDelegate):
      
          def paint(self, painter, option, index):
              if index.column() == 2:
                  progress = index.data(Qt.UserRole+1)
                  print(type(progress))
                  opt = QtWidgets.QStyleOptionProgressBar()
                  opt.state = QtWidgets.QStyle.State_Enabled
                  opt.direction = QApplication.layoutDirection()
                  opt.rect = option.rect
                  opt.minimum = 0
                  opt.maximum = 100
                  opt.textAlignment = Qt.AlignCenter
                  opt.progress = progress
                  opt.text = "{}%".format(progress)
                  opt.textVisible = True
                  QApplication.style().drawControl(QStyle.CE_ProgressBar, opt, painter)
      
      
      if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QTableView()
          w.setColumnWidth(1, 250)
          w.setColumnWidth(2, 250)
          w.setFixedSize(500, 400)
      
          model = QtGui.QStandardItemModel()
          model.setHorizontalHeaderLabels(["ID", "Name", "Progress"])
          r = 0
          for _id, _name, _progress in data:
              it_id = QtGui.QStandardItem(_id)
              it_name = QtGui.QStandardItem(_name)
              it_progress = QtGui.QStandardItem(str(_progress))
              it_progress.setData(_progress, Qt.UserRole+1)
              model.setItem(r, 0, QtGui.QStandardItem(it_id))
              model.setItem(r, 1, QtGui.QStandardItem(it_name))
              model.setItem(r, 2, QtGui.QStandardItem(it_progress))
              r += 1
          w.setModel(model)
          delegate = ProgressDelegate(w)
          w.setItemDelegateForColumn(2, delegate)
      
          # app.setStyle('Fusion')
      
          w.show()
          sys.exit(app.exec_())
      
      

      FYI:

      show correctly on pyqt5 with windows10

      show correctly on pyqt5 with macOS Big Sur (only fushion style)

      can not show correctly on pyside6 with neither window10 or macOS Big Sur

       

        1. PyQt5.png
          PyQt5.png
          171 kB
        2. pyside1628_stardelegate_repro.diff
          1 kB
        3. pyside1628.py
          3 kB
        4. PySide6.png
          PySide6.png
          262 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            crmaurei Cristian Maureira-Fredes
            kjlamuro Jianlong Ke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes