QTableWidget:仅通过委托的数字

QTableWidget: Only numbers via delegate

本文关键字:数字 QTableWidget      更新时间:2023-10-16

我目前正在尝试让我的QTableWidget只显示数字。我读到我需要一个QAbstractItemDelegate来这样做,所以我通读了文档,发现createEditor无效。这是我目前使用的代码:

#include "tabledelegate.h"
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* TableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
    QLineEdit* editor = new QLineEdit(parent);
    QDoubleValidator* val = new QDoubleValidator(editor);
    val->setBottom(0);
    val->setNotation(QDoubleValidator::StandardNotation);
    editor->setValidator(val);
    return editor;
}

我试图通过在 MainWindow 的构造函数中执行此操作来调用委托:

ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget));

但它给了我这个错误:

调用 'QTableWidget::setItemDelegate(TableDelegate*)' 时没有匹配函数 ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget)); ^

为什么?

您可以通过子类化 QTableWidgetItem 并重写 setData 方法来实现此行为,而不是使用项委托:

void my_table_item::setData(int role, const QVariant& value)
{
    if(role == Qt::EditRole)
    {
        QObject* parent = 0; // reference call needed for linux
        QRegExpValidator rxv(QRegExp("[+-]?\d*\.?\d+"), parent);
        int pos = 0;
        QString tmp = value.toString(); // reference call needed by linux
        if(rxv.validate(tmp,pos) != QValidator::Acceptable) { return; }
    }
    this->QTableWidgetItem::setData(role, value);
}

注意:仅当用户按 Enter 时验证输入,而不是在用户输入数据时验证。

我自己做的!我犯了一些错误:

  • 我忘了在我的头文件中子类QStyledItemDelegate。
  • 我忘了重新实现其他三个需要的函数setEditorData(),setModelData() 和 updateEditorGeometry()。
  • 我不得不运行一个QMake。

这是我的新来源:

//HEADER
#ifndef TABLEDELEGATE_H
#define TABLEDELEGATE_H
#include <QStyledItemDelegate>
class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
    TableDelegate(QObject* parent = 0);
    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;
};
#endif // TABLEDELEGATE_H

//.CPP

#include "tabledelegate.h"
#include <QLineEdit>
#include <QDoubleValidator>
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* TableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
    QLineEdit* editor = new QLineEdit(parent);
    QDoubleValidator* val = new QDoubleValidator(editor);
    val->setBottom(0);
    val->setNotation(QDoubleValidator::StandardNotation);
    editor->setValidator(val);
    return editor;
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    double value = index.model()->data(index,Qt::EditRole).toDouble();
    QLineEdit* line = static_cast<QLineEdit*>(editor);
    line->setText(QString().setNum(value));
}
void TableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
    QLineEdit* line = static_cast<QLineEdit*>(editor);
    QString value = line->text();
    model->setData(index,value);
}
void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

感谢任何帮助过的人。