0

カスタム テーブル モデルは から派生しQAbstractTableModel、 に表示されますQTableView

次のようになります。

ここに画像の説明を入力

モデルで決定できる特定の行ヘッダーのテキストの色を変更したいと思います。そこから特定のヘッダーに色を付けることは可能ですか? 私は今まで道を見つけることができませんでした。私が見つけたのは、特別なヘッダーではなく、すべてのヘッダーの背景/テキストの色を設定することでした。色は、ユーザーにとって一種のマークアップであると想定されています。

4

1 に答える 1

2

あなたがしなければならないのは再実装QAbstractTableModel::headerData()です。セクションの値(ゼロから始まるヘッダーインデックス)に応じて、ヘッダーアイテムのスタイルを個別に設定できます。Qt :: ItemDataRoleの前景(=テキストの色)と背景に関連する値はQt::BackgroundRoleQt::ForegrondRole

例:このように:

QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
  //make all odd horizontal header items black background with white text
  //for even ones just keep the default background and make text red
  if (orientation == Qt::Horizontal) {
    if (role == Qt::ForegroundRole) {
       if (section % 2 == 0)
          return Qt::red;
       else
         return Qt::white;
    }
    else if (role == Qt::BackgroundRole) {
       if (section % 2 == 0)
          return QVariant();
       else
         return Qt::black;
    }
    else if (...) {
      ...
      // handle other roles e.g. Qt::DisplayRole
      ...
    }
    else {
      //nothing special -> use default values
      return QVariant();
    }
  }
  else if (orientation == Qt::Vertical) {
      ...
      // handle the vertical header items
      ...
  }
  return QVariant();
}
于 2012-11-05T17:36:16.267 に答える