カスタムモデル(QAbstractTableModel
サブクラス)を定義することをお勧めします。このカスタムクラスのメンバとしてQSqlQueryModel
が必要です。
それは、読み取り専用モデルの場合は、少なくともこれらのメソッドを実装する必要があります。次のことができるようにするためのモデルが必要な場合も
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
と行儀のモデルのためにデータを編集/送信するには、もう少し複雑になり、これらのメソッドを実装する必要もあります。
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
どのような実際の行の外観を変更しますが、このメソッドの戻り値である:
QVariant data(const QModelIndex &index, int role) const;
ダム例:
QVariant MyCustomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
int col = index.column();
switch (role)
{
case Qt::BackgroundRole:
{
if(somecondition){
// background for this row,col is blue
return QVariant(QBrush (QColor(Qt::blue)));
}
// otherwise background is white
return QVariant(QBrush (QColor(Qt::white)));
}
case Qt::DisplayRole:
{
// return actual content for row,col here, ie. text, numbers
}
case Qt::TextAlignmentRole:
{
if (1==col)
return QVariant (Qt::AlignVCenter | Qt::AlignLeft);
if (2==col)
return QVariant (Qt::AlignVCenter | Qt::AlignTrailing);
return QVariant (Qt::AlignVCenter | Qt::AlignHCenter);
}
}
}
+1デリゲートを持つソリューションへの参照です。私はそれを忘れてしまった。 – dschulz
私はテーブルcolmunの各値の色を設定する必要があります(SELECT name、status FROM users)この場合 "status"このコードを編集できますか? – Tineo
optionV4-> backgroundBrush = QBrush(calculateColorForRow(index.row()));それはエラー – Tineo