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. Clickable links in QListWidget with Rich Text
Forum Updated to NodeBB v4.3 + New Features

Clickable links in QListWidget with Rich Text

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.1k 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.
  • S Offline
    S Offline
    Scla
    wrote on last edited by
    #1

    Hello everyone,
    I am trying to implement a way to have clickable URLs in a QListWidget.
    I am using the HTMLDelegate class described in this reply: https://stackoverflow.com/a/1956781
    The text is interpreted correctly and it's painted correctly, but the URLs in <a> tags are not clickable in any way.

    Example text for an item: Sample text <a href="http://sample.com">this is an url</a> and <b>this is bold text</b>
    Does anyone have any ideas how I can make the URLs clickable?

    I'm on Qt 6.4, if it is a relevant information.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If memory serves well, you would implement the editorEvent method and there detect whether the cursor is on top of your link and use QDesktopServices to open it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        If memory serves well, you would implement the editorEvent method and there detect whether the cursor is on top of your link and use QDesktopServices to open it.

        S Offline
        S Offline
        Scla
        wrote on last edited by
        #3

        @SGaist Hello, thanks for the answer!
        I think the editorEvent route may be the one I'm looking for, but I'm not really sure on how to implement it.
        Can I ask you to give it a go?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Something along the line of:

          bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
          {
               if (event->type() == QEvent::MouseButtonRelease) {
                   // check if link text is under cursor
                   if (linkIsUnderCursor) {
                       QString url = index.data().toString();
                       QDesktopServices::openUrl(QUrl(url));
                       return true;
                   }
               }
               return QStyledItemDelegate.editorEvent(event, model, option, index);
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S G 2 Replies Last reply
          3
          • SGaistS SGaist

            Something along the line of:

            bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
            {
                 if (event->type() == QEvent::MouseButtonRelease) {
                     // check if link text is under cursor
                     if (linkIsUnderCursor) {
                         QString url = index.data().toString();
                         QDesktopServices::openUrl(QUrl(url));
                         return true;
                     }
                 }
                 return QStyledItemDelegate.editorEvent(event, model, option, index);
            
            S Offline
            S Offline
            Scla
            wrote on last edited by
            #5

            @SGaist Correct me if I'm wrong, but this is based on the assumption that only one URL will be present in the message, which is not the case

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

              That's an information you did not give. In any case, the idea is the same except that you have to find in which part of the text the cursor is hovering. You might be able to leverage the implementation of QLabel to replicate the behaviour here.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • SGaistS SGaist

                Something along the line of:

                bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
                {
                     if (event->type() == QEvent::MouseButtonRelease) {
                         // check if link text is under cursor
                         if (linkIsUnderCursor) {
                             QString url = index.data().toString();
                             QDesktopServices::openUrl(QUrl(url));
                             return true;
                         }
                     }
                     return QStyledItemDelegate.editorEvent(event, model, option, index);
                
                G Offline
                G Offline
                GayaneAlaverdyan
                wrote on last edited by
                #7

                @SGaist How can I check that the link is under cursor?

                SGaistS 1 Reply Last reply
                0
                • G GayaneAlaverdyan

                  @SGaist How can I check that the link is under cursor?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @GayaneAlaverdyan did you check the QLabel implementation as suggested above ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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