如何为QTableWidget添加自动完成功能

问题描述 投票:1回答:1

我想做的是开始在表单元格中键入一些数据,它显示了完成建议,但到目前为止没有成功。

我尝试在单元格中添加QLineEdit,但是有没有一种方法可以不使用QLineEdit作为cellWidget?

编辑:

通过覆盖QItemDelegate类使之起作用

Autocomplete_Delegate.h


#include <QItemDelegate>
#include <QModelIndex>
#include <QLineEdit>
#include <QCompleter>

class Autocomplete_Delegate : public QItemDelegate {
public:
    Autocomplete_Delegate(QObject *parent, QStringList model);
    ~Autocomplete_Delegate();
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;

private:
    QStringList model;
};

Autocomplete_Delegate..cpp

Autocomplete_Delegate::Autocomplete_Delegate(QObject *parent, QStringList model) : QItemDelegate(parent), model(model) {}

Autocomplete_Delegate::~Autocomplete_Delegate() {
    model.clear();
}

QWidget *Autocomplete_Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QWidget *editor = QItemDelegate::createEditor(parent, option, index); //* Create the editor so it looks native to the tablewidget
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor); //* create a linedit so it behaves like a line edit and cast the editor to line edit
    QCompleter *completer = new QCompleter(model, parent); //* make a completer and pass in the wordlist
    completer->setCaseSensitivity(Qt::CaseInsensitive); //* set the case senstivity
    lineEdit->setCompleter(completer); //* set the completor on line edit
    return lineEdit;
}

void Autocomplete_Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    QString data = index.model()->data(index, Qt::EditRole).toString(); //* get the data from the model -> the cell
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    lineEdit->setText(data); //* set the data in the editor
}

感谢@eyllanesc的想法。

c++ qt qtablewidget
1个回答
0
投票

您可以使用与QTableWidgetItem相关联的角色,并使用委托来创建用于建立和更新模型的QCompleter。

#include <QtWidgets>

enum CustomRoles{
    ListCompleterRole = Qt::UserRole + 1000
};

class CompletedDelegate: public QStyledItemDelegate{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index);
        if(QLineEdit *le = qobject_cast<QLineEdit *>(editor)){
            QStringListModel *model = new QStringListModel(le);
            QCompleter *completer = new QCompleter(le);
            completer->setModel(model);
            le->setCompleter(completer);
        }
        return editor;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const{
        QStyledItemDelegate::setEditorData(editor, index);
        if(QLineEdit *le = qobject_cast<QLineEdit *>(editor)){
            if(QCompleter *completer = le->completer()){
                if(QStringListModel *model = qobject_cast<QStringListModel *>(completer->model())){
                    QVariant v = index.data(CustomRoles::ListCompleterRole);
                    if (v.canConvert<QStringList>()){
                        QStringList options = v.value<QStringList>();
                        model->setStringList(options);
                    }
                }
            }
        }
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QStringList words = {
        "astonishing", 
        "agreement", 
        "appeal", 
        "autonomy", 
        "accompany", 
        "articulate", 
        "article", 
        "amuse", 
        "advertise",
        "admiration"
    };

    QTableWidget w(1, 1);
    CompletedDelegate *delegate = new CompletedDelegate(&w);
    w.setItemDelegate(delegate);

    QTableWidgetItem *item = new QTableWidgetItem;
    item->setData(CustomRoles::ListCompleterRole, words); // update options
    w.setItem(0, 0, item);

    w.show();
    return a.exec();
}
© www.soinside.com 2019 - 2024. All rights reserved.