Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QInputDialog::getText() and QRegExp

[Solved] QInputDialog::getText() and QRegExp

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tilsitt
    wrote on last edited by
    #1

    Hi,

    I am using QInputDialog::getText() to get a string from the user. I would like to add some constraints on the string, using a QRegExp, but according to the doc, there is no such feature with QInputDialog (Qt::InputMethodHints doesn't fit my needs). What is the better way to do such a thing?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Serenity
      wrote on last edited by
      #2

      I think, you should use QRegExp by your input object (e.g. QLineEdit, QTextEdit).
      It contrains the editing of the object, so the user cannot type in wrong characters

      EDIT: Another way is to check, if the string is in the right format:
      [code] QRegExp rx("*.txt");
      rx.setPatternSyntax(QRegExp::Wildcard);
      rx.exactMatch("README.txt"); // returns true
      rx.exactMatch("welcome.txt.bak"); // returns false[/code]
      See also: http://qt-project.org/doc/qt-4.8/qregexp.html

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        The way to go is probably to catch the close event and check with a QRegExp the stuff that is inserted (like Serenity does). When the string is oke (or empty to escape it) you can accept the close event. If not you could ignore the event with maybe an information box on top to indicate that the user should do it right this time. Or maybe catch some other event or signal (inputchanged or something).
        Greetz

        Greetz, Jeroen

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tilsitt
          wrote on last edited by
          #4

          Thx for the reply, I will investigate them.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Did you consider to write your own input dialog using a QRegExpValidator on the line edit ?

            Hope it helps

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • jazzycamelJ Offline
              jazzycamelJ Offline
              jazzycamel
              wrote on last edited by
              #6

              SGaist was just touch quicker than me, I've actually done this before as below:

              regexpinputdialog.h
              @
              #ifndef REGEXPINPUTDIALOG_H
              #define REGEXPINPUTDIALOG_H

              #include <QDialog>

              class QLabel;
              class QLineEdit;
              class QDialogButtonBox;
              class QRegExpValidator;

              class RegExpInputDialog : public QDialog
              {
              Q_OBJECT
              public:
              explicit RegExpInputDialog(QWidget *parent = 0, Qt::WindowFlags=0);

              void setTitle(const QString &title);
              void setLabelText(const QString &label);
              void setText(const QString &text);
              void setRegExp(const QRegExp &regExp);
              
              QString getLabelText();
              QString getText();
              
              static QString getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp &regExp, bool *ok, Qt::WindowFlags flags=0);
              

              signals:

              private slots:
              void checkValid(const QString &text);

              private:
              QLabel *label;
              QLineEdit *text;
              QDialogButtonBox *buttonBox;
              QRegExp regExp;
              QRegExpValidator *validator;
              };

              #endif // REGEXPINPUTDIALOG_H
              @

              regexpinputdialog.cpp
              @
              #include <QtGui>
              #include "regexpinputdialog.h"

              RegExpInputDialog::RegExpInputDialog(QWidget *parent, Qt::WindowFlags flags) :
              QDialog(parent)
              {
              if(flags!=0) setWindowFlags(flags);

              QVBoxLayout *l=new QVBoxLayout(this);
              
              label=new QLabel(this);
              
              regExp=QRegExp("*");
              regExp.setPatternSyntax(QRegExp::Wildcard);
              validator=new QRegExpValidator(regExp);
              
              text=new QLineEdit(this);
              text->setValidator(validator);
              connect(text, SIGNAL(textChanged(QString)), this, SLOT(checkValid(QString)));
              
              buttonBox=new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, Qt::Horizontal, this);
              connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
              connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
              
              l->addWidget(label);
              l->addWidget(text);
              l->addWidget(buttonBox);
              

              }

              void RegExpInputDialog::setTitle(const QString &title){ setWindowTitle(title); }
              void RegExpInputDialog::setLabelText(const QString &label){ this->label->setText(label); }
              void RegExpInputDialog::setText(const QString &text){ this->text->setText(text); }

              void RegExpInputDialog::setRegExp(const QRegExp &regExp){
              validator->setRegExp(regExp);
              checkValid(text->text());
              }

              QString RegExpInputDialog::getLabelText(){ return label->text(); }
              QString RegExpInputDialog::getText(){ return text->text(); }

              void RegExpInputDialog::checkValid(const QString &text){
              QString _text=QString(text);
              int pos=0;
              bool valid=validator->validate(_text, pos)==QRegExpValidator::Acceptable;
              buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
              }

              QString RegExpInputDialog::getText(QWidget *parent, const QString &title, const QString &label, const QString &text, const QRegExp &regExp, bool *ok, Qt::WindowFlags flags){
              RegExpInputDialog *r=new RegExpInputDialog(parent, flags);
              r->setTitle(title);
              r->setLabelText(label);
              r->setText(text);
              r->setRegExp(regExp);
              *ok=r->exec()==QDialog::Accepted;
              if(*ok) return r->getText();
              else return QString();
              }
              @

              Usage example
              @
              ...
              bool ok;
              QRegExp regExp("*.txt");
              regExp.setPatternSyntax(QRegExp::Wildcard);
              QString text=RegExpInputDialog::getText(this, "Title", "Label", "Text", regExp, &ok);
              qDebug() << "Text:" << text;
              ...
              @

              Hope this helps ;o)

              For the avoidance of doubt:

              1. All my code samples (C++ or Python) are tested before posting
              2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
              1 Reply Last reply
              0
              • T Offline
                T Offline
                tilsitt
                wrote on last edited by
                #7

                Thx jazzycamel, that's exactly what I want to achieve :) .

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved