子类化QPixmap

时间:2015-11-18 22:38:55

标签: c++ qt events subclass qpixmap

我想在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子类化以获取鼠标按下事件?

2 个答案:

答案 0 :(得分:3)

在QPixmap上接收鼠标事件没有意义,因为QPixmap不是QWidget,因此QPixmap永远不会直接出现在Qt GUI中。

屏幕上的 是一种绘制和显示QPixmap的QWidget。这可能是QLabel或QWidget,其paintEvent(QPaintEvent *)方法已被重写,以QPixmap作为参数调用painter.drawPixmap()。覆盖mousePressEvent()的合理位置将在该窗口小部件的子类中,而不是通过子类化QPixmap。

答案 1 :(得分:0)

我最终使用了QPushButton:

QPushButton *button = new QPushButton;
button->setIcon(QIcon(myQPixmap));
buttonWidget=MySceneClass->scene()->addWidget(button);
QObject::connect(button, SIGNAL(clicked()),this, SLOT(clickedSlot()));
相关问题