Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How Can I make Clickable Label in My Project

How Can I make Clickable Label in My Project

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 5.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 13 Feb 2020, 16:09 last edited by
    #2

    Hi
    Did you try this sample ?
    https://wiki.qt.io/Clickable_QLabel

    It will make your own QLabel and then you use that in your MainWindow to click on.

    K 1 Reply Last reply 14 Feb 2020, 05:54
    4
    • M mrjj
      13 Feb 2020, 16:09

      Hi
      Did you try this sample ?
      https://wiki.qt.io/Clickable_QLabel

      It will make your own QLabel and then you use that in your MainWindow to click on.

      K Offline
      K Offline
      Ketan__Patel__0011
      wrote on 14 Feb 2020, 05:54 last edited by
      #3

      @mrjj i was used this reference It works good But i want to set click-event only particular label

      i was used Connect function for any particular label and run my project now i will click any label it will effect each and every label

      M 1 Reply Last reply 14 Feb 2020, 07:52
      0
      • K Ketan__Patel__0011
        14 Feb 2020, 05:54

        @mrjj i was used this reference It works good But i want to set click-event only particular label

        i was used Connect function for any particular label and run my project now i will click any label it will effect each and every label

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 14 Feb 2020, 07:52 last edited by
        #4

        @Ketan__Patel__0011

        Well then something went wrong then as it really is pr label.
        If you dont connect the label, clicking it will do nothing.

        • now i will click any label it will effect each and every label

        Then there is bug in your code. Connecting one instance of the label
        will not affect any other.

        Please show the code so we can correct it. :)

        K 1 Reply Last reply 16 Feb 2020, 12:17
        4
        • M mrjj
          14 Feb 2020, 07:52

          @Ketan__Patel__0011

          Well then something went wrong then as it really is pr label.
          If you dont connect the label, clicking it will do nothing.

          • now i will click any label it will effect each and every label

          Then there is bug in your code. Connecting one instance of the label
          will not affect any other.

          Please show the code so we can correct it. :)

          K Offline
          K Offline
          Ketan__Patel__0011
          wrote on 16 Feb 2020, 12:17 last edited by
          #5

          @mrjj

          I am Take A Three Label, in my ui

          ****************** My header files like this *********************
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>
          #include <QMouseEvent>
          #include <QMessageBox>
          #include <QDebug>

          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();

          private:
          Ui::MainWindow *ui;

          signals:
          void clicked();

          protected:
          void mousePressEvent(QMouseEvent* event);

          };

          #endif // MAINWINDOW_H


          ****************** My Sources file like this *********************

          #include "mainwindow.h"
          #include "ui_mainwindow.h"

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          connect(ui->label,SIGNAL(clicked()),this,SLOT(mousePressEvent()));
          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::mousePressEvent(QMouseEvent *event)
          {
          QMessageBox::information(this,"Message","WELCOME");
          emit clicked();
          }


          when i click any label message will be raised

          but i want this message for any only label1

          please Help Me for slove this problem

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 16 Feb 2020, 12:57 last edited by
            #6

            Hi,

            It has absolutely nothing to do with your labels. Wherever you'll click on the MainWindow it will show that message.

            If you only what to act on a specific QLabel either make that one use the custom class suggested by @mrjj or use an event filter to act only on that QLabel.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply 16 Feb 2020, 13:05
            5
            • S SGaist
              16 Feb 2020, 12:57

              Hi,

              It has absolutely nothing to do with your labels. Wherever you'll click on the MainWindow it will show that message.

              If you only what to act on a specific QLabel either make that one use the custom class suggested by @mrjj or use an event filter to act only on that QLabel.

              K Offline
              K Offline
              Ketan__Patel__0011
              wrote on 16 Feb 2020, 13:05 last edited by
              #7

              @SGaist no i will just click on label then message is raised

              or any other location i clicked that time message will not raised

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 16 Feb 2020, 14:54 last edited by
                #8

                @Ketan__Patel__0011 said in How Can I make Clickable Label in My Project:

                no i will just click on label then message is raised

                I would dobut that since you connect the qlabel to a slot which is called on every mouse click as explained in the documentation. Use another slot name.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                K 1 Reply Last reply 16 Feb 2020, 15:07
                3
                • Christian EhrlicherC Christian Ehrlicher
                  16 Feb 2020, 14:54

                  @Ketan__Patel__0011 said in How Can I make Clickable Label in My Project:

                  no i will just click on label then message is raised

                  I would dobut that since you connect the qlabel to a slot which is called on every mouse click as explained in the documentation. Use another slot name.

                  K Offline
                  K Offline
                  Ketan__Patel__0011
                  wrote on 16 Feb 2020, 15:07 last edited by
                  #9

                  @Christian-Ehrlicher
                  sir

                  Can you make One Example For Me ?

                  plzzz

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 16 Feb 2020, 15:34 last edited by
                    #10

                    @Ketan__Patel__0011 said in How Can I make Clickable Label in My Project:

                    Can you make One Example For Me ?

                    Of what? creating a member function with a different name than 'mousePressEvent()'? What's the problem?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 16 Feb 2020, 15:55 last edited by mrjj
                      #11

                      Hi
                      Several things are wrong
                      If you are using a plain QLabel, it wont have a clicked signal.
                      Only using the class in the link it will have it. a plain QLabel cannot be clicked.

                      So lets say you ARE using the new class then this can work.

                      connect(ui->label,SIGNAL(clicked()),this,SLOT(LabelClicked()));
                      
                      void MainWindow::LabelClicked()
                      {
                      QMessageBox::information(this,"Message","WELCOME");
                      }
                      

                      The reason it shows the message anywhere you click currently is due to the fact you added
                      void MainWindow::mousePressEvent(QMouseEvent *event)
                      for the MAINWINOW do it will trigger anywhere you click on it.
                      So its only useful if you where to click anywhere on the whole Mainwindow and not
                      on any labels.

                      K 1 Reply Last reply 17 Feb 2020, 05:49
                      3
                      • M mrjj
                        16 Feb 2020, 15:55

                        Hi
                        Several things are wrong
                        If you are using a plain QLabel, it wont have a clicked signal.
                        Only using the class in the link it will have it. a plain QLabel cannot be clicked.

                        So lets say you ARE using the new class then this can work.

                        connect(ui->label,SIGNAL(clicked()),this,SLOT(LabelClicked()));
                        
                        void MainWindow::LabelClicked()
                        {
                        QMessageBox::information(this,"Message","WELCOME");
                        }
                        

                        The reason it shows the message anywhere you click currently is due to the fact you added
                        void MainWindow::mousePressEvent(QMouseEvent *event)
                        for the MAINWINOW do it will trigger anywhere you click on it.
                        So its only useful if you where to click anywhere on the whole Mainwindow and not
                        on any labels.

                        K Offline
                        K Offline
                        Ketan__Patel__0011
                        wrote on 17 Feb 2020, 05:49 last edited by
                        #12

                        @mrjj i was write same code in my program but its not worked

                        M 1 Reply Last reply 17 Feb 2020, 08:11
                        0
                        • K Ketan__Patel__0011
                          17 Feb 2020, 05:49

                          @mrjj i was write same code in my program but its not worked

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 17 Feb 2020, 08:11 last edited by
                          #13

                          @Ketan__Patel__0011

                          Well did you use the ClickableLabel or not ?

                          K 1 Reply Last reply 17 Feb 2020, 08:26
                          0
                          • M mrjj
                            17 Feb 2020, 08:11

                            @Ketan__Patel__0011

                            Well did you use the ClickableLabel or not ?

                            K Offline
                            K Offline
                            Ketan__Patel__0011
                            wrote on 17 Feb 2020, 08:26 last edited by
                            #14

                            @mrjj please Can you share me a Full code of project ?
                            like Header file as well as source file code

                            M A 2 Replies Last reply 17 Feb 2020, 08:36
                            0
                            • K Ketan__Patel__0011
                              17 Feb 2020, 08:26

                              @mrjj please Can you share me a Full code of project ?
                              like Header file as well as source file code

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 17 Feb 2020, 08:36 last edited by
                              #15

                              @Ketan__Patel__0011

                              Hi
                              there is no full project.
                              the wiki page shows what to put in the .cpp and .h.

                              However, if its also due to you not understanding how to use it.
                              I can make you a test project this evening if that helps?

                              K 1 Reply Last reply 17 Feb 2020, 13:56
                              0
                              • K Ketan__Patel__0011
                                17 Feb 2020, 08:26

                                @mrjj please Can you share me a Full code of project ?
                                like Header file as well as source file code

                                A Offline
                                A Offline
                                anil_arise
                                wrote on 17 Feb 2020, 10:41 last edited by
                                #16

                                @Ketan__Patel__0011 Example :
                                Clickable QLabel

                                this link will be active for 2 days;
                                you need to use promote widget after add widget header and class name (**right click options in ui file)

                                Screenshot_2020-02-17_16-01-21.png

                                K 2 Replies Last reply 17 Feb 2020, 14:09
                                2
                                • M mrjj
                                  17 Feb 2020, 08:36

                                  @Ketan__Patel__0011

                                  Hi
                                  there is no full project.
                                  the wiki page shows what to put in the .cpp and .h.

                                  However, if its also due to you not understanding how to use it.
                                  I can make you a test project this evening if that helps?

                                  K Offline
                                  K Offline
                                  Ketan__Patel__0011
                                  wrote on 17 Feb 2020, 13:56 last edited by
                                  #17

                                  @mrjj Hello Sir

                                  i am try for last 2 days but nothing is happening

                                  so plz sir help me

                                  How can you share me a test project of clickable_Label ?

                                  1 Reply Last reply
                                  0
                                  • A anil_arise
                                    17 Feb 2020, 10:41

                                    @Ketan__Patel__0011 Example :
                                    Clickable QLabel

                                    this link will be active for 2 days;
                                    you need to use promote widget after add widget header and class name (**right click options in ui file)

                                    Screenshot_2020-02-17_16-01-21.png

                                    K Offline
                                    K Offline
                                    Ketan__Patel__0011
                                    wrote on 17 Feb 2020, 14:09 last edited by
                                    #18

                                    **@anil_arise hey Brother Thank you So much

                                    It really Good Way My Problem is solved

                                    Again Thank You So Much**

                                    1 Reply Last reply
                                    0
                                    • A anil_arise
                                      17 Feb 2020, 10:41

                                      @Ketan__Patel__0011 Example :
                                      Clickable QLabel

                                      this link will be active for 2 days;
                                      you need to use promote widget after add widget header and class name (**right click options in ui file)

                                      Screenshot_2020-02-17_16-01-21.png

                                      K Offline
                                      K Offline
                                      Ketan__Patel__0011
                                      wrote on 2 Mar 2020, 10:34 last edited by
                                      #19

                                      @anil_arise Can you share this link again plzzzzz

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved