qt - 使用 QStyledItemDelegate 子类在 QTableView 中创建 PushButtons

标签 qt qtableview qitemdelegate

我有完全相同的problem ,但我将使用 QTableView 小部件。我读了this并且想知道我是否可以覆盖 createEditor 函数以使用例如 QFileDialog 来获取新数据。

如果可能的话,谁能给我一个例子来实现这样一个 QItemDelegate 的子类。

如果没有,任何人都可以提供一个示例来实现 QItemDelegate 的子类,女巫可以在 QLineEdit 旁边绘制一个按钮来获得功能 here .

编辑:也许这个问题真的很愚蠢,我没有意识到,因为我离开这个项目大约半年。

第二:从 Qt 5.7 更新到 5.8 是否安全?

最佳答案

我已经尽力了,这是我的解决方案。 QStyledItemDelegate 子类的代码大部分来自here .

解决方案图片

但是,我很想解决一件事:(也许有人可以帮助我并发表评论)

  • QPixmap::grabWidget 已弃用,请改用 QWidget::grab() 但看起来 QWidget::grab() 不是用于此目的的正确解决方案.

foo.h:

#ifndef LIBRARYITEMDELEGATE_H
#define LIBRARYITEMDELEGATE_H

#include <QStyledItemDelegate>
#include <QWidget>
#include <QPushButton>
#include <QTableView>

class LibraryItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit LibraryItemDelegate(QObject *parent = 0);
    ~LibraryItemDelegate();

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
//    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *modal, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;

public slots:
    void cellEntered(const QModelIndex &index);

private:
    QTableView *myView;
    QPushButton *btn;
    bool isOneCellInEditMode;
    QPersistentModelIndex currentEditedCellIndex;
};

#endif // LIBRARYITEMDELEGATE_H

foo.cpp:

#include "libraryitemdelegate.h"

#include <QPainter>
#include <QStylePainter>

LibraryItemDelegate::LibraryItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
    if(QTableView *tableView = qobject_cast<QTableView*>(parent))
    {
        myView = tableView;
        btn = new QPushButton("...", myView);
        btn->hide();
        myView->setMouseTracking(true);
        connect(myView, SIGNAL(entered(QModelIndex)), this, SLOT(cellEntered(QModelIndex)));
        isOneCellInEditMode = false;
    }
}

LibraryItemDelegate::~LibraryItemDelegate(){}

void LibraryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.model()->headerData(index.column(), Qt::Horizontal, Qt::UserRole).toInt() == 1)
    {
        btn->setGeometry(option.rect);
        btn->setText("...");
        if(option.state == QStyle::State_Selected)
        {
            painter->fillRect(option.rect, option.palette.highlight());
        }
        QPixmap map = QPixmap::grabWidget(btn);
        painter->drawPixmap(option.rect.x(), option.rect.y(), map);
    }
    else
    {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

//QSize LibraryItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
//{
//    // return the QSize of the item in Your view
//}

QWidget *LibraryItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.model()->headerData(index.column(), Qt::Horizontal, Qt::UserRole).toInt() == 1)
    {
        QPushButton *btn = new QPushButton(parent);
//        btn->setText(index.data().toString());
        btn->setText("...");
        return btn;
    }
    else
    {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void LibraryItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(index.model()->headerData(index.column(), Qt::Horizontal, Qt::UserRole).toInt() == 1)
    {
        QPushButton *btn = qobject_cast<QPushButton*>(editor);
//        btn->setProperty("data_value", index.data());
        btn->setProperty("data_value", "...");
        btn->setText("...");
    }
    else
    {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}

void LibraryItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(index.model()->headerData(index.column(), Qt::Horizontal, Qt::UserRole).toInt() == 1)
    {
        QPushButton *btn = qobject_cast<QPushButton*>(editor);
        model->setData(index, btn->property("data_value"));
    }
    else
    {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

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

void LibraryItemDelegate::cellEntered(const QModelIndex &index)
{
    if(index.model()->headerData(index.column(), Qt::Horizontal, Qt::UserRole).toInt() == 1)
    {
        if(isOneCellInEditMode)
        {
            myView->closePersistentEditor(currentEditedCellIndex);
        }
        myView->openPersistentEditor(index);
        isOneCellInEditMode = true;
        currentEditedCellIndex = index;
    }
    else
    {
        if(isOneCellInEditMode)
        {
            isOneCellInEditMode = false;
            myView->closePersistentEditor(currentEditedCellIndex);
        }
    }
}

实现:

QStandardItemModel *myModel; // This is in the Header file

myModel = new QStandardItemModel(0,2,this);
myModel->setHeaderData(1, Qt::Horizontal, 1, Qt::UserRole);
myModel->setHorizontalHeaderLabels(QStringList(tr("Pfad zu den bibliotheks Ordnern")));

// Set Model and delegate to the View
ui->tableView_pathes->setModel(myModel);
LibraryItemDelegate *delegate = new LibraryItemDelegate(ui->tableView_pathes);
ui->tableView_pathes->setItemDelegate(delegate);

// Stretch only the first column
ui->tableView_pathes->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
ui->tableView_pathes->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Fixed);

编辑:这是 tableView 中按钮的代码。用 connect(btn, SIGNAL(pressed()), this, SLOT(buttonPressed())); 连接 createEditor 中的信号,并设置为委托(delegate)提供对 QStandardItemModel 的引用。

void LibraryItemDelegate::buttonPressed()
{
    QString dir = QFileDialog::getExistingDirectory(new QWidget(), tr("Wähle die bibliotheks Ordner"), "/home", QFileDialog::ShowDirsOnly);

    qDebug() << "Test: " << dir;

    if(!dir.isEmpty())
    {
        QModelIndex ind = currentEditedCellIndex.model()->index(currentEditedCellIndex.row(), 0);
        myModel->setData(ind, dir, Qt::DisplayRole);
    }
}

关于qt - 使用 QStyledItemDelegate 子类在 QTableView 中创建 PushButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43082419/

相关文章:

c++ - Qt - 清除 QTableView 的内容

python - PyQt 中复选框的 ListView

c++ - 为什么按下 "Tab"键只会发出 QEvent::ShortcutOverride 事件?

qt - 如何在Qt中将一个小部件放置在另一个上

qt - OpenGL资源共享策略

qt - 暂时禁用 QTableView 的 UI 更新

user-interface - 如何在QTableView中触发项目的编辑模式?

c++ - 如何为 QTreeView 中的特定行创建自定义 QItemDelegate?

c++ - 如何在 Qt C++ 中向自定义控件添加属性?

c++ - Qt/SQL - 从没有记录的表中获取列类型和名称