PySide/PyQt 中 QTableWidget 的 QMenu
这是我的问题详细信息:我有这些小部件 - QMenuBar、QTableWidget 和 QToolbar。这是我的代码示例:
import sys
from PySide import QtGui
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.header_lbls = ['Name', 'Second Name', 'Surname', 'Birth Date', 'Phone Number', 'Skype', 'E-mail']
self.table = QtGui.QTableWidget(10, 7)
self.table.setHorizontalHeaderLabels(self.header_lbls)
self.setCentralWidget(self.table)
#ACTIONS
self.createActions()
#MENUBAR
self.createMenus()
#TOOLBAR
self.createToolbar()
#STATUSBAR
self.creatStatusbar()
def contextMenuEvent(self, event):
self.menu = QtGui.QMenu(self.table)
self.menu.addAction(self.aboutAct)
self.menu.exec_(QtGui.QCursor.pos())
def createActions(self):
self.exitAct = QtGui.QAction('E&xit', self, shortcut='Ctrl+Q',
statusTip='Exit the application', triggered=app.exit)
def createMenus(self):
self.menubar = self.menuBar()
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.exitAct)
def createToolbar(self):
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.addAction(self.settingsAct)
self.toolbar.addSeparator()
self.toolbar.addAction(self.exitAct)
def creatStatusbar(self):
self.statusBar()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Example()
window.setGeometry(80, 80, 800, 600)
window.show()
sys.exit(app.exec_())
问题是:如何使 QMenu 像 Microsoft Excel 中一样(我的意思是仅添加/删除行/列)。提前致谢。
here is my question details: I have these widgets - QMenuBar, QTableWidget and QToolbar. Here is my code sample:
import sys
from PySide import QtGui
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.header_lbls = ['Name', 'Second Name', 'Surname', 'Birth Date', 'Phone Number', 'Skype', 'E-mail']
self.table = QtGui.QTableWidget(10, 7)
self.table.setHorizontalHeaderLabels(self.header_lbls)
self.setCentralWidget(self.table)
#ACTIONS
self.createActions()
#MENUBAR
self.createMenus()
#TOOLBAR
self.createToolbar()
#STATUSBAR
self.creatStatusbar()
def contextMenuEvent(self, event):
self.menu = QtGui.QMenu(self.table)
self.menu.addAction(self.aboutAct)
self.menu.exec_(QtGui.QCursor.pos())
def createActions(self):
self.exitAct = QtGui.QAction('E&xit', self, shortcut='Ctrl+Q',
statusTip='Exit the application', triggered=app.exit)
def createMenus(self):
self.menubar = self.menuBar()
self.fileMenu = self.menuBar().addMenu("&File")
self.fileMenu.addAction(self.exitAct)
def createToolbar(self):
self.toolbar = self.addToolBar('Toolbar')
self.toolbar.addAction(self.settingsAct)
self.toolbar.addSeparator()
self.toolbar.addAction(self.exitAct)
def creatStatusbar(self):
self.statusBar()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Example()
window.setGeometry(80, 80, 800, 600)
window.show()
sys.exit(app.exec_())
The question is: How make QMenu like in Microsoft Excel for example (I mean only add/delete rows/coloumns). Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用表标题视图的 customContextMenuRequested 信号:
Use the customContextMenuRequested signal of the table's header-views: