How to show icons use QSqlRelationalTableModel and QTableView in every row
-
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);
}
} -
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);
}@
-
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);
}
@ -
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));
}@