有没有办法确定qspinbox中箭头按钮的宽度? 我正在尝试覆盖上下文菜单事件,我只希望在用户右键单击箭头按钮时发生自定义事件,否则我希望显示正常的上下文菜单。你知道吗
现在我只是硬编码一个值20,这是不理想的。你知道吗
import sys
import os
from PySide import QtGui, QtCore
class MySpinner(QtGui.QSpinBox):
def __init__(self, parent=None):
super(MySpinner, self).__init__(parent)
self.setAccelerated(False)
self.setRange(-1000,1000)
self.setSingleStep(1)
self.setValue(300)
def contextMenuEvent(self, event):
if event.pos().x() > self.rect().right()-20:
self.setValue(50)
self.selectAll()
else:
super(self.__class__, self).contextMenuEvent(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(300, 200)
grid = QtGui.QVBoxLayout()
grid.addWidget(MySpinner())
content = QtGui.QWidget()
content.setLayout(grid)
self.setCentralWidget(content)
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
无需获取宽度,只需获取子控件即可知道是否按了其中一个箭头按钮:
如果您想得到每个子控件的QRect,您应该使用
另一种选择:
相关问题 更多 >
编程相关推荐