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 display richtext and images in QTableview and QSqlQueryModel delegate
Forum Updated to NodeBB v4.3 + New Features

How to display richtext and images in QTableview and QSqlQueryModel delegate

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.9k 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.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    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();
        }
    }
    

    }@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stima_ua
      wrote on last edited by
      #2

      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);
      label->setText(richStr);
      @

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          umen242
          wrote on last edited by
          #4

          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 :/

          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