在QTreeView中为行绘制焦点

时间:2014-11-10 15:48:03

标签: qt

我必须为我的项目使用QTableView,现在我必须为所选行绘制焦点。我必须为行绘制一个虚线矩形。但我不知道如何做到这一点。

你可以帮帮我吗? 非常感谢您的回答。

1 个答案:

答案 0 :(得分:1)

使用自定义委托:

部首:

#ifndef ITEMDELEGATEPAINT_H
#define ITEMDELEGATEPAINT_H

#include <QAbstractItemDelegate>
#include <QStyledItemDelegate>

class ItemDelegatePaint : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit ItemDelegatePaint(QObject *parent = 0);


protected:
    void paint( QPainter *painter,
                const QStyleOptionViewItem &option,
                const QModelIndex &index ) const;
    QSize sizeHint( const QStyleOptionViewItem &option,
                    const QModelIndex &index ) const;
    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;

signals:

public slots:

};

.cpp的:

#include "itemdelegatepaint.h"
#include <QPainter>
#include <QLineEdit>
#include <QDebug>

ItemDelegatePaint::ItemDelegatePaint(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

void ItemDelegatePaint::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QString txt = index.model()->data( index, Qt::DisplayRole ).toString();

    if( option.state & QStyle::State_Selected )
    {
        painter->setPen(QPen(QBrush(Qt::red),1,Qt::DotLine));
        painter->drawRect(option.rect);
        painter->drawText(option.rect,txt);
    }
    else
    {
        QStyledItemDelegate::paint(painter, option, index);
    }

}

QSize ItemDelegatePaint::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize( 45, 15 );
}

QWidget *ItemDelegatePaint::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QLineEdit *editor = new QLineEdit(parent);
    return editor;
}


void ItemDelegatePaint::setEditorData(QWidget *editor,
                                 const QModelIndex &index) const
{
    QString value =index.model()->data(index, Qt::EditRole).toString();
    QLineEdit *line = static_cast<QLineEdit*>(editor);
    line->setText(value);
}


void ItemDelegatePaint::setModelData(QWidget *editor,
                                QAbstractItemModel *model,
                                const QModelIndex &index) const
{
    QLineEdit *line = static_cast<QLineEdit*>(editor);
    QString data = line->text();
    model->setData(index, data);
}


void ItemDelegatePaint::updateEditorGeometry(QWidget *editor,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

用法:

ui->tableView->setItemDelegate(new ItemDelegatePaint);

结果:

enter image description here