Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
19
Indexable
Never
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QObject>

class MainWindow: public QWidget 
{
  Q_OBJECT  

  public:
    MainWindow()
    {
      QLineEdit* userInput = new QLineEdit(this);

      QPushButton* loginBtn = new QPushButton("Login", this);
      QObject::connect(loginBtn, &QPushButton::clicked, this, [=]{
        this->validate(userInput);
      });

      QGridLayout* container = new QGridLayout(this);
      container->addWidget(userInput, 0, 0);
      container->addWidget(loginBtn, 1, 0);

      this->setLayout(container);
    }

  public slots:    
       void validate(QLineEdit* pUserInput)
       {
         QRegularExpression rx("^[^_\\W]+$");
         QValidator* validator = new QRegularExpressionValidator(rx, this);
         pUserInput->setValidator(validator);

         // what property to get the validate result? 
         // i need to pass to QMessageBox

         QMessageBox message;
         message.setText(validateResult);
         message.exec();
       }    
}

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

  MainWindow w;
  w.show();
    
  return app.exec();
}