繁体   English   中英

子类化QPixmap

[英]Subclassing 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的类以获取鼠标按下事件?

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

什么在屏幕上是某种形式绘制并显示的QPixmap的一个QWidget。 这可能是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()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM