Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to open a .txt file directly by clicking on a label



  • Hi all,
    maybe this is a stupid issue, but I've been searching the solution on Internet since two hours and now I'm fed up and I ask to you.

    I have a Qlabel. When I click it (by using the slot linkActivated I suppose) I want to directly open a .txt file in my PC.

    Is this doable? Who can help me? Please let me know the proper instructions.



  • Yes you can do this with QDesktopServices::openUrl.



  • @cfdev

    Hi, Thank you for reply.
    Could you be more precise? Have you got an example ?

    How can I set the label, link, url, QDesktopService, and so on... ?

    Thank you ;)


  • Moderators

    @FranciscoSantiago

    QLabel* label = new QLabel("<a href=\"file:///path/to/my/local/file\">Any text or file path</a>");
    connect( label, SIGNAL(linkActivated(QString)), this, SLOT(onLabelLinkClicked(QString)) );
    ...
    void onLabelLinkClicked(const QString & link)
    {
          QUrl url = QUrl::fromUserInput(link);
          if( url.isValid() )
              QDesktopServices::openUrl(url);
    }
    

Log in to reply