SOLVED - Loop thru QLists?
-
wrote on 11 Apr 2014, 23:10 last edited by
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
}@ -
wrote on 12 Apr 2014, 01:09 last edited by
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]);
}@ -
wrote on 12 Apr 2014, 10:43 last edited by
[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.
-
Hi,
Then what has failed ?
-
wrote on 12 Apr 2014, 14:16 last edited by
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.
1/5