आपकी सर्वश्रेष्ठ शर्त एक कस्टम मॉडल (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);
}
}
}
+1 एक प्रतिनिधि के साथ समाधान के संदर्भ के लिए। मैं इसके विषय मे भूल गया। – dschulz
मुझे इस मामले में "स्थिति" में एक तालिका colmun (चयन नाम, उपयोगकर्ताओं से स्थिति) के प्रत्येक मान के लिए एक रंग सेट करने की आवश्यकता है। क्या आप इस कोड को संपादित कर सकते हैं। – Tineo
विकल्प वी 4-> पृष्ठभूमिब्रश = क्यूब्रश (गणना करें कोलोरफोररो (index.row())); यह त्रुटि उत्पन्न करता है – Tineo