7

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.

innocent boy
  • 315
  • 4
  • 13
  • 2
    In one of my answers I made a [derived `QLabel` with `mousePressEvent()`](https://stackoverflow.com/a/44451423/7478597). Update: It is quite similar like suggested by @zapredelom. – Scheff's Cat Jul 06 '17 at 10:29
  • Also see [QLabel click event using Qt?](https://stackoverflow.com/q/32018941/608639) – jww Dec 18 '19 at 21:30

2 Answers2

7

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

enter image description here

enter image description here

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Bearded Beaver
  • 646
  • 4
  • 21
  • thanks! but for the final step, if I want to my existing label clickable, I need writr ui->label in my new clickablelabel.cpp ? And what should I do? so sorry for asking in detail, I am really a beginner in Qt – innocent boy Jul 06 '17 at 10:46
  • In clickablelabel.cpp you have the code of your clickable label class, it has nothing to do with the existing ordinary `QLabel`. You have existing label somewhere (I suppose in your main window), that's where you need to promote label (if it's created in designer) or change `QLabel` to `ClickableLabel` if it's created dynamically in the code (in this case don't forget to include clickablelabel.h in main window header file) – Bearded Beaver Jul 06 '17 at 10:50
  • yup, the existing Qlabel is in my ui form and in my mainwindow, but how to make it work? For example if i want to show a second dialog bu clicking the label? where and how do I write the code ? I only don't know the structure of writing the code – innocent boy Jul 06 '17 at 11:01
  • You should implement a function and connect it to the cickable label signal `pressed` (or `clicked` or whatever you've called it). Creating dialog should probably be implemented in the main window class – Bearded Beaver Jul 06 '17 at 11:03
  • OK I think I got it, tomorrow I try it, thanks. If worked, I will accept you answer – innocent boy Jul 06 '17 at 12:13
  • Sorry, I failed again, can you just give me a whole example of it? The example include all the code like the new .c file and new .h file and make the label cilckable, after I click the label, I can receive a information box. Can you teach me? So sorry I am so stupid to make it. – innocent boy Jul 07 '17 at 04:25
  • OMG! I forgot to search on github! Thanks a lot! – innocent boy Jul 07 '17 at 08:41
  • I've just made this project for you – Bearded Beaver Jul 07 '17 at 08:45
  • Thanks! Thanks! thanks! The important thing must be said 3 times! – innocent boy Jul 07 '17 at 09:22
  • You can upvote the answer as an addition to your thanks – Bearded Beaver Jul 07 '17 at 10:57
  • How will the promotion know how to construct the custom widget? – Johannes Schaub - litb Jul 25 '17 at 08:26
  • @JohannesSchaub-litb you have the header file with the custom constructor defined – Bearded Beaver Jul 29 '17 at 08:07
  • @bearded but how will the promotion know what arguments to pass? – Johannes Schaub - litb Jul 29 '17 at 11:06
3

You can use QPushButton instead, but if you desperately need QLabel, 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 can't paste complete 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()));
...
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
zapredelom
  • 1,009
  • 1
  • 11
  • 28
  • yes correct and after did this, how can I make my existing label clickable? Do I need promote my existing label or connect it to the class? – innocent boy Jul 06 '17 at 10:36
  • Just change the type in your source from qlabel to Clickable – zapredelom Jul 06 '17 at 10:38
  • you can't connect label to anything, you just can connect signals – Bearded Beaver Jul 06 '17 at 10:40
  • Do you have any complete example? I don't understand it – innocent boy Jul 07 '17 at 07:45
  • @jycjo i've updated answer, with some part of code. unfortunately i can't add all my code there. I hope this will help – zapredelom Jul 07 '17 at 07:58
  • @zapredelom It is ok, thanks a lot but I also a bit confused. Have you seen my updated code above? If I want to the click_test label show the cat image after I click the label, where I wrote wrong? no error shown after I build – innocent boy Jul 07 '17 at 08:21
  • as i see you connected the signal to on_label_clicked in same function , am I right ? if yes, thats the one problem. You need to setup signal/slot connections before the firts signal was emitthed( mostly on clickable label creation) – zapredelom Jul 07 '17 at 08:41