Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QInputDialog::getText, maxLenght String?



  • 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



  • @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.



  • @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.



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

    Can an example be?
    Thank you


  • Lifetime Qt Champion

    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.



  • @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



  • 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



  • @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";
        }
    


  • 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


Log in to reply