右键单击 QTreeView 内的 QHeaderView
我编写了具有多列的 QTreeView 的后代。我想创建一个弹出菜单,当用户右键单击列标题时显示该菜单。我已尝试为此捕获来自 QTreeView 的信号,但 QTreeView 似乎没有在标题上发出信号。 QTreeView.header() 确实如此。因此,我相信我必须:
1:将 QHeaderView 的信号之一连接到弹出函数 - 我一直无法找到单击右键时触发的信号 - 我尝试过sectionClicked,sectionHandleDoubleClicked,sectionDoubleClicked,sectionPressed(双击功能没有捕获并不奇怪单击右键 - 但他们确实捕获了双击右键)
self.header().sectionClicked.connect(self.headerMenu)
self.header().sectionHandleDoubleClicked.connect(self.headerMenu)
self.header().sectionDoubleClicked.connect(self.headerMenu)
self.header().sectionPressed.connect(self.headerMenu)
或者,
2:用我自己的 MousePressEvent 函数编写 QHeaderView 的后代,并将其用于我的标题。到目前为止,我尚未成功将新标头类连接到 QTreeView 后代。我在运行时不断收到分段错误,没有更多解释。
#in DiceView's init, where DiceHeaders is the QHeaderView descendant
self.setHeader(DiceHeaders())
有什么想法吗?
I've written a descendant of QTreeView with multiple columns. I want to create a popup menu that appears whe nthe user right-clicks over the column headers. I have tried catching signals from QTreeView for this, but QTreeView doesn't seem to emit signals on the headers. QTreeView.header() does. I therefore believe I must either:
1: connect one of QHeaderView's signals to a popup function - I have been unable to find a signal that is triggered on a single right click - I have tried sectionClicked, sectionHandleDoubleClicked, sectionDoubleClicked, sectionPressed (not surprised the double click functions didn't catch a single right click - but they do catch a double right click)
self.header().sectionClicked.connect(self.headerMenu)
self.header().sectionHandleDoubleClicked.connect(self.headerMenu)
self.header().sectionDoubleClicked.connect(self.headerMenu)
self.header().sectionPressed.connect(self.headerMenu)
or,
2: write a descendant of QHeaderView with my own MousePressEvent function, and use that for my headers. I have so far been unsuccessful in connecting the new header class to the QTreeView descendant. I keep getting a Segmentation Fault on runtime, with no more explanation.
#in DiceView's init, where DiceHeaders is the QHeaderView descendant
self.setHeader(DiceHeaders())
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了 setContextMenuPolicy 函数:
然后,在 headerMenu 中:
I discovered the setContextMenuPolicy function:
Then, in headerMenu:
我会选择解决方案n°2:编写自己的类继承QHeaderView。
您的分段错误可能来自 python/pyqt 故障?
您应该通过保留对 DiceHeaders 对象的引用来确保它存在。
I'd go for solution n°2 : Write your own class inheriting QHeaderView.
Your segmentation fault may be coming from a python/pyqt glitch ?
You should make sure that you DiceHeaders object exists by keeping a reference to it.