2012-04-18 68 views
6
void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ 
     model = new QSqlQueryModel(this); 
     model->setQuery(sql); 
} 

この方法では、QSQlQueryModelsをQTableviewsに設定できます。QTableView行に色を設定する

しかし、セルの値に基づいて色を行に設定する方法はありますか?

答えて

18

図である細胞のQt::BackgroundRole役割に基づいて背景を描画しますその役割のためにQAbstractItemModel::data(index, role)によって返されたQBrush値。

あなたの計算された色を返すようにdata()を再定義するために、またはあなたがQtの> 4.8を持っている場合、あなたはQIdentityProxyModel使用できるQSqlQueryModelをサブクラス化することができます

class MyModel : public QIdentityProxyModel 
{ 
    QColor calculateColorForRow(int row) const { 
     ... 
    } 

    QVariant data(const QModelIndex &index, int role) 
    { 
     if (role == Qt::BackgroundRole) { 
      int row = index.row(); 
      QColor color = calculateColorForRow(row);   
      return QBrush(color); 
     } 
     return QIdentityProxyModel::data(index, role); 
    } 
}; 

とSQLで、ビューにそのモデルを使用しますモデルはソースとしてQIdentityProxyModel::setSourceModelと設定されています。あなたがQAbstractItemView::setItemDelegateでビューに設定されたデリゲートと変わらないモデルを維持し、背景を変更することができ

OR

class BackgroundColorDelegate : public QStyledItemDelegate { 

public: 
    BackgroundColorDelegate(QObject *parent = 0) 
     : QStyledItemDelegate(parent) 
    { 
    } 
    QColor calculateColorForRow(int row) const; 

    void initStyleOption(QStyleOptionViewItem *option, 
         const QModelIndex &index) const 
    { 
     QStyledItemDelegate::initStyleOption(option, index); 

     QStyleOptionViewItemV4 *optionV4 = 
       qstyleoption_cast<QStyleOptionViewItemV4*>(option); 

     optionV4->backgroundBrush = QBrush(calculateColorForRow(index.row())); 
    } 
}; 

の最後の方法は、C++コードから変換するために必ずしも明確ではないので、ここでは、Pythonで同等です:

def initStyleOption(self, option, index): 
    super(BackgroundColorDelegate,self).initStyleOption(option, index) 
    option.backgroundBrush = calculateColorForRow(index.row()) 
+1

+1デリゲートを持つソリューションへの参照です。私はそれを忘れてしまった。 – dschulz

+0

私はテーブルcolmunの各値の色を設定する必要があります(SELECT name、status FROM users)この場合 "status"このコードを編集できますか? – Tineo

+0

optionV4-> backgroundBrush = QBrush(calculateColorForRow(index.row()));それはエラー – Tineo

3

カスタムモデル(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); 

     } 
    } 

    } 
関連する問題