如何在 PyQt 中设置 QHeaderView 的标签?

发布于 2024-12-08 22:55:12 字数 817 浏览 5 评论 0原文

在 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 ):
        ???

In Pyqt, I am trying to make the QHeaderView of a QTableWidget respond to right mouse clicks.
I have subclassed QHeaderView and i have overloaded the mousePressEvent.

Then i can set it as as the header of my custom QTableWidget, the DataTable class. However i don't understand how to set the labels of the header.

Thanks for helping!

Here is some code.

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 ):
        ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-12-15 22:55:12

这个示例代码应该有用:

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()

This sample code should be useful:

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()
夜无邪 2024-12-15 22:55:12

简短的回答:

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

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

我会将此添加为对 pedrotech 的评论,但我没有足够的代表来发表评论。

Short answer:

The QTableWidget has a function called "setHorizontalHeaderLabels", simply supply your headers to it. Like so:

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

I would have added this as a comment to pedrotech, but I don't have enough rep for comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文