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. How to hyperlink a data item in a QTableview table.
Forum Updated to NodeBB v4.3 + New Features

How to hyperlink a data item in a QTableview table.

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

    I am using QTableView to display set of data in a table successfully.
    Now I wanted to know how I can have in that table, data items in a cell as hyperlink so that when I click on a data items a new window should open up. Any help is much solicited.

    Note:I know entered() SIGNAL but I don't know how to make the data available in the Cell colored/under line. So that it look like hyperlink.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      The view emits a clicked() signal. It contains a QModelIndex. Using that parameter, you can get the data out of the view's model.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mady
        wrote on last edited by
        #3

        Thanks Volker, but what I want is to show the content of a particular cell as underlined and colored but I don't know how to do that.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          I think you have to do this in your implemetation of the QAbstractTableModel::data(..) method.
          @
          if(role == Qt::ForegroundRole)
          {
          return QVariant(Qt::blue);
          }
          @
          This will give a blue colored text. Don't know from the top of my mind how to get the underline property though.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Better use a proxy model. The brand new [[Doc:QIdentityProxyModel]] of 4.8 would be the perfect base.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mady
              wrote on last edited by
              #6

              I would love to use Qt4.8 but unfortunately we have decided to stick with 4.7 :(....

              BTW, I saw clicked() signal and it seems it tells only in which block/cell user has clicked. What if I wanted to know whether it is right click or left click and based on that my app will respond to that event.
              Is it possible to detect whether that mouse click is right or left?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                To my knowledget, no, there is no means to detect wether it was a left, middle or right click.

                For 4.7, [[Doc:QSortFilterProxyModel]] would be a good start. It's a bit heavyweighted for your purpose, but you need not do actual filtering, etc.

                If your application is open source (GPL) or if you use a commercial license, you could consider just taking the sources of the identity proxy model form 4.8 and incorporate it in your project (I would rename the classes to not run into trouble once you switch to 4.8, though). It just two relatively small files.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KA51O
                  wrote on last edited by
                  #8

                  to detect the mouse click and react to it depending on the pressed mouse button just reimplement the mousePressedEvent(QMouseEvent* event) for your QTableView.

                  @
                  void YourTableView::mousePressEvent(QMouseEvent* event)
                  {
                  // get the buttons type
                  Qt::MouseButtons mouseButton = event->buttons();
                  // only the right mouse buton
                  if( mouseButton == Qt::RightButton )
                  {
                  // clear the previous selection
                  clearSelection();
                  // select the item at the position of the right-click event
                  QPersistentModelIndex nextIndex = indexAt(event->pos());
                  setCurrentIndex(nextIndex);
                  // this is where I start my custom context menu, do the stuff you want to here
                  }
                  else
                  {
                  //call the parents function
                  QTreeView::mousePressEvent(event);
                  }
                  }
                  @

                  When you have retrieved the index for the event position you should also have a check if the cell related to the index of the event position is your hyperlink or not.

                  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