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. [SOLVED] How can a label be linkable not to a website but to a void funtion of the program?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How can a label be linkable not to a website but to a void funtion of the program?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 5.7k Views 1 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    Lets say void is called :
    void MainWindow::label_clicked()

    Usually i use labels with links , that link u to a website and not to a funtion, thus i don't know how to do it...

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      QLabel has no clicked() signal, so you must create one by subclassing QLabel:

      @class MyLabel : public QLabel
      {
      Q_OBJECT
      public: explicit MyLabel(QWidget *parent = 0) : QLabel(parent){}
      signals: void clicked();
      protected: void mouseReleaseEvent(QMouseEvent *ev) {emit clicked();}
      };@

      Each time you click on MyLabel it will emit a clicked() signal, which you can connect to whatever you want using the regular Qt connect syntax:

      @MyLabel *label = new MyLabel(this);
      label->setText("Custom clickable label");

      connect(label, SIGNAL(clicked()), this, SLOT(labelClicked()));@

      In Qt5 you can connect it to any function, not just slots, by using the new connection syntax.

      Other than the ability to click, MyLabel provides the same functionality as QLabel.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        Ok so i added this to my header file ( changed signals to Q_SIGNALS )

        @class MyLabel : public QLabel
        {
        Q_OBJECT
        public: explicit MyLabel(QWidget *parent = 0) : QLabel(parent){}

        Q_SIGNALS: void clicked();

        protected: void mouseReleaseEvent(QMouseEvent *ev) {emit clicked();}
        };@

        And this to my cpp file ( label is already made in the designer )
        @MyLabel *label = ui->how_to_use_label;
        connect(label, SIGNAL(clicked()), this, SLOT(label_clicked()));@

        Thus i get an error:
        'emit' was not declared in this scope

        Am i doing something wrong?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          You are assigning the MyLabel pointer to a QLabel pointer from the UI, you are not really creating a MyLabel instance in this case. I don't know if that is the cause of the error, but it is the most obvious candidate.

          Try creating the label with new as in my example and then add it to the corresponding layout in your UI.

          If you want to, you can export MyLabel as a component for designer "by following this example":http://harmattan-dev.nokia.com/docs/library/html/qt4/designer-customwidgetplugin.html.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leon
            wrote on last edited by
            #5

            Well even if i dont assign it by UI but create it with ur code it's the same error about the emit not beeing declared..

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              In that case I don't know, it works for me, emit is the standard Qt keyword to emit a signal. Do you have the Q_OBJECT macro in the header of MyLabel as in my example?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                There is no need to subclass QLabel.

                Just set "openExternalLinks":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#openExternalLinks-prop to false on connect to the "linkActivated":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#linkActivated signal.
                @
                ui->label->setOpenExternalLinks(false);
                connect(ui->label, SIGNAL(linkActivated(QString)), this, SLOT(label_clicked(QString)));

                void MainWindow::label_clicked(const QString &link)
                {
                if (link == "...") ...
                }
                @

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Leon
                  wrote on last edited by
                  #8

                  [quote author="Lukas Geyer" date="1358770207"]There is no need to subclass QLabel.

                  Just set "openExternalLinks":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#openExternalLinks-prop to false on connect to the "linkActivated":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#linkActivated signal.
                  @
                  ui->label->setOpenExternalLinks(false);
                  connect(ui->label, SIGNAL(linkActivated(QString)), this, SLOT(label_clicked(QString)));

                  void MainWindow::label_clicked(const QString &link)
                  {
                  if (link == "...") ...
                  }
                  @[/quote]

                  and how should the text of the label be?
                  how will it be clickable(linkable).?

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Code_ReaQtor
                    wrote on last edited by
                    #9

                    [quote author="Leon" date="1358781102"]

                    and how should the text of the label be?
                    how will it be clickable(linkable).?

                    [/quote]

                    The text SHOULD be in "Rich text" , AFAIK.

                    Please visit my open-source projects at https://github.com/Code-ReaQtor.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Leon
                      wrote on last edited by
                      #10

                      [quote author="Code_ReaQtor" date="1358782000"]
                      [quote author="Leon" date="1358781102"]

                      and how should the text of the label be?
                      how will it be clickable(linkable).?

                      [/quote]

                      The text SHOULD be in "Rich text" , AFAIK. [/quote]

                      I know that.. You click change rich text in the designer and there is a button "Insert link".

                      What should i put there when i want the label_clicked void to be called..?

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        Whatever you like.

                        @
                        ui->label->setText("This is text with a <a href='someLink'>link</a>.");

                        void MainWindow::label_clicked(const QString &link)
                        {
                        if (link == "someLink") ...
                        }
                        @

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          Leon
                          wrote on last edited by
                          #12

                          [quote author="Lukas Geyer" date="1358782837"]Whatever you like.

                          @
                          ui->label->setText("This is text with a <a href='someLink'>link</a>.");

                          void MainWindow::label_clicked(const QString &link)
                          {
                          if (link == "someLink") ...
                          }
                          @[/quote]

                          Didn't know that, thanks! :)

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            Leon
                            wrote on last edited by
                            #13

                            [quote author="utcenter" date="1358720224"]In that case I don't know, it works for me, emit is the standard Qt keyword to emit a signal. Do you have the Q_OBJECT macro in the header of MyLabel as in my example?[/quote]

                            And by the way, yes i was using
                            #define QT_NO_KEYWORDS

                            I just saw it...

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #14

                              You're welcome! ;-)

                              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