ColinDuquesnoy / QDarkStyleSheet

A dark style sheet for QtWidgets application
Other
2.84k stars 730 forks source link

Font role not taken into account for table header #315

Open mgrojo opened 2 years ago

mgrojo commented 2 years ago

Describe Your Environment

[Versions from your environment]

Language

C++

Description / Steps to Reproduce [if necessary]

I made these changes to sqlitebrowser to get column headers with some font decorations (underlined and cursive): https://github.com/sqlitebrowser/sqlitebrowser/commit/b6c5024de0cc3ff8dd275a3586757b6a37a2c028

The change works as expected when using the standard desktop style.

Actual Result

No noticable change, every column header has a regular font.

Expected Results / Proposed Result

Primary Key columns are underlined, foreign key columns are cursive.

Shouldn't work the style sheet as the default style?

RiekeJ commented 1 year ago

Describe Your Environment

QDarkStyle: 3.0.2 and also 3.1 from PyPi

OS: Windows 10 22H2 Language Python 3.9.12 PyQt5 5.15.7

Description / Steps to Reproduce [if necessary] Vertical and horizontal header font and font size in QTableView are not adjusted if QDarkStyle is used . Changes are as expected if standard style is used.

minimum working example:

# inspired by https://www.pythonguis.com/tutorials/qtableview-modelviews-numpy-pandas/
import sys
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
import qdarkstyle

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data, horizontal_headers,**kwargs):
        super(TableModel, self).__init__()
        self._data = data
        self._horizontal_headers = horizontal_headers

        if 'font' in kwargs.keys():            
            self._font = QtGui.QFont(kwargs['font'])
        else:
            self._font = QtGui.QFont()  

        if 'fontsize' in kwargs.keys():
            self._font.setPixelSize(kwargs['fontsize'])
        else:
            self._font.setPixelSize(12)

    def data(self, index, role) -> str:
        """method for visualization of the data model"""        
        # display
        if role == Qt.DisplayRole:
            datum = self._data[index.row(),index.column()]            
            return str(datum)
        # set fonts
        if role == Qt.FontRole:
            return self._font              

    def rowCount(self, index):
        return self._data.shape[0]

    def columnCount(self, index):
        return self._data.shape[1]        

    def headerData(self, section, orientation, role):        
        # section is the index of the column/row.
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return str(self._horizontal_headers[section])

            if orientation == Qt.Vertical:                
                return str(section)

        # set fonts for headers
        if role == Qt.FontRole:
            return self._font        

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.table = QtWidgets.QTableView()
        data = np.array([
          [4, 9, 2],
          [1, 0, 0],
          [3, 5, 0],
          [3, 3, 2],
          [7, 8, 9],
        ],dtype=int)
        self.model = TableModel(data,['A','B','C'],font='Arial', fontsize=9)
        self.table.setModel(self.model)
        self.setCentralWidget(self.table)

if __name__ == "__main__":
    app=QtWidgets.QApplication(sys.argv)
    dark_stylesheet = qdarkstyle.load_stylesheet_pyqt5()
    app.setStyleSheet(dark_stylesheet)    
    window=MainWindow()
    window.show()
    app.exec_()

Actual Results Header do not change by request.

Expected Results / Proposed Result Headers should change font and font size as well as table data.

Commenting out

   dark_stylesheet = qdarkstyle.load_stylesheet_pyqt5()
   app.setStyleSheet(dark_stylesheet)

and using standard style works as intended.