QFileDialog and QLineEdit



  • Hello !

    I have 10 QLineEdit fields and 1 button which open 1 QFileDialog. When I select 1 file with the QFileDialog, the access directory is written in the first QLineEdit. After, if I want to select another file with the same button and write his access directory in the second QLineEdit, I verify if the first line is empty or not. But it doesn't work and I don't understand why. Here is my code.

    if (lined0.text().isEmpty())
            QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined0] {lined0.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)")));});
        else
            QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined1] {lined1.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)")));});
    


  • Hi , According ur question and sample code , this line " if (lined0.text().isEmpty()) " , is every time will check QLineEdit lined0 is empty . if lined0 is not empty .. it will go for next lined1.



  • Like this code ?

    if (!(lined0.text().isEmpty()))
            QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined1] {lined1.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)")));});
        else
            QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined0] {lined0.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (*.mp3)")));});
    


  • Can u try like this
    if (lined0.text().isEmpty())
    QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined0] {lined0.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (.mp3)")));});
    else if(!lined0.text().isEmpty()&& lined1.text().isEmpty())
    QObject::connect(&buttonetced, &QPushButton::clicked, [&windowed, &lined1] {lined1.setText(QFileDialog::getOpenFileName(&windowed, QObject::tr("Sélectionner un fichier son ..."), "C:/", QObject::tr("Audio (
    .mp3)")));});


  • Lifetime Qt Champion

    Hi,

    Your connect statement shouldn't depend on your QLineEdits state.

    Just connect the button once and then check which of the QLineEdit is empty once you return from the call to getOpenFileName.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.