1

I extended the qstyleditemview class. When I am in the editing mode for the qtreeview item, the paint method seems not to be executing right. When I change state to QStyle::State_Selected it works - it paints the selected row (text) in the qtreeview.

Any idea why it is not working in editing mode?

void myQItemDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
    QStyleOptionViewItem s = *qstyleoption_cast<const QStyleOptionViewItem*>(&option);

    if(s.state & QStyle::State_Editing) 
    {
        painter->fillRect(s.rect, s.palette.highlight());
        s.palette.setColor(QPalette::HighlightedText, QColor(Qt::blue));
    }
    QStyledItemDelegate::paint(painter, s, index);
}
ttulka
  • 10,309
  • 7
  • 41
  • 52
Tom
  • 21
  • 3

2 Answers2

1

In the State_Editing state the editor that is a widget created in createEditor() method is opened so that it will not be affected by the QStyleOptionViewItem palette.

Also instead of overwriting the paint method, use the initStyleOption() method:

#include <QtWidgets>

class StyledItemDelegate: public QStyledItemDelegate
{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QWidget * widget = QStyledItemDelegate::createEditor(parent, option, index);
        QPalette pal(widget->palette());
        pal.setColor(QPalette::HighlightedText, QColor(Qt::blue));
        pal.setBrush(QPalette::Highlight, option.palette.highlight());
        widget->setPalette(pal);
        return  widget;
    }
protected:
    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override{
        QStyledItemDelegate::initStyleOption(option, index);
        option->palette.setColor(QPalette::HighlightedText, QColor(Qt::blue));
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTreeView w;
    QStandardItemModel model;
    w.setModel(&model);
    w.setItemDelegate(new StyledItemDelegate);
    for(int i=0; i<3; ++i){
        auto it = new QStandardItem(QString::number(i));
        model.appendRow(it);
        for (int j=0; j<4; ++j) {
            it->appendRow(new QStandardItem(QString("%1-%2").arg(i).arg(j)));
        }
    }
    w.expandAll();
    w.show();
    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

Thanks for helping me out.

I understand now. I added code in QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const metod to style QLineEdit. I am trying to get the same background color for QLineEdit when it is created- when I am editing qtreeview item. The problem is when I select the item in qtreeview the whole row gets colored. That's ok. Now when I edit the item for example to change the text in qtreeview item, only the text part gets selected and colored with the same color as previous row selection color. The rest of the QLineEdit is white. In editing mode I would like to color the whole row which is edited with the same color. I could as obviously from my code color it with RGB but I don't know the exact RGB values. Is there any way to get the exact RGB color from item selection and then use it in

pal.setColor(QPalette::Highlight,QColor(qRgb(0,0,255)));

my code:

       QWidget* myQItemDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
    {
    QLineEdit *lineEdit = new QLineEdit(parent);
    connect(lineEdit,SIGNAL(editingFinished()),this,SLOT(commitAndCloseEditor()));

            QPalette pal;
            pal.setColor(QPalette::HighlightedText, QColor(Qt::white));
            pal.setColor(QPalette::Highlight,QColor(qRgb(0,0,255)));
            lineEdit->setPalette(pal);

    lineEdit->setFrame(false);

    return lineEdit;
    }

Thanks, Tom

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
Tom
  • 21
  • 3