How to hyperlink a data item in a QTableview table.

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

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

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

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

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

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

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

  • 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
    // select the item at the position of the right-click event
    QPersistentModelIndex nextIndex = indexAt(event->pos());
    // this is where I start my custom context menu, do the stuff you want to here
    //call the parents function

    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.

Log in to reply