2012-04-18 13 views

Antwort

18

Der Blick den Hintergrund auf der Qt::BackgroundRole Rolle der Zelle basierte zieht das ist Der Wert QBrush wird von QAbstractItemModel::data(index, role) für diese Rolle zurückgegeben.

können Sie die QSqlQueryModel Unterklasse data() neu zu definieren Ihre berechnete Farbe zurückzukehren, oder wenn Sie Qt> 4.8 haben, können Sie verwenden, um ein QIdentityProxyModel:

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); 
    } 
}; 

Und dieses Modell verwenden, in der Ansicht, mit dem SQL Modell als Quelle mit QIdentityProxyModel::setSourceModel festgelegt.

ODER

Sie das Modell halten unverändert und den Hintergrund mit einem auf der Ansicht mit QAbstractItemView::setItemDelegate gesetzt Delegierten ändern:

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())); 
    } 
}; 

als die letzte Methode nicht immer offensichtlich ist aus C++ Code zu übersetzen, Hier ist das Äquivalent in Python:

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

+1 für den Verweis auf die Lösung mit einem Delegaten. Ich habe es vergessen. – dschulz

+0

ich muss eine Farbe für jeden Wert einer Tabelle colmun (SELECT Name, Status FROM Benutzer) in diesem Fall "status" können Sie diesen Code bearbeiten. – Tineo

+0

optionV4-> backgroundBrush = QBrush (calculateColorForRow (index.row())); es erzeugt einen Fehler – Tineo

3

Am besten definieren Sie ein benutzerdefiniertes Modell (QAbstractTableModel Unterklasse). Sie möchten wahrscheinlich ein QSqlQueryModel als Mitglied in dieser benutzerdefinierten Klasse haben.

Wenn es eine schreibgeschützte Modell ist, müssen Sie zumindest diese Methoden implementieren:

int rowCount(const QModelIndex &parent) const; 
int columnCount(const QModelIndex &parent) const; 
QVariant data(const QModelIndex &index, int role) const; 

und für gut erzogene Modelle auch

QVariant headerData(int section, Qt::Orientation orientation, int role) const; 

Wenn Sie das Modell müssen in der Lage zu Bearbeiten/Senden von Daten, die Dinge werden ein wenig komplizierter und Sie müssen auch diese Methoden implementieren:

Was ist eigentlich eine Reihe Aussehen ändert liegt in dem Rückgabewert dieser Methode:

QVariant data(const QModelIndex &index, int role) const; 

Ein stummes Beispiel:

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); 

     } 
    } 

    } 
Verwandte Themen