Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QHeaderView paintSection Problem
Forum Updated to NodeBB v4.3 + New Features

QHeaderView paintSection Problem

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.7k 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.
  • I Offline
    I Offline
    iceboxx89
    wrote on last edited by
    #1

    Hi Forum,

    Im trying to change the background color of the header im using for my tableview, i am subclassing QHeaderView because the bg-color for the header will vary based on certain parameters. below is the code:

    @
    from PyQt4 import QtCore, QtGui

    class CustomHeader(QtGui.QHeaderView):

    def __init__(self, orientation, parent):
        super(CustomHeader, self).__init__(orientation, parent)
    
    def paintSection(self, painter, rect, section):
        painter.save()
        option = QtGui.QStyleOptionHeader()
        option.textAlignment = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter
        option.text = "Date %d"%section
        option.rect = rect
        p = option.palette
        p.setBrush(p.Button, QtCore.Qt.red)
        self.style().drawControl(QtGui.QStyle.CE_Header, option, painter)
        painter.restore()
    

    class CustomController(QtCore.QAbstractTableModel):

    def __init__(self, parent=None):
        super(CustomController, self).__init__(parent)
    
    def rowCount(self, parent=QtCore.QModelIndex()):
        return 10
    
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 10
    
    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            if index.column() == 0:
                return "Artist xx"
            else:
                return None
        if role == QtCore.Qt.TextAlignmentRole:
            return QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter
        return None
    

    if name == "main":
    app = QtGui.QApplication([])
    #app.setStyle("Plastique")
    view = QtGui.QTableView()
    view.horizontalHeader().setHidden(True)
    view.verticalHeader().setHidden(True)
    c_header = CustomHeader(QtCore.Qt.Horizontal, view)
    view.setHorizontalHeader(c_header)
    model = CustomController(view)
    view.setModel(model)
    view.show()
    view.raise_()
    app.exec_()
    @

    the issue is this works only if the application's style is set to something other than native.
    i'm on a mac book pro using Qt version 4.8.5. is this a bug which i've already reported at
    https://bugreports.qt-project.org/browse/QTBUG-31804 if yes is there a work around for this issue.?

    1 Reply Last reply
    0

    • Login

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