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

इस विधि के साथ मैं अपने QTQlQueryModels को मेरे QTableviews पर सेट कर सकता हूं।एक QTableView पंक्ति

लेकिन मैं सेल मूल्य के आधार पर एक पंक्ति में रंग कैसे सेट कर सकता हूं?

उत्तर

18

दृश्य सेल है, जिनमें से Qt::BackgroundRole भूमिका के आधार पर पृष्ठभूमि ड्रॉ 0 क्या वास्तव में एक पंक्ति उपस्थिति बदल जाएगा इस विधि की वापसी मूल्य में निहित है उस भूमिका के लिए QBrush मान QAbstractItemModel::data(index, role) द्वारा लौटाया गया मूल्य।

आप data() को फिर से परिभाषित करने के लिए अपने गणना रंग वापस जाने के लिए QSqlQueryModel उपवर्ग कर सकते हैं, या यदि आप क्यूटी> 4.8, आप एक 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); 
    } 
}; 

और ध्यान में रखते हुए कि मॉडल का उपयोग, एसक्यूएल के साथ QIdentityProxyModel::setSourceModel के साथ स्रोत के रूप में मॉडल सेट करें।

या

आप मॉडल अपरिवर्तित रख सकते और पृष्ठभूमि को संशोधित एक प्रतिनिधि QAbstractItemView::setItemDelegate साथ दृश्य पर सेट के साथ:

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

पिछले विधि सी ++ कोड से अनुवाद करने के लिए स्पष्ट हमेशा नहीं है के रूप में, यहां पायथन में बराबर है:

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

+1 एक प्रतिनिधि के साथ समाधान के संदर्भ के लिए। मैं इसके विषय मे भूल गया। – dschulz

+0

मुझे इस मामले में "स्थिति" में एक तालिका colmun (चयन नाम, उपयोगकर्ताओं से स्थिति) के प्रत्येक मान के लिए एक रंग सेट करने की आवश्यकता है। क्या आप इस कोड को संपादित कर सकते हैं। – Tineo

+0

विकल्प वी 4-> पृष्ठभूमिब्रश = क्यूब्रश (गणना करें कोलोरफोररो (index.row())); यह त्रुटि उत्पन्न करता है – Tineo

3

आपकी सर्वश्रेष्ठ शर्त एक कस्टम मॉडल (QAbstractTableModel सबक्लास) को परिभाषित करना है। शायद आप इस कस्टम क्लास में सदस्य के रूप में QSqlQueryModel रखना चाहते हैं।

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

और अच्छी तरह व्यवहार मॉडल के लिए भी

QVariant headerData(int section, Qt::Orientation orientation, 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); 

     } 
    } 

    } 
संबंधित मुद्दे