proxyView = new QTreeView;
proxyView->setStyleSheet("QTreeView::item {background-color: red}" //для всех строкproxyView->setStyleSheet("QTreeView::item.selected {background-color: white;}"); //выделенная строка
OkButton = new QPushButton;connect (OkButton, SIGNAL (clicked()), this, SLOT(changeRow()));
void changeRow(){proxyView->setStyleSheet ("QTreeView::item:selected {background-color: green;}"); //это работает не правильно, цвет выделяемой строки изменяется на зеленый}
void changeRow(){QModelIndex indexRow = proxyView->currentIndex();proxyModel->setData(indexRow, QColor (Qt::red), Qt::BackgroundColorRole);}
void changeRow(){QModelIndex indexRow = proxyView->currentIndex();proxyModel->data(indexRow,Qt::BackgroundColorRole);}
C++ (Qt)class MyModel : ...{ ...public: void addGreenIndex(const QModelIndex &index) { _greenIndeces += index; }private: QSet _greenIndeces;}; QVariant MyModel::data(const QModelIndex &index, int role) const{ switch (role) { case Qt::BackgroundRole: if (_greenIndeces.contains(index)) return QColor(Qt::green); }} ... void MyWidget::changeRow(){ _model->addGreenIndex(proxyView->currentIndex());}
public: void addGreenIndex(const QModelIndex &index) { _greenIndeces += index; } //'_greenIndeces was not declared in this scope //In member function 'void MyModel::addGreenIndex(const QModelIndex&)':private: QSet _greenIndeces; //invalid use of template-name 'QSet' without an argument list
#include <QSet> // подключен заголовокpublic: void addGreenIndex(const QModelIndex &index) { greenIndeces += index; }private: QSet greenIndeces;
QVariant MyModel::data(const QModelIndex &index, int role) const{ switch (role) { case Qt::BackgroundRole: if (greenIndeces.contains(index)) return QColor(Qt::green); }}
void MyWidget::changeRow(){ model->addGreenIndex(proxyView->currentIndex());}
C++ (Qt)QSet<QModelIndex> greenIndeces;