Search code examples
qtcolorspaintqwidgetqslider

How to change color of QWidget in QTableWidget


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?


Solution

  • 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);