我正在对qspinbox进行子类化,并尝试获取子控件的上/下箭头矩形大小,如这里用python和pyside演示的那样。但是,当试图将代码转换为C++时,我会出错。
下面是Python的例子,我想把它转换成Qt/c++
import sys
import os
from PySide import QtGui, QtCore
class SuperSpinner(QtGui.QSpinBox):
def __init__(self, parent=None):
super(SuperSpinner, self).__init__(parent)
def contextMenuEvent(self, event):
opt = QtGui.QStyleOptionSpinBox()
self.initStyleOption(opt)
r = QtCore.QRect()
for sc in (QtGui.QStyle.SC_SpinBoxUp, QtGui.QStyle.SC_SpinBoxDown):
r= r.united(self.style().subControlRect(QtGui.QStyle.CC_SpinBox, opt, sc, self))
if r.contains(event.pos()):
self.setValue(0)
self.selectAll()
else:
super(self.__class__, self).contextMenuEvent(event)
def main():
app = QtGui.QApplication(sys.argv)
ex = SuperSpinner()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
尝试编译时qt中的错误消息
cpp文件的主要位:
#include "intsuperspinbox.h"
#include <QMouseEvent>
#include <QStyleOptionSpinBox>
#include <QCommonStyle>
#include <QStyle>
#include <QDebug>
IntSuperSpinBox::IntSuperSpinBox(QWidget *parent) :
QSpinBox(parent)
{
setMouseTracking(true);
}
void IntSuperSpinBox::contextMenuEvent(QContextMenuEvent *event)
{
QStyleOptionSpinBox opt;
opt.initFrom(this);
// ERRORS HERE
QRect upRect(this->style()->subControlRect(QStyle::CC_SpinBox, opt, QStyle::SC_SpinBoxUp, this));
QSpinBox::contextMenuEvent(event);
}