QSqlRelationalTableModel 显示图标而不是数据

QSqlRelationalTableModel displaying an icon instead of data

我正在显示一个 table,其中包含一个带有 blob 的列(pdf 文件)。我本可以为用户隐藏该列,但我想在列中显示文件时显示一个图标。我将 QsqlRealtionalTableModel 子类化并重载数据函数。如下所示。我的问题是现在图标与 PDF 中的乱码数据一起显示。我以为这个重载函数是用图标代替数据。

QVariant RelationalTableModelWithIcon::data(const QModelIndex &item, int role) const
{
  if(item.column() == 3 && role == Qt::DecorationRole)
    {
      QSqlRecord r= record(item.row());
      QByteArray a= r.field(3).value().toByteArray();
      QIcon icon = QIcon(":/icons/Art/Icons/Iynque-Flat-Ios7-Style-Documents-Pdf.ico");
      if(a.isNull() == false)
        {
          return QVariant(icon);
        }
    }
  return QSqlRelationalTableModel::data(item,role);
}

DROP TABLE IF EXISTS `ComOper`.`documentsqueue` ;

CREATE TABLE IF NOT EXISTS `ComOper`.`documentsqueue` (
  `iddocumentsqueue` INT NOT NULL AUTO_INCREMENT,
  `iddocument` INT NULL DEFAULT 1,
  `name` VARCHAR(45) NOT NULL,
  `image` MEDIUMBLOB NULL,
  `dateEntered` DATE NULL,
  `dateExpired` DATE NULL,
  `dateApproved` DATE NULL,
  `notes` MEDIUMTEXT NULL,
  `archived` TINYINT NULL DEFAULT 0,
  `iddocType` INT NOT NULL DEFAULT 1,
  `idsupplier` INT NOT NULL,
  `idfacilities` INT NOT NULL DEFAULT 1,
  `idproducts` INT NOT NULL DEFAULT 1,
  `iduser` INT NOT NULL DEFAULT 1,
  PRIMARY KEY (`iddocumentsqueue`),
  CONSTRAINT `fk_documentsqueue_docType1`
    FOREIGN KEY (`iddocType`)
    REFERENCES `ComOper`.`docType` (`iddocType`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_documentsqueue_documents1`
    FOREIGN KEY (`iddocument`)
    REFERENCES `ComOper`.`documents` (`iddocument`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_documentsqueue_user1`
    FOREIGN KEY (`iduser`)
    REFERENCES `ComOper`.`user` (`iduser`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_documentsqueue_products1`
    FOREIGN KEY (`idproducts`)
    REFERENCES `ComOper`.`products` (`idproducts`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_documentsqueue_facilities1`
    FOREIGN KEY (`idfacilities`)
    REFERENCES `ComOper`.`facilities` (`idfacilities`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_documentsqueue_supplier1`
    FOREIGN KEY (`idsupplier`)
    REFERENCES `ComOper`.`supplier` (`idsupplier`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

这样你就不会显示不可读的 PDF 文本,当他们要求 Qt::DisplayRole 角色时你必须 return 一个空字符串,当他们要求 return 图标时Qt::DecorationRole 角色。我还建议仅在图标唯一时阅读一次该图标,如下所示:

*.h

private:
    QIcon icon;

*.cpp

RelationalTableModelWithIcon::RelationalTableModelWithIcon(QObject *parent, QSqlDatabase db):
    QSqlRelationalTableModel(parent, db)
{
    icon =  QIcon(":/icons/Art/Icons/Iynque-Flat-Ios7-Style-Documents-Pdf.ico");
}
QVariant RelationalTableModelWithIcon::data(const QModelIndex &index, int role) const
{
    if(index.column() == 3){
        if(role == Qt::DisplayRole)
            return "";
        else if ( role == Qt::DecorationRole)
            return icon;
    }
    return QSqlRelationalTableModel::data(index, role);
}