参考: https://wiki.qt.io/Clickable_QLabel
头文件:
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include <QLabel>
#include <QMouseEvent>
#include <QWidget>
class ClickableLabel : public QLabel {
Q_OBJECT
public:
ClickableLabel(QWidget* parent = nullptr);
~ClickableLabel();
signals:
void clicked();
protected:
virtual void mousePressEvent(QMouseEvent* event);
};
#endif // CLICKABLELABEL_H
实现
#include "clickablelabel.h"
ClickableLabel::ClickableLabel(QWidget *parent)
{
}
ClickableLabel::~ClickableLabel() { }
void ClickableLabel::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
emit clicked();
}
}
使用
ClickableLabel* label = new ClickableLabel();
connect(label, &ClickableLabel::clicked, this, &ClassName::SlotRemoveTableRow);