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



  • 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...



  • 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.



  • 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?



  • 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.



  • 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..



  • 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?



  • 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 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).?



  • [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 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..?



  • 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 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! :)



  • [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...



  • You're welcome! ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.