【问题标题】:How can i set labels of QHeaderView in PyQt?如何在 PyQt 中设置 QHeaderView 的标签?
【发布时间】:2011-12-03 13:48:17
【问题描述】:

在 Pyqt 中,我试图让 QTableWidget 的 QHeaderView 响应鼠标右键单击。 我继承了 QHeaderView 并且重载了 mousePressEvent。

然后我可以将它设置为我的自定义 QTableWidget 的标题,即 DataTable 类。但是我不明白如何设置标题的标签。

感谢您的帮助!

这是一些代码。

class Header( QtGui.QHeaderView ):
    def __init__ ( self, parent ):
        QtGui.QHeaderView.__init__( self, QtCore.Qt.Vertical, parent=parent )

    def mousePressEvent( self, event ):
        if event.button() == QtCore.Qt.RightButton:
            do_stuff()


class DataTable( QtGui.QTableWidget ):
    def __init__ ( self ):
        QtGui.QTableWidget.__init__( self )
        self.setShowGrid(True)

        self.header = Header( parent = self )
        self.header.setClickable(True)
        self.setHorizontalHeader( self.header )

    def set_header( self, labels ):
        ???

【问题讨论】:

    标签: python pyqt qtablewidget qheaderview


    【解决方案1】:

    这个示例代码应该很有用:

    import sys
    import string
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    class Header(QHeaderView):
        def __init__(self, parent=None):
            super(Header, self).__init__(Qt.Horizontal, parent)
    
            self.setContextMenuPolicy(Qt.CustomContextMenu)
            self.customContextMenuRequested.connect(self.ctxMenu)
            self.hello = QAction("Hello", self)
            self.hello.triggered.connect(self.printHello)
            self.currentSection = None
    
        def printHello(self):
            data = self.model().headerData(self.currentSection, Qt.Horizontal)
            print data.toString()
    
        def ctxMenu(self, point):
            menu = QMenu(self)
            self.currentSection = self.logicalIndexAt(point)
            menu.addAction(self.hello)
            menu.exec_(self.mapToGlobal(point))
    
    
    class Table(QTableWidget):
        def __init__(self, parent=None):
            super(Table, self).__init__(parent)
            self.setHorizontalHeader(Header(self))
            self.setColumnCount(3)
            self.setHorizontalHeaderLabels(['id', 'name', 'username'])
            self.populate()
    
        def populate(self):
            self.setRowCount(10)
            for i in range(10):
                for j,l in enumerate(string.letters[:3]):
                    self.setItem(i, j, QTableWidgetItem(l)) 
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        t = Table()
        t.show()
        app.exec_()
        sys.exit()
    

    【讨论】:

      【解决方案2】:

      简答:

      QTableWidget 有一个名为“setHorizo​​ntalHeaderLabels”的函数,只需将标题提供给它即可。像这样:

      your_table_widget.setHorizontalHeaderLabels(["name", "phone number", "email"])
      

      我会将此作为评论添加到 pedrotech,但我没有足够的代表用于 cmets。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 2017-09-06
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        相关资源
        最近更新 更多