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. QInputDialog::getText, maxLenght String?
Forum Updated to NodeBB v4.3 + New Features

QInputDialog::getText, maxLenght String?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.0k Views 3 Watching
  • 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.
  • A Offline
    A Offline
    Ant02
    wrote on 10 Oct 2019, 14:11 last edited by
    #1

    Hello,

    I’m working on a Qt proget is my problem is the following:

    I am desperately trying to find a solution to limit the number of characters to enter in a "Qinputdialog::getText"

    With a Qlineedit, we use the maxLenght method, but the for "Qt Qinputdialog" I dry!

    void TW_S_Rck::renameTextBank()
        {
            bool ok = false;
            int index = listeDeroulanteBank->currentIndex();
            QString buff = nameBank[index];
     
            nameBank[index] = QInputDialog::getText(this,"Rename Bank","",QLineEdit::Normal, QString("%1").arg(nameBank[index]), &ok);
     
            if(ok && !nameBank[index].isEmpty()){listeDeroulanteBank->setItemText(index,nameBank[index]);}
            else{nameBank[index] = buff;}
        }
    

    Thank you in advance for your help.

    Regards, Antoine

    P J 2 Replies Last reply 10 Oct 2019, 14:40
    0
    • A Ant02
      10 Oct 2019, 14:11

      Hello,

      I’m working on a Qt proget is my problem is the following:

      I am desperately trying to find a solution to limit the number of characters to enter in a "Qinputdialog::getText"

      With a Qlineedit, we use the maxLenght method, but the for "Qt Qinputdialog" I dry!

      void TW_S_Rck::renameTextBank()
          {
              bool ok = false;
              int index = listeDeroulanteBank->currentIndex();
              QString buff = nameBank[index];
       
              nameBank[index] = QInputDialog::getText(this,"Rename Bank","",QLineEdit::Normal, QString("%1").arg(nameBank[index]), &ok);
       
              if(ok && !nameBank[index].isEmpty()){listeDeroulanteBank->setItemText(index,nameBank[index]);}
              else{nameBank[index] = buff;}
          }
      

      Thank you in advance for your help.

      Regards, Antoine

      P Offline
      P Offline
      Pl45m4
      wrote on 10 Oct 2019, 14:40 last edited by
      #2

      @Ant02

      Hi,

      have you tried to react on this signal? (https://doc.qt.io/qt-5/qinputdialog.html#textValueChanged)
      Whenever the text changes, you could check if the number of characters exceeds your threshold and then ignore any additional input.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • A Ant02
        10 Oct 2019, 14:11

        Hello,

        I’m working on a Qt proget is my problem is the following:

        I am desperately trying to find a solution to limit the number of characters to enter in a "Qinputdialog::getText"

        With a Qlineedit, we use the maxLenght method, but the for "Qt Qinputdialog" I dry!

        void TW_S_Rck::renameTextBank()
            {
                bool ok = false;
                int index = listeDeroulanteBank->currentIndex();
                QString buff = nameBank[index];
         
                nameBank[index] = QInputDialog::getText(this,"Rename Bank","",QLineEdit::Normal, QString("%1").arg(nameBank[index]), &ok);
         
                if(ok && !nameBank[index].isEmpty()){listeDeroulanteBank->setItemText(index,nameBank[index]);}
                else{nameBank[index] = buff;}
            }
        

        Thank you in advance for your help.

        Regards, Antoine

        J Offline
        J Offline
        JonB
        wrote on 10 Oct 2019, 14:51 last edited by
        #3

        @Ant02
        Because QInputDialog::getText() cannot be hooked to a QValidator --- which you could have on your own dialog --- if you want to stick with QInputDialog you must indeed do it as @Pl45m4 has written.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Ant02
          wrote on 10 Oct 2019, 15:12 last edited by
          #4

          Thank you for your answers,
          I will test "Qinputdialog:: textValueChanged", but how to analyze the readable tank number?

          Can an example be?
          Thank you

          M 1 Reply Last reply 10 Oct 2019, 15:39
          0
          • A Ant02
            10 Oct 2019, 15:12

            Thank you for your answers,
            I will test "Qinputdialog:: textValueChanged", but how to analyze the readable tank number?

            Can an example be?
            Thank you

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Oct 2019, 15:39 last edited by mrjj 10 Oct 2019, 15:39
            #5

            Hi

            Can an example be?

            Sure. You have to use the non static way/version of the Dialog.

               QInputDialog inp; // we make own dialog
               // set some of the common options
                inp.setWindowTitle("title");
                inp.setLabelText("label");
                inp.setTextValue("1234");
              // connect the signal
                connect(&inp, &QInputDialog::textValueChanged, this, [&inp](const QString text) {
                    const int maxtext = 4;
                    if (text.length() > maxtext) // check text and cap it. (you might want different)
                        inp.setTextValue( text.right(maxtext) );
                    qDebug() << text;
                });
            
                int ret = inp.exec(); // run it
            // check if user pressed ok
                if (ret == QDialog::DialogCode::Accepted )
                    qDebug() << "ok";
            

            You could wrap it up in your own GetText Function for easy reuse.

            A 1 Reply Last reply 10 Oct 2019, 20:45
            2
            • M mrjj
              10 Oct 2019, 15:39

              Hi

              Can an example be?

              Sure. You have to use the non static way/version of the Dialog.

                 QInputDialog inp; // we make own dialog
                 // set some of the common options
                  inp.setWindowTitle("title");
                  inp.setLabelText("label");
                  inp.setTextValue("1234");
                // connect the signal
                  connect(&inp, &QInputDialog::textValueChanged, this, [&inp](const QString text) {
                      const int maxtext = 4;
                      if (text.length() > maxtext) // check text and cap it. (you might want different)
                          inp.setTextValue( text.right(maxtext) );
                      qDebug() << text;
                  });
              
                  int ret = inp.exec(); // run it
              // check if user pressed ok
                  if (ret == QDialog::DialogCode::Accepted )
                      qDebug() << "ok";
              

              You could wrap it up in your own GetText Function for easy reuse.

              A Offline
              A Offline
              Ant02
              wrote on 10 Oct 2019, 20:45 last edited by
              #6

              @mrjj

              J'ai tester votre code, ca ne marche pas chez moi, j'ai les erreur suivante:

              void TW_S_Rck::renameTextBank()
                  {
                  /*
                      bool ok = false;
                      int index = listeDeroulanteBank->currentIndex();
                      QString buff = nameBank[index];
              */
                      QInputDialog inp; // we make own dialog
                         // set some of the common options
                          inp.setWindowTitle("title");
                          inp.setLabelText("label");
                          inp.setTextValue("1234");
                        // connect the signal
                          connect(&inp, &QInputDialog::textValueChanged, this, [&inp](const QString text) {
                              const int maxtext = 4;
                              if (text.length() > maxtext) // check text and cap it. (you might want different)
                              inp.setTextValue( text.right(maxtext) );
                              qDebug() << text;
                          });
              
                          int ret = inp.exec(); // run it
                      // check if user pressed ok
                          if (ret == QDialog::DialogCode::Accepted )
                              qDebug() << "ok";
                  }
              

              erreur : no matching function for call to 'TW_S_Rck::connect(QInputDialog*, void (QInputDialog::)(const QString&), TW_S_Rck const, TW_S_Rck::renameTextBank()::__lambda0)'

              erreur : template argument for 'template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType)' uses local type 'TW_S_Rck::renameTextBank()::__lambda0'

              erreur : 'QInputDialog::DialogCode' is not a class or namespace

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on 10 Oct 2019, 21:07 last edited by mpergand 10 Oct 2019, 21:11
                #7

                Ca fonctionne bien pour moi.
                Essaye de supprimer l'argument this
                connect(&inp, &QInputDialog::textValueChanged, this, [&inp](const QString text)
                Et si TW_S_Rck n'est pas un QObject, utilise la fonction statique QObject::connect

                A 1 Reply Last reply 10 Oct 2019, 21:33
                0
                • M mpergand
                  10 Oct 2019, 21:07

                  Ca fonctionne bien pour moi.
                  Essaye de supprimer l'argument this
                  connect(&inp, &QInputDialog::textValueChanged, this, [&inp](const QString text)
                  Et si TW_S_Rck n'est pas un QObject, utilise la fonction statique QObject::connect

                  A Offline
                  A Offline
                  Ant02
                  wrote on 10 Oct 2019, 21:33 last edited by
                  #8

                  @mpergand

                  J'ai tester sans le this, aucun résulta!
                  TW_S_Rck est un QObject.
                  void TW_S_Rck::renameTextBank() est un Slot. Est le problème?

                  le code avec les deux ligne en gras qui donne l'erreur

                  void TW_S_Rck::renameTextBank()
                      {
                  
                          bool ok = false;
                          int index = listeDeroulanteBank->currentIndex();
                          QString buff = nameBank[index];
                  
                          QInputDialog inp; // we make own dialog
                             // set some of the common options
                              inp.setWindowTitle("title");
                              inp.setLabelText("label");
                              inp.setTextValue("1234");
                            // connect the signal
                  
                              QObject::connect(&inp, &QInputDialog::textValueChanged, this,[&inp](const QString text) {
                                  const int maxtext = 4;
                                  if (text.length() > maxtext) // check text and cap it. (you might want different)
                                  inp.setTextValue( text.right(maxtext) );
                                  qDebug() << text;
                              **});**
                  
                              int ret = inp.exec(); // run it
                          // check if user pressed ok
                              **if (ret == QDialog::DialogCode::Accepted )**
                                  qDebug() << "ok";
                      }
                  
                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Ant02
                    wrote on 10 Oct 2019, 22:14 last edited by
                    #9

                    Après d'autre réponse, j'ai trouvé une solution qui fonction bien:

                    void 
                        {
                            QInputDialog *dialog = new QInputDialog(this);
                                dialog->setInputMode(QInputDialog::TextInput);
                                dialog->setWindowTitle("Rename Bank");
                                dialog->setLabelText("");
                                dialog->setTextValue(nameBank[index]);
                    
                                QLineEdit *lineEdit = dialog->findChild<QLineEdit *>();
                                lineEdit->setMaxLength(16);
                    
                                if (dialog->exec() == QDialog::Accepted)
                                {
                                    nameBank[index] =  dialog->textValue();
                                    listeDeroulanteBank->setItemText(index,nameBank[index]);
                                }
                                else
                                {
                                    // Saisie annulée
                                }
                        }
                    

                    Merci pour vos réponses rapide!
                    Cordialement, Antoine

                    1 Reply Last reply
                    0

                    1/9

                    10 Oct 2019, 14:11

                    • Login

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