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. Qt.ItemDataRole.ForegroundRole for QtCore.QAbstractTableModel is not working with PySide6.7.2 (PySide6.2.4 is OK)
Forum Updated to NodeBB v4.3 + New Features

Qt.ItemDataRole.ForegroundRole for QtCore.QAbstractTableModel is not working with PySide6.7.2 (PySide6.2.4 is OK)

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 405 Views 2 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.
  • L Offline
    L Offline
    little3pen
    wrote on last edited by
    #1

    I am trying customed table cells by QtWidgets.QTableView and QtCore.QAbstractTableModel.

    When I set Qt.ItemDataRole.ForegroundRole under venv pyside67 (pip install pyside6), the text in cells is all black(ignored ForegroundRole).

    Then I tried same script under venv pyside62 (pip install 'pyside6<6.3,>=6.2'), the text in cells is customed as I wish.

    Is there any probrem in PySide6.7.2?
    Or do I miss something for PySide6.7.2?

    OS:Microsoft Windows 11 Pro
    Python: 3.10.11

    script and venv infomation is below

    # c.f. https://www.pythonguis.com/tutorials/pyqt6-qtableview-modelviews-numpy-pandas/
    import sys
    from PySide6 import QtCore, QtGui, QtWidgets
    from PySide6.QtCore import Qt
    
    
    class TableModel(QtCore.QAbstractTableModel):
        def __init__(self, data):
            super(TableModel, self).__init__()
            self._data = data
    
        def data(self, index, role):
            if role == Qt.ItemDataRole.DisplayRole:
                return self._data[index.row()][index.column()]
            if role == Qt.ItemDataRole.ForegroundRole:
                if index.column()==2:
                    return QtGui.QBrush('white')
            if role == Qt.ItemDataRole.BackgroundRole:
                if index.column()==2:
                    return QtGui.QBrush('blue')
    
        def rowCount(self, index):
            return len(self._data)
    
        def columnCount(self, index):
            return len(self._data[0])
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.table = QtWidgets.QTableView()
            data = [
              [4, 9, 2],
              [1, 0, 0],
              [3, 5, 0],
              [3, 3, 2],
              [7, 8, 9],
            ]
            self.model = TableModel(data)
            self.table.setModel(self.model)
            self.setCentralWidget(self.table)
    
    
    app=QtWidgets.QApplication(sys.argv)
    window=MainWindow()
    window.show()
    app.exec_()
    

    venv pyside67 (THIS IS NOT WORKING)

    > pip list
    Package            Version
    ------------------ -------
    pip                24.1
    PySide6            6.7.2
    PySide6_Addons     6.7.2
    PySide6_Essentials 6.7.2
    setuptools         70.1.1
    shiboken6          6.7.2
    wheel              0.43.0
    > python.exe -V
    Python 3.10.11
    >
    

    venv pyside62 (THIS IS OK)

    > pip list
    Package    Version
    ---------- -------
    pip        24.1.2
    PySide6    6.2.4
    setuptools 70.3.0
    shiboken6  6.2.4
    wheel      0.43.0
    > python.exe -V
    Python 3.10.11
    >
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Try forcing the style to "windowsvista". The current Windows 11 style has some issues.

      In any case, your code works fine with the same version of PySide6 on macOS.

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

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Try forcing the style to "windowsvista". The current Windows 11 style has some issues.

        In any case, your code works fine with the same version of PySide6 on macOS.

        L Offline
        L Offline
        little3pen
        wrote on last edited by
        #3

        @SGaist , thanks a lot.

        app=QtWidgets.QApplication(sys.argv)
        app.setStyle('windowsvista')
        

        The revised code(above) is working venv67 as well.

        Some clients are expecting Win10 and Win11, so this is a big help.

        1 Reply Last reply
        0
        • L little3pen has marked this topic as solved on

        • Login

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