我想在Qt中的QPixmap上得到鼠标按下事件。我试图使用以下方法对其进行子类:
class CustomPixmap : public QPixmap
{
Q_OBJECT
public:
CustomPixmap(QPaintDevice *parent = NULL);
~CustomPixmap() {};
protected:
void mousePressEvent(QMouseEvent *event);
};但是由于错误,它没有编译。
./moc_output/moc_customPixmap.cpp:52:8: error: no member named
'staticMetaObject' in 'QPixmap'; did you mean simply 'staticMetaObject'?将Q_OBJECT取出来可以编译,但不调用mousePressEvent。如何正确地子类QPixmap以获取鼠标按下事件?
发布于 2015-11-19 20:23:11
最后,我使用了一个QPushButton:
QPushButton *button = new QPushButton;
button->setIcon(QIcon(myQPixmap));
buttonWidget=MySceneClass->scene()->addWidget(button);
QObject::connect(button, SIGNAL(clicked()),this, SLOT(clickedSlot()));发布于 2015-11-19 04:08:04
在QPixmap上接收鼠标事件是没有意义的,因为QPixmap不是QWidget,因此QPixmap永远不会直接出现在Qt中。
屏幕上的是绘制和显示QPixmap的某种类型的QPixmap。这可能是一个QLabel,也可能是一个QWidget,其paintEvent(QPaintEvent *)方法已被重写,以调用以QPixmap作为参数的painter.drawPixmap()。重写mousePressEvent()的合理位置应该在该小部件的子类中,而不是通过子类QPixmap。
https://stackoverflow.com/questions/33791820
复制相似问题