Russian Qt Forum
Март 29, 2024, 09:28 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: Выбор цвета ячейки в QTableView (QT 4.8)  (Прочитано 2730 раз)
qtkoder777
Частый гость
***
Offline Offline

Сообщений: 245


Просмотр профиля
« : Октябрь 18, 2017, 13:19 »

Как сделать такое:
В столбце таблицы ячейки закрашены заданными цветами
Пользователь щёлкает по ячейке таблицы, вылезает QColorDialog, ячейка красится в выбранный цвет

Строго QTableView, а не QTableWidget.

Пишу делегата. После задания цвета мелькает нужный цвет и ячейка становится белой. При выборе другой ячейки предыдущая красится в выбранный цвет. Что не правильно?

Код
C++ (Qt)
class ColorDelegate : public QItemDelegate
{
Q_OBJECT
 
public:
ColorDelegate(QObject *parent = 0);
 
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  const QModelIndex &index) const;
 
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
  const QModelIndex &index) const;
 
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};

Код
C++ (Qt)
QWidget *ColorDelegate::createEditor(QWidget *parent,
   const QStyleOptionViewItem &/* option */,
   const QModelIndex & index ) const
{
QWidget* editor = new QWidget(parent);
QHBoxLayout* ly = new QHBoxLayout(editor);
editor->setAutoFillBackground(true);
QVariant value = index.model()->data(index, Qt::UserRole + 1);
QColor c = value.value<QColor>();
c = QColorDialog::getColor(c, 0, tr("Выбор цвета"), QColorDialog::ShowAlphaChannel);
QVariant newValue(c);
QAbstractItemModel* model = const_cast<QAbstractItemModel*>(index.model());
model->setData(index, newValue, Qt::UserRole + 1);
editor->setPalette(QPalette(c));
qApp->processEvents();
 
return editor;
}
 
void ColorDelegate::setEditorData(QWidget *editor,
                                   const QModelIndex &index) const
{
}
 
void ColorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                               const QModelIndex &index) const
{
}
 
void ColorDelegate::updateEditorGeometry(QWidget *editor,
   const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
   editor->setGeometry(option.rect);
}
 
void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
QVariant value = index.model()->data(index, Qt::UserRole + 1);
QColor c = value.value<QColor>();
painter->fillRect(option.rect, c);
}
 
« Последнее редактирование: Октябрь 18, 2017, 16:09 от qtkoder777 » Записан
Racheengel
Джедай : наставник для всех
*******
Offline Offline

Сообщений: 2679


Я работал с дискетам 5.25 :(


Просмотр профиля
« Ответ #1 : Октябрь 19, 2017, 09:25 »

QColorDialog::getColor надо вызывать в слоте, который клик обрабатывает, а не в createEditor.
Записан

What is the 11 in the C++11? It’s the number of feet they glued to C++ trying to obtain a better octopus.

COVID не волк, в лес не уйдёт
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.1 секунд. Запросов: 22.