C++ 子类化QPixmap
我想在Qt中的QPixmap上获取鼠标按下事件。 我尝试使用以下方法对其进行子类化:C++ 子类化QPixmap,c++,qt,events,subclass,qpixmap,C++,Qt,Events,Subclass,Qpixmap,我想在Qt中的QPixmap上获取鼠标按下事件。 我尝试使用以下方法对其进行子类化: class CustomPixmap : public QPixmap { Q_OBJECT public: CustomPixmap(QPaintDevice *parent = NULL); ~CustomPixmap() {}; protected: void mousePressEvent(QMouseEvent *event); }; 但由于错误,它无法编译 .
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_对象可以很好地编译,但不调用MousePresseEvent。如何正确地将QPixmap子类化以获取鼠标按下事件 在QPixmap上接收鼠标事件没有意义,因为QPixmap不是QWidget,因此QPixmap永远不会直接出现在Qt GUI中
屏幕上显示的是某种QWidget,用于绘制和显示QPixmap。这可能是一个QLabel,也可能是一个QWidget,它的paintEvent(QPaintEvent*)方法已被重写,以QPixmap作为参数调用painter.drawPixmap()。覆盖mousePressEvent()的合理位置是在该小部件的子类中,而不是通过子类化QPixmap。在QPixmap上接收鼠标事件没有意义,因为QPixmap不是QWidget,因此QPixmap永远不会直接出现在Qt GUI中
屏幕上显示的是某种QWidget,用于绘制和显示QPixmap。这可能是一个QLabel,也可能是一个QWidget,它的paintEvent(QPaintEvent*)方法已被重写,以QPixmap作为参数调用painter.drawPixmap()。覆盖mousePressEvent()的合理位置是在该小部件的子类中,而不是通过子类化QPixmap。我最终使用了QPushButton:
QPushButton *button = new QPushButton;
button->setIcon(QIcon(myQPixmap));
buttonWidget=MySceneClass->scene()->addWidget(button);
QObject::connect(button, SIGNAL(clicked()),this, SLOT(clickedSlot()));
我最终使用了一个QPushButton:
QPushButton *button = new QPushButton;
button->setIcon(QIcon(myQPixmap));
buttonWidget=MySceneClass->scene()->addWidget(button);
QObject::connect(button, SIGNAL(clicked()),this, SLOT(clickedSlot()));
谢谢那么具体地说,如何将我的QPixmap链接到QWidget呢?将Pixmap添加到我的QGraphicscene会给我一个QGraphicsPixmapItem。这是我应该截取鼠标事件的那个吗?如果是,谢谢。重写QGraphicsSitem::MousePresseEvent(QGraphicsScenemousePresseEvent*)方法。谢谢那么具体地说,如何将我的QPixmap链接到QWidget呢?将Pixmap添加到我的QGraphicscene会给我一个QGraphicsPixmapItem。这是我应该截取鼠标事件的那个吗?如果是,谢谢。重写QGraphicsSitem::MousePresseEvent(QGraphicsScenemousePresseEvent*)方法。