12
void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){
        model = new QSqlQueryModel(this);
        model->setQuery(sql);
}

このメソッドを使用すると、QSQlQueryModelsをQTableviewsに設定できます。

しかし、セルの値に基づいて行に色を設定するにはどうすればよいですか?

4

2 に答える 2

28

ビューは、その役割に対してによって返される値Qt::BackgroundRoleであるセルの役割に基づいて背景を描画します。QBrushQAbstractItemModel::data(index, role)

をサブクラス化しQSqlQueryModelて再定義data()し、計算された色を返すか、Qt > 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);
    }
};

そして、ビューでそのモデルを使用し、SQL モデルをソースとして設定し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()));
    }
};

最後の方法は、C++ コードから変換することが常に明白であるとは限らないため、Python での同等の方法を次に示します。

def initStyleOption(self, option, index):
    super(BackgroundColorDelegate,self).initStyleOption(option, index)
    option.backgroundBrush = calculateColorForRow(index.row())
于 2012-04-19T00:44:09.797 に答える
4

最善の策は、カスタム モデル (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 );

        }
     }

  }
于 2012-04-19T00:13:08.097 に答える