SOLVED - Loop thru QLists?



  • Hi, so I have a function that I need to call of every line edit and checkbox. I tried to do it myself but failed a bit and need some help. This is what I got :

    ps. The amount of line edits and checkboxes are equal.
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QCheckBox"
    #include "QString"
    #include "QLineEdit"
    #include "QListIterator"

    void connectCheckBoxToLineEdit(QCheckBox *cb, QLineEdit *le);

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QList<QLineEdit> *lineEdits = ui->centralWidget->findChildren<QLineEdit();
    QList<QCheckBox> *checkBoxes = ui->centralWidget->findChildren<QCheckBox>();
    for(int i=0; i<=lineEdits.size(); i++){
        connectCheckBoxToLineEdit();
    }
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::connectCheckBoxToLineEdit(QCheckBox *cb, QLineEdit *le)
    {
    connect(le, &QLineEdit::textChanged, [cb](const QString &text) {
    cb->setEnabled(!text.isEmpty());
    });
    }

    void MainWindow::on_pushButton_clicked()
    {
    //SELECT DIRECTORY
    }

    void MainWindow::on_pushButtonOutput_clicked()
    {
    //GENERATE FILES
    }@



  • Your connectCheckBoxToLineEdit() function takes two parameters, but then you try to call it without any arguments?

    @for(int i=0; i<=lineEdits.size(); i++){
    connectCheckBoxToLineEdit();
    }@

    What about this:
    @const int len = qMin(checkBoxes.size(), lineEdits.size());
    for(int i=0; i <= len; i++)
    {
    connectCheckBoxToLineEdit(checkBoxes[i], lineEdits[i]);
    }@



  • [quote author="MuldeR" date="1397264982"]Your connectCheckBoxToLineEdit() function takes two parameters, but then you try to call it without any arguments?

    @for(int i=0; i<=lineEdits.size(); i++){
    connectCheckBoxToLineEdit();
    }@

    What about this:
    @const int len = qMin(checkBoxes.size(), lineEdits.size());
    for(int i=0; i <= len; i++)
    {
    connectCheckBoxToLineEdit(checkBoxes[i], lineEdits[i]);
    }@[/quote]

    Just did it as an example.


  • Lifetime Qt Champion

    Hi,

    Then what has failed ?



  • seems like you are using the findChildren method a little bit wrong?
    change
    @
    QList<QLineEdit> lineEdits = ui->centralWidget->findChildren<QLineEdit();
    @
    to
    @
    QList<QLineEdit
    > lineEdits = ui->centralWidget->findChildren<QLineEdit*>();
    @

    Also you have defined "connectCheckBoxToLineEdit" as a global function but then declare it as a member function of MainWindow. Maybe read some c++ book or tutorials first to learn the basics of c++, just saying you have some weird errors in that code.


Log in to reply
 

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