QLabel doesn't act like a link while it should



  • code like this:

        cTitle = QStringLiteral("<a href=\"\">")+QChar(0x25b6)+title+"</a>";
        eTitle = QStringLiteral("<a href=\"\">")+QChar(0x25bc)+title+"</a>";
        titleLabel = new QLabel(cTitle, this);
        titleLabel->setTextFormat(Qt::RichText);
        titleLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByKeyboard);
        titleLabel->setOpenExternalLinks(false);
    

    but the label still looks like plain text and when clicking nothing happened (I have connected the linkActivated signal

    connect(titleLabel, QLabel::linkActivated, this, clicked);
    

    )
    Any help please. Thanks in advance!


  • Qt Champions 2016

    Does connect return true?
    its normally
    Activated(const QString)
    but new syntax hides that info it seems.

    If you do it all in Designer, it will insert extra to color link
    label->setText("<html><head/><body><p><a href="HELLO!"><span style=" text-decoration: underline; color:#0000ff;">TextLabel</span></a></p></body></html>");

    it does work :)
    https://www.dropbox.com/s/tl4to8nr9i3z5fg/mylink.zip?dl=0


  • Moderators

    @rockinJ
    you should at least specify a protocol in the url. Then the text will be rendered as link.
    So use

    QStringLiteral("<a href=\"http://\">")
    

    or even

    QStringLiteral("<a href=\"foo://\">")
    


  • thank you @mrjj !
    after study your example, I found that it can work even I change to this:

        cTitle = QStringLiteral("<a href=\"c\">")+QChar(0x25b6)+title+"</a>";
        eTitle = QStringLiteral("<a href=\"e\">")+QChar(0x25bc)+title+"</a>";
        titleLabel = new QLabel(cTitle, this);
    //    titleLabel->setTextFormat(Qt::RichText);
    //    titleLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByKeyboard);
    //    titleLabel->setOpenExternalLinks(false);
    

    the only thing we need is to put something into the href instead of an empty string.



  • @raven-worx that's right, thank you!


Log in to reply
 

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