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. QTableView formatting depends on app style
Qt 6.11 is out! See what's new in the release blog

QTableView formatting depends on app style

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 532 Views 1 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.
  • G Offline
    G Offline
    George55978
    wrote on last edited by George55978
    #1

    Hi!
    I have an issue with this simple code:

    import sys
    from datetime import datetime
    
    from PyQt6 import QtCore, QtGui, QtWidgets
    from PyQt6.QtCore import Qt
    
    class TableModel(QtCore.QAbstractTableModel):
        def __init__(self, data):
            super().__init__()
            self._data = data
    
        def data(self, index, role):
            if role == Qt.ItemDataRole.ForegroundRole:
                value = self._data[index.row()][index.column()]
    
                if (
                    isinstance(value, int) or isinstance(value, float)
                ) and value < 0:
                    return QtGui.QColor("red")
    
            if role == Qt.ItemDataRole.DisplayRole:
                value = self._data[index.row()][index.column()]
    
                if isinstance(value, datetime):
                    return value.strftime("%Y-%m-%d")
    
                return value
    
        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, -1, "hello"],
                [3.023, 5, -5],
                [3, 3, datetime(2017, 10, 1)],
                [7.555, 8, 9],
            ]
    
            self.model = TableModel(data)
            self.table.setModel(self.model)
    
            self.setCentralWidget(self.table)
            self.setGeometry(600, 100, 400, 200)
    
    app = QtWidgets.QApplication(sys.argv)
    # app.setStyle("Fusion")
    window = MainWindow()
    window.show()
    app.exec()
    

    It formats the QTableView somehow. But it doesn't do it with the foreground color. For the default application style, it seems to just ignore the foreground formatting (Figure 1).
    1.png
    Fig.1
    For the fusion style, things are better. (Just uncomment app.setStyle("Fusion"), Figure 2):
    2.png
    Fig.2

    This code worked fine until recently but from some point it broke.
    Any ideas what might have gone wrong?

    Package Version


    pip 24.2
    PyQt6 6.7.1
    PyQt6-Qt6 6.7.2
    PyQt6_sip 13.8.0

    Processor 11th Gen Intel(R) Core(TM) i3-1115G4 @ 3.00GHz 3.00 GHz
    Installed RAM 8,00 GB (7,75 GB usable)
    System type 64-bit operating system, x64-based processor

    Edition Windows 11 Pro
    Version 23H2
    OS build 22631.4037

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

      Hi,

      There's a new style for Windows11 that seems to have some issue. You can also try the windowsvista style.

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

      G 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        There's a new style for Windows11 that seems to have some issue. You can also try the windowsvista style.

        G Offline
        G Offline
        George55978
        wrote on last edited by
        #3

        @SGaist said in QTableView formatting depends on app style:

        Hi,

        There's a new style for Windows11 that seems to have some issue. You can also try the windowsvista style.

        Yes, you're right, thanks.
        I kept looking for and found that: [https://bugreports.qt.io/browse/QTBUG-126543](link url) so it is definetely a bug.

        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