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 to click the qlabel to perform some action....
Forum Updated to NodeBB v4.3 + New Features

how to click the qlabel to perform some action....

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 6.3k Views 5 Watching
  • 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.
  • ? A Former User

    Hi! See this wiki article: Clickable QLabel.

    the_T Offline
    the_T Offline
    the_
    wrote on last edited by
    #3

    Another solution could be using event filter

    //MyClass.h
    QLabel *clickablelabel
    
    //MyClass.cpp
    MyClass::MyClass(QObject *parent) {
    //...
    clickablelabel = new QLabel("Click Me!");
    
    clickablelabel->installEventFilter(this);
    //...
    }
    bool MyClass::eventFilter(QObject *o, QEvent *e) {
    
    if(o == clickablelabel && e->type() == QMouseEvent::MouseButtonPress) {
    //do something
    }
    /* //using metaObject() thingies 
    if(e->type() == QMouseEvent::MouseButtonPress && o->metaObject()->className() == (new QLabel)->metaObject()->className()) {
    //do something
    }
    */
    return QObject::eventFilter(o,e);
    }
    

    -- No support in PM --

    1 Reply Last reply
    4
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Two other options:

      If it's clickable why not use a button? You can make it flat if you don't want the usual button bevel.

      You can put an html link in the label and it will emit a linkActivated signal when you click on it.

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #5

        so 3 super answers to the cool description:

        list item

        :)

        1 Reply Last reply
        5
        • ? A Former User

          Hi! See this wiki article: Clickable QLabel.

          K Offline
          K Offline
          karti gesar
          wrote on last edited by
          #6

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          QLabel *dispimg= new QLabel(this);
          dispimg->setStyleSheet("image:url(E:/may/may_7/images/singdisp.png);");
          dispimg->setGeometry(330,190,200,200);
          connect(this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }
          void MainWindow::mousePressEvent ( QMouseEvent * event )
          {
          emit clicked();
          }

          void MainWindow::slotClicked()
          {
          qDebug()<<"hello";
          }

          above code is what i used...

          1 Reply Last reply
          0
          • K Offline
            K Offline
            karti gesar
            wrote on last edited by
            #7

            it work's all area of the mouse pressed....i want to click the particular label

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Please read the answers above, there is even code you can use.

              1 Reply Last reply
              1
              • ? A Former User

                Hi! See this wiki article: Clickable QLabel.

                K Offline
                K Offline
                karti gesar
                wrote on last edited by
                #9

                @Wieland i want to click for the particular label....

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  karti gesar
                  wrote on last edited by
                  #10

                  can any one send the coding ..for above question

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    karti gesar
                    wrote on last edited by
                    #11

                    thanks for all..i got it....

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12
                      This post is deleted!
                      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