【问题标题】:QT - QInputDialog How to Validate?QT - QInputDialog 如何验证?
【发布时间】:2023-04-09 18:31:01
【问题描述】:

我想向我的 QInputDialog 添加某种类型的验证。我使用对话框的输入来创建文件系统路径。所以我想排除 @$#%^&*() 等字符,但保留 - 和 _。我正在考虑应用正则表达式模式,但我不确定工作流程。

如果不可能或使用不同的东西有意义,我也愿意。

这是我目前正在使用的:

QString defaultText("whatever");
bool ok;
QString caseInput = QInputDialog::getText(this, tr("Input Text"), tr("New Text:"), QLineEdit::Normal, defaultText, &ok);

if (ok && !caseInput.isEmpty())
{
   // do stuff
}

【问题讨论】:

    标签: c++ regex qt validation dialog


    【解决方案1】:

    因此,如果您想完全控制它,您将需要创建自己的QDialog,为文本添加QLabel,然后添加行编辑,设置QValidator,然后访问返回之后的价值。

    像这样:

    mydialog.h

    #include <QDialog>
    #include <QLineEdit>
    
    class MyDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        MyDialog(QWidget *parent = 0);
        ~MyDialog();
        QString getNewValue();
    
    signals:
        //void rejected();
        //void accepted();
    
    public slots:
    
    
    private:
        QLineEdit * le;
    };
    

    mydialog.cpp

    #include "mydialog.h"
    #include <QDialogButtonBox>
    #include <QRegExpValidator>
    #include <QLineEdit>
    #include <QVBoxLayout>
    #include <QLabel>
    
    MyDialog::MyDialog(QWidget *parent)
        : QDialog(parent)
    {
        le = 0;
        this->setAttribute(Qt::WA_QuitOnClose, false);
    
        QVBoxLayout * vbox = new QVBoxLayout;
    
        vbox->addWidget(new QLabel(tr("Type in your text:")));
    
        le = new QLineEdit();
    
        // le->setText(tr("Profile"));
        // le->selectAll();
        le->setPlaceholderText(tr("Profile"));
    
        vbox->addWidget(le);
    
        QRegExpValidator * v = new QRegExpValidator(QRegExp("[\\w\\d_ \\.]{24}"));
        le->setValidator(v);
    
    
        QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
            | QDialogButtonBox::Cancel);
        vbox->addWidget(buttonBox);
        this->setLayout(vbox);
    
         // connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(accepted()));
         // connect(buttonBox, SIGNAL(rejected()), this, SIGNAL(rejected()));
    }
    
    MyDialog::~MyDialog()
    {
    
    }
    
    QString MyDialog::getNewValue()
    {
            return le->text();
    }
    

    示例用法:

    MyDialog dialog;
    if(dialog.exec() == QDialog::Accepted)
    {
        QString retVal = dialog.getNewValue();
        qDebug() << "Dialog value:" << retVal;
    }
    

    实现几乎相同目的的另一种方法:

    http://qt-project.org/doc/qt-4.8/qlineedit.html#inputMask-prop http://qt-project.org/doc/qt-4.8/widgets-lineedits.html

    如果你想使用股票getTextQInputDialog你可以设置InputMethodHint的字段:

    http://qt-project.org/doc/qt-4.8/qinputdialog.html#getText

    http://qt-project.org/doc/qt-4.8/qt.html#InputMethodHint-enum

    QRegExp 在我看来是最强大的。

    以下是本课程中QRegExp 的一些很好的示例:

    http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter-highlighter-cpp.html

     classFormat.setFontWeight(QFont::Bold);
     classFormat.setForeground(Qt::darkMagenta);
     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
     rule.format = classFormat;
     highlightingRules.append(rule);
    
     singleLineCommentFormat.setForeground(Qt::red);
     rule.pattern = QRegExp("//[^\n]*");
     rule.format = singleLineCommentFormat;
     highlightingRules.append(rule);
    
     multiLineCommentFormat.setForeground(Qt::red);
    
     quotationFormat.setForeground(Qt::darkGreen);
     rule.pattern = QRegExp("\".*\"");
     rule.format = quotationFormat;
     highlightingRules.append(rule);
    
     functionFormat.setFontItalic(true);
     functionFormat.setForeground(Qt::blue);
     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
     rule.format = functionFormat;
     highlightingRules.append(rule);
    
     commentStartExpression = QRegExp("/\\*");
     commentEndExpression = QRegExp("\\*/");
    

    希望对您有所帮助。

    【讨论】:

    • connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())) 更酷。与被拒绝的信号相同。
    猜你喜欢
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多