Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font role not taken into account for table header #315

Open
mgrojo opened this issue Jun 15, 2022 · 1 comment
Open

Font role not taken into account for table header #315

mgrojo opened this issue Jun 15, 2022 · 1 comment

Comments

@mgrojo
Copy link
Contributor

mgrojo commented Jun 15, 2022

Describe Your Environment

[Versions from your environment]

  • QDarkStyle: tested with 3.0 and 3.1 6ff5fdf
  • OS: Ubuntu Linux 20.04

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):
sqlitebrowser/sqlitebrowser@b6c5024

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
Copy link

RiekeJ commented Feb 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants