Make Qlabel clickable or double clickable in Qt

那年仲夏 提交于 2019-12-20 02:56:15

问题


I am beginner in Qt, now I want to make my label clickable, I have searched so much online, but no one gives my a real example of how they made it. So can someone teach me step by step? Now my basic thinking is creating a new .c file and new .h file respectively and then include them into my mainwindow.c and then connect it with the existing label in ui form. These are what I was trying to do, but can not make it. Hope someone can teach and better put the step picture in the command, thanks. Here is the clicklabel.h code:

#ifndef CLICKEDLABEL_H
#define CLICKEDLABEL_H

#include <QWidget>
#include <QLabel>

class ClickedLabel : public QLabel
{
    Q_OBJECT
public:
    ClickedLabel(QWidget *parent=0): QLabel(parent){}
    ~ClickedLabel() {}
signals:
    void clicked(ClickedLabel* click); 
protected:
    void mouseReleaseEvent(QMouseEvent*); 
};

#endif // CLICKEDLABEL_H

This the clicklabel.c code:

#include "clicklabel.h"
void ClickedLabel::mouseReleaseEvent(QMouseEvent *)
{
    emit clicked(this); 
}

These are what I added into my mainwindow.c( the name of the label is click_test):

void data_labeling::on_label_clicked()
{
    QString path="/home/j/Pictures/images.jpeg";
    QPixmap cat(path);
    connect(ui->click_test, SIGNAL(clicked()), this, 
SLOT(on_label_clicked()));
    ui->click_test->setPixmap(cat);
    ui->click_test->resize(cat.width(),cat.height());

}

Of course I have promoted it to clicklabel.h and also I have added void on_label_click() to my mainwindow.h under private slots, but nothing happened.


回答1:


Create a new class derived from QLabel, reimplement mousePressEvent to emit custom pressed() signal (or any other functionality you need)

If you need to use your clickable label in ui files, follow these steps:

  1. Add QLabel to the form

  2. Right-click on added label and select Promote to...

  3. Enter your clickable label class name and its header file name

  4. Press add, than select your label in the tree and select promote

Now you can use your subclassed label (this tutorial actually works for any subclassed widget) as any QWidget using ui->




回答2:


you can use qpushbutton instead , but it you desperately need qlable , you can do this

clickable.h

class Clickable :public QLabel
    {
        Q_OBJECT
            signals :
        void clicked();
    public:
        void mousePressEvent(QMouseEvent* event);

        using QLabel::QLabel;
    };

clickable.cpp

void Clickable::mousePressEvent(QMouseEvent* event)
{
    emit clicked();
}

UPDATE: this implementation I used in my source code. I cant past compleate code , but here is the part where i used it. source.h

...
private: 
    QLabel* label1;
    QLabel* label2;
...

source.cpp ...

label1 = new Clickable("label1 text", this);
label2 = new Clickable("label2 text", this);
...
connect(label1 , SIGNAL(clicked()), this, SLOT(label1clicked()));
connect(label2 , SIGNAL(clicked()), this, SLOT(label1clicked()));
...


来源:https://stackoverflow.com/questions/44946009/make-qlabel-clickable-or-double-clickable-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!