验证 QTableView 中的用户输入 [英] Validating user input in a QTableView

查看:63
本文介绍了验证 QTableView 中的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTableView 并且我想验证用户输入.如果用户在 QTableView 的单元格中插入无效值,我想突出显示该单元格并禁用 QPushButton.

I have a QTableView and I want to validate user input. If user insert an invalid value in a cell of the QTableView, I want to highlight that cell and disable a QPushButton.

我怎样才能做到这一点?我可以使用 QValidator 吗?

How can I achieve this? Can I use QValidator?

推荐答案

是的,你可以这样做,为此目的使用自定义 QItemDelegate(我使用了 QIntValidator 就像示例).

Yes, you can do this, use custom QItemDelegate for this purpose (I used QIntValidator just as example).

标题:

#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H

#include <QItemDelegate>

class ItemDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    explicit ItemDelegate(QObject *parent = 0);

protected:
    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:

};

#endif // ITEMDELEGATE_H

每次通话费用

#include "itemdelegate.h"
#include <QLineEdit>
#include <QIntValidator>

ItemDelegate::ItemDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget *ItemDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QLineEdit *editor = new QLineEdit(parent);
    editor->setValidator(new QIntValidator);
    return editor;
}


void ItemDelegate::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 ItemDelegate::setModelData(QWidget *editor,
                                QAbstractItemModel *model,
                                const QModelIndex &index) const
{
    QLineEdit *line = static_cast<QLineEdit*>(editor);
    QString value = line->text();
    model->setData(index, value);
}


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

用法:

#include "itemdelegate.h"
//...
ItemDelegate *itDelegate = new  ItemDelegate;
ui->tableView->setItemDelegate(itDelegate);

在这种情况下用户将无法输入错误的数据,但您可以使用next:

In this case user will not be able input wrong data, but you can use next:

void ItemDelegate::setModelData(QWidget *editor,
                                QAbstractItemModel *model,
                                const QModelIndex &index) const
{
    QLineEdit *line = static_cast<QLineEdit*>(editor);

    QIntValidator validator;
    int pos = 0;
    QString data = line->text();
    if(validator.validate(data,pos) != QValidator::Acceptable)
    {
        qDebug() << "not valid";//do something
    }
    else
    {
        model->setData(index, data);
    }
}

但在这种情况下不要忘记从代码中删除 editor->setValidator(new QIntValidator);

But in this case don't forget remove editor->setValidator(new QIntValidator); line from your code

这篇关于验证 QTableView 中的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆