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 show icons use QSqlRelationalTableModel and QTableView in every row
Qt 6.11 is out! See what's new in the release blog

How to show icons use QSqlRelationalTableModel and QTableView in every row

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k 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.
  • A Offline
    A Offline
    aliceljm
    wrote on last edited by
    #1

    Now,I am try to use QSqlRelationalTableModel and QTableView to show my data in database,according to the properity "fileType" such as "doc,txt,exe,sln" to show a icon in the first column.

    dirModel=new QSqlRelationalTableModel(this);
    dirModel->setTable("ecm_doc");
    dirModel->setFilter(QString("creatoruserid='%1' and parentid='%2'").arg(userid).arg(parentid));
    dirModel->select();
    dirView=new QTableView(this);
    dirView->setItemDelegate(new DirDelegate(this));
    
    dirView->setModel(dirModel);
    dirView->setSelectionMode(QAbstractItemView::SingleSelection);
    
     showIcon();
    

    void DirTree::showIcon()
    {
    int rowCount = dirModel->rowCount();
    for(int row = 0; row < rowCount; row++)
    {
    //here is a test.
    QModelIndex index = dirModel->index(row, 1);
    QIcon folderIcon(style()->standardPixmap(QStyle::SP_DirClosedIcon));
    dirModel->setData(index, folderIcon, Qt::DecorationRole);
    }
    }

    1 Reply Last reply
    0
    • D Offline
      D Offline
      D0IT
      wrote on last edited by
      #2

      The default implementation of the data method of QSqlRelationalTableModel will only return data for the Qt::DisplayRole and Qt::EditRole (line 4 below). Thus you'll need to provide a custom model e.g. by subclassing "QIdentityProxyModel":http://qt-project.org/doc/qt-4.8/qidentityproxymodel.html and reimplementing the data method to also return data for the Qt::DecorationRole.

      @QVariant QSqlTableModel::data(const QModelIndex &index, int role) const
      {
      Q_D(const QSqlTableModel);
      if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole))
      return QVariant();

      QModelIndex item = indexInQuery(index);
      
      switch (d->strategy) {
      case OnFieldChange:
      case OnRowChange:
          if (index.row() == d->insertIndex) {
              QVariant val;
              if (item.column() < 0 || item.column() >= d->rec.count())
                  return val;
              val = d->editBuffer.value(index.column());
              if (val.type() == QVariant::Invalid)
                  val = QVariant(d->rec.field(item.column()).type());
              return val;
          }
          if (d->editIndex == item.row()) {
              QVariant var = d->editBuffer.value(item.column());
              if (var.isValid())
                  return var;
          }
          break;
      case OnManualSubmit: {
          const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
          const QVariant var = row.rec.value(item.column());
          if (var.isValid() || row.op == QSqlTableModelPrivate::Insert)
              return var;
          break; }
      }
      return QSqlQueryModel::data(item, role);
      

      }@

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

        Thank you a lot,now i am subclassed QSqlRelationalTableModel and overwriteed the mathod "data" it works,but how can i get data table information in "data" method

        @
        QVariant SqlTableModel::data(const QModelIndex &item, int role) const
        {
        if(role==Qt::DecorationRole)
        {
        QString fileType=;//from the column 9 in data table,such as txt,exe,sln
        return getIconFromType(fileType);
        }
        return QSqlRelationalTableModel::data(item,role);
        }
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aliceljm
          wrote on last edited by
          #4

          I got it,but i think my method getIconFromType is not good,because i can't find a way to replace it ,Can you Help me?
          @
          QVariant SqlTableModel::data(const QModelIndex &item, int role) const
          {
          if(role==Qt::DecorationRole&&item.column()==Doc_DocType)
          {
          QSqlRecord r=this->record(item.row());
          return QVariant(getIconFromType(r.field(Doc_DocType).value().toString()));
          }
          return QSqlRelationalTableModel::data(item,role);
          }

          QIcon SqlTableModel::getIconFromType(QString filetype)
          {
          QDir currentdir=QDir::currentPath();
          QDir d(currentdir.path()+"/icon");
          if(!d.exists())
          if(!d.mkdir(d.path()))
          qDebug()<<"make dir /icon fail";
          QString iconfile=currentdir.path()+"/icon/icon."+filetype;
          QFileIconProvider p;
          if(!QFile::exists(iconfile))
          {
          QFile file(iconfile);
          if(!file.open(QIODevice::WriteOnly))
          {
          qDebug()<<QString("write a .%1 file fail").arg(filetype);
          return p.icon(QFileIconProvider::File);
          }
          file.close();
          file.deleteLater();
          }
          return p.icon(QFileInfo(iconfile));
          }

          @

          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