【问题标题】:qstyleditemdelegate subclassing paint method not working rightqstyleeditemdelegate 子类化绘制方法无法正常工作
【发布时间】:2019-03-08 16:23:26
【问题描述】:

我扩展了qstyleditemview 类。当我处于qtreeview 项目的编辑模式时,paint 方法似乎没有正确执行。当我将状态更改为QStyle::State_Selected 时,它会起作用 - 它会在qtreeview 中绘制选定的行(文本)。

知道为什么它不能在编辑模式下工作吗?

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

【问题讨论】:

    标签: qt paint editing qitemdelegate qstyleditemdelegate


    【解决方案1】:

    State_Editing 状态下,作为在createEditor() 方法中创建的小部件的编辑器被打开,因此它不会受到QStyleOptionViewItem 调色板的影响。

    也不要覆盖paint方法,而是使用initStyleOption()方法:

    #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();
    }
    

    【讨论】:

      【解决方案2】:

      感谢您帮助我。

      我现在明白了。我在 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 方法中添加了代码来设置 QLineEdit 的样式。我试图在创建 QLineEdit 时获得相同的背景颜色 - 当我编辑 qtreeview 项目时。问题是当我在 qtreeview 中选择项目时,整行都会被着色。没关系。现在,当我编辑项目以更改 qtreeview 项目中的文本时,只有文本部分被选中并使用与前一行选择颜色相同的颜色着色。 QLineEdit 的其余部分是白色的。在编辑模式下,我想用相同的颜色为编辑的整行着色。我可以很明显地从我的代码中用 RGB 着色它,但我不知道确切的 RGB 值。有什么方法可以从项目选择中获取确切的 RGB 颜色,然后在中使用它

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

      我的代码:

             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;
          }
      

      谢谢,汤姆

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        • 2010-11-02
        • 1970-01-01
        • 2019-06-02
        • 2021-12-29
        • 2014-09-29
        相关资源
        最近更新 更多