How to display richtext and images in QTableview and QSqlQueryModel delegate

  • Im trying to find away ( simple ) to be able to display Images and RichText when using QTableview and QSqlQueryModel delegate class ,
    when fetching data from SQLite. i like to be able to present some text as links and some text adding them Images .
    i delegate the QSqlQueryModel and i know i have control on the Text from the data method but how can i make the Text rich ( to add underline for example )

    @QVariant ListSqlModel::data(const QModelIndex &index, int role) const
    QVariant value = QSqlQueryModel::data(index, role);

    if (value.isValid() && role == Qt::DisplayRole) 
        if (index.column() == 4)
            return value.toString().toUpper();


  • You can combine void QAbstractItemView::setIndexWidget (const QModelIndex& index, QWidget* widget) with data in you model. Where widget its QLabel. Something like this:
    QString str = model()->data(index, Qt::DisplayRole).toString();
    QString richStr = "<font color = "red">" + str + "</font>";
    QLabel *label = new QLabel;
    view->setItemWidget(index, label);

  • Note that the approach above works, but is not very performant and totally breaks the way model/view is supposed to work.

    If it is enough to modify the display properties of the whole text in an item, then you can use a proxy model to set the data roles for that. If you want to have real rich text, that is, text where within a cell different parts of the text have different fonts, then you have a problem. That is hard to do. It is possible to do this using a delegate, but it is hard to make it work right.

  • i just what , underline in the text and make the mouse icon change when it is over the text . now i try to work with ItemDelegate implementing the paint method .. i dont know how it will work :/

Log in to reply

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