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

Với phương pháp này tôi có thể đặt QSQlQueryModels cho QTableviews của tôi.Đặt màu cho hàng QTableView

Nhưng Làm thế nào tôi có thể thiết lập màu sắc cho một hàng dựa trên giá trị của ô?

Trả lời

18

Quan điểm thu hút nền dựa trên Qt::BackgroundRole vai trò của tế bào đó là giá trị QBrush được trả về bởi QAbstractItemModel::data(index, role) cho vai trò đó.

Bạn có thể phân lớp các QSqlQueryModel để xác định lại data() trở lại màu sắc tính của bạn, hoặc nếu bạn có Qt> 4.8, bạn có thể sử dụng một 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); 
    } 
}; 

Và sử dụng mô hình mà trong giao diện, với sql mô hình được đặt làm nguồn với QIdentityProxyModel::setSourceModel.

HOẶC

Bạn có thể giữ các mô hình không thay đổi và sửa đổi nền với một đại biểu đặt trên quan điểm với 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())); 
    } 
}; 

Là phương pháp cuối cùng không phải lúc nào cũng rõ ràng để dịch từ C++, ở đây tương đương với python:

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

+1 để tham khảo giải pháp với đại biểu. Tôi đã quên điều đó. – dschulz

+0

tôi cần phải thiết lập một màu cho mỗi giá trị của một colmun bảng (tên SELECT, tình trạng TỪ người dùng) trong trường hợp "tình trạng" này, bạn có thể chỉnh sửa mã này. – Tineo

+0

optionV4-> backgroundBrush = QBrush (calculateColorForRow (index.row())); nó tạo ra lỗi – Tineo

3

Tốt nhất là để xác định một mô hình tùy chỉnh (QAbstractTableModel lớp con). Bạn có thể muốn có một số QSqlQueryModel làm thành viên trong lớp tùy chỉnh này.

Nếu đó là một mô hình read-only, bạn cần phải thực hiện ít nhất là những phương pháp:

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

và cho các mô hình cũng cư xử cũng

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

Nếu bạn cần mô hình để có thể chỉnh sửa/gửi dữ liệu, mọi thứ có liên quan nhiều hơn một chút và bạn cũng sẽ cần phải triển khai các phương pháp sau:

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

Điều gì thực sự sẽ thay đổi một diện mạo hàng nằm trong giá trị trả về của phương pháp này:

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

Một ví dụ câm:

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

     } 
    } 

    } 
Các vấn đề liên quan