PyQt QtableView标头。如何添加右上角的小按钮来隐藏/显示带有选中/未选中复选框的列?
对于PyQt4,我使用的是一个包含10多列的QtableView。用户必须可以选择显示/隐藏列 这通常是通过在表格标题的右上角添加一个小按钮来完成的。该按钮显示带有选中/未选中复选框的菜单,允许隐藏/显示列 这是我们的一个例子。 因此,我想知道如何使用PyQt的QtableView实现同样的功能PyQt QtableView标头。如何添加右上角的小按钮来隐藏/显示带有选中/未选中复选框的列?,qt,checkbox,pyqt4,qtableview,Qt,Checkbox,Pyqt4,Qtableview,对于PyQt4,我使用的是一个包含10多列的QtableView。用户必须可以选择显示/隐藏列 这通常是通过在表格标题的右上角添加一个小按钮来完成的。该按钮显示带有选中/未选中复选框的菜单,允许隐藏/显示列 这是我们的一个例子。 因此,我想知道如何使用PyQt的QtableView实现同样的功能 谢谢,在QTableView中没有类似“Sqlite管理器表”的按钮。但您可以通过使用自定义小部件,并与之一起从用户处获取列。并使用&隐藏显示您的列 充分的例子 import sys import ra
谢谢,在
QTableView
中没有类似“Sqlite管理器表”的按钮。但您可以通过使用自定义小部件,并与之一起从用户处获取列。并使用&隐藏显示您的列
充分的例子
import sys
import random
from functools import partial
from PyQt4 import QtGui
class QCustomTableViewWidget (QtGui.QWidget):
def __init__ (self, myQStandardItemModel, *args, **kwargs):
super(QCustomTableViewWidget, self).__init__(*args, **kwargs)
# Layout setup
self.localQTableView = QtGui.QTableView()
self.rightCornerQPushButton = QtGui.QPushButton()
menuQHBoxLayout = QtGui.QHBoxLayout()
menuQHBoxLayout.addStretch(1)
menuQHBoxLayout.addWidget(self.rightCornerQPushButton)
allQVBoxLayout = QtGui.QVBoxLayout()
allQVBoxLayout.addLayout(menuQHBoxLayout)
allQVBoxLayout.addWidget(self.localQTableView)
self.setLayout(allQVBoxLayout)
# Object setup
self.localQTableView.setModel(myQStandardItemModel)
self.rightCornerQPushButton.setText('Show column')
currentQMenu = QtGui.QMenu()
for column in range(myQStandardItemModel.columnCount()):
currentQAction = QtGui.QAction('Column %d' % (column + 1), currentQMenu)
currentQAction.setCheckable(True)
currentQAction.setChecked(True)
currentQAction.toggled.connect(partial(self.setColumnVisible, column))
currentQMenu.addAction(currentQAction)
self.rightCornerQPushButton.setMenu(currentQMenu)
def setColumnVisible (self, column, isChecked):
if isChecked:
self.localQTableView.showColumn(column)
else:
self.localQTableView.hideColumn(column)
def tableView (self):
return self.localQTableView
# Simulate data
myQStandardItemModel = QtGui.QStandardItemModel()
for _ in range(10):
myQStandardItemModel.appendRow([QtGui.QStandardItem('%d' % random.randint(100, 999)), QtGui.QStandardItem('%d' % random.randint(100, 999)), QtGui.QStandardItem('%d' % random.randint(100, 999))])
# Main application
myQApplication = QtGui.QApplication(sys.argv)
myQCustomTableViewWidget = QCustomTableViewWidget(myQStandardItemModel)
myQCustomTableViewWidget.show()
sys.exit(myQApplication.exec_())
谢谢Kitsune Meyoko,这是个好主意……) 我发现了另一个与您非常相似的解决方案,它使用QMenu和可检查的QActions,而不是QPushButton:Let's Go:
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.setup()
@pyqtSlot(bool)
def printID(self, i):
print("id")
if i == False:
self.hideSection(0)
else:
self.showSection(0)
@pyqtSlot(bool)
def printNAME(self, i):
print("name")
if i == False:
self.hideSection(1)
else:
self.showSection(1)
@pyqtSlot(bool)
def printUSERNAME(self, i):
print("username")
if i == False:
self.hideSection(2)
else:
self.showSection(2)
def setup(self):
self.id = QAction("id",self)
self.id.setCheckable(True)
self.id.setChecked(True)
self.connect(self.id, SIGNAL("triggered(bool)"), self, SLOT("printID(bool)"))
self.name = QAction("name",self)
self.name.setCheckable(True)
self.name.setChecked(True)
self.connect(self.name, SIGNAL("triggered(bool)"), self, SLOT("printNAME(bool)"))
self.username = QAction("username",self)
self.username.setCheckable(True)
self.username.setChecked(True)
self.connect(self.username, SIGNAL("triggered(bool)"), self, SLOT("printUSERNAME(bool)"))
def ctxMenu(self, point):
menu = QMenu(self)
self.currentSection = self.logicalIndexAt(point)
menu.addAction(self.id)
menu.addAction(self.name)
menu.addAction(self.username)
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.ascii_letters[:3]):
self.setItem(i, j, QTableWidgetItem(l))
if __name__ == '__main__':
app = QApplication(sys.argv)
t = Table()
t.show()
app.exec_()
sys.exit()
听起来不错。阿糖胞苷。。。。。如果我全部隐藏,它将无法再次右键单击。它应该在
QTableWidget
中处理超过?或者按钮处理起来好吗?嗯,是的,我认为用户没有兴趣隐藏所有部分。因此,当只有一个部分保持可见时,可以禁用隐藏部分!是的,隐藏所有部分应该是没有意义的。但它可以设置隐藏所有的可能性。如果用户意外地隐藏了所有内容,并希望再次显示,则无法恢复。所以,用户可以认为它是“BUG”,不是吗?