I have a QTableWidget. In its cells I need to display 3-state QSliders, that must change their color, depending form their state. -1 = red, 0 - normal, 1 - green. I tried to set QPalette to QSlider - whitout success. I tried to place QSlider into QWidget with Layout and apply palette to QWidget - whitout success. How to do that? I need any color sign (border or full background, e.t.c) How to do that?
Asked
Active
Viewed 1,105 times
0
-
do you want to show (piece of code) how you have tried? so, we can see what you did. – Lwin Htoo Ko Oct 22 '12 at 09:43
-
This is for label. The background color is sill white. QLabel *label = new QLabel(this); QPalette palette = label->palette(); palette.setColor(label->backgroundRole(), Qt::green); label->setText("aaa"); label->setPalette(palette); this->ui->tableWidgetSwitches->setCellWidget(0, 1, label); – Sacha_D Oct 22 '12 at 11:16
1 Answers
1
You can use QItemDelegate
, then you'll could to rule your QSlider
into QTableWidget
.
Detail.
At first, you should derive from QItemDelegate
. A good docs presents by doc.qt.digia
example using qitemdelegate
You should substitute QSpinBox
to QSlider
. And after reading this document, you can do needed with setting color your QSlider
.
QTableWidget *table = new QTableWidget(this);
table->setItemDelegateForColumn(index_column, delegate);
// or table->setItemDelegateForRow(index_row, delegate);
// or table->setItemDelegate(delegate);
To editor
was opened always, you should use openPersistentEditor()
. For example:
QTableWidgetItem *item = new QTableWidgetItem;
table->insertRow(row);
table->setItem(row, index_your_delegate, item);
table->openPersistentEditor(item);

Ruu
- 1,245
- 9
- 9
-
-
Is this spinBox appears only when cell get focus? I need that they keep their colors without focus, when they not selected. – Sacha_D Oct 22 '12 at 12:28
-
Sacha_D, You should sets `openPersistentEditor()` method from QTableWidget. – Ruu Oct 22 '12 at 13:16
-
Оно красится функцией item->setBackgroundColor(Qt::green); QPalette к QSlider и к QWidget не работает. Как связать сигнал объекта, создаваемого в QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; со слотом на форме? – Sacha_D Oct 23 '12 at 05:22
-
I created this like example "Using qitemdelegate", but I didn't implement void setEditorData(QWidget *editor, const QModelIndex &index) const; Also, as all editors are always opened, commit signal never emitting automatcally. In class, that derived from QitemDelegate, I add int rows, columns, that obtained form tableWidget and In the constructor create an array of QPointer
[rows*columns]. Then, in reimplemented function createEditor, I connect slider signal void valueChanged(int) to SLOT, in which I emit commit signal for call reimplemented setModelData function. – Sacha_D Oct 24 '12 at 05:02