PDA

View Full Version : How to extract data from QFormLayout?



szisziszilvi
16th May 2011, 10:42
Hi,

I would like to use QFormLayout for a typical form-like widget and my problem is that I don't know to extract any data from my form. Now I just made a small example for learning purposes, there I have this:
class MainWindow : public QMainWindow

{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

QFormLayout * formlo;
QGroupBox *formGroupBox;
};

and this:


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

this->formlo = new QFormLayout;
this->formlo->addRow(new QLabel("Elso sor:"),new QLineEdit);
formGroupBox = new QGroupBox(tr("Form layout"));
formGroupBox->setLayout(formlo);

ui->formLayout->addWidget(formGroupBox);
}


which works perfect, but no form is without purpose, so now I can't find the way how to extract the user-given input data from the QLineEdit. I thought maybe itemAt() could be my friend, but I'm not that sure about that, I'm a bit confused actually. Is there maybe a good built-in example? The "Basic Layouts" one gives hints only for displaying.

dholliday
16th May 2011, 12:18
I can answer your question. But I have to say I personally don't think this is a very good way of designing a form. Mainly because you are not naming any of your controls.
You are just initalising them on the fly.

Anyway you can find the value by doing the following



QList<QLineEdit *> allLineEdits = this->findChildren<QLineEdit *>(); //Return all LineEdit controls that have been created.

if (allLineEdits.count() > 0){
for (int i =0; i < allLineEdits.count(); i++)
{
QString myValue = allLineEdits.at(i)->text();
}
}

This will work for your example. But as soon as you get more than one LineEdit on your form you will need to find some way of identifying what control you are on.
This code will out put the values for all lineedits.

Hope this helps.

Dan

szisziszilvi
16th May 2011, 12:40
Thanks for the hint.
This type of designing the for was "stolen" from the example "Basic Layouts". However your advice to name the textedits sounds of couse much better, I think in the real program I will follow that one. And furthermore if I think it over, in that case getting its text value needs clearly no special trick. :cool:

Zlatomir
16th May 2011, 13:16
That example seems to be an odd one - it uses a ui file that doesn't seem to do anything (since you code the gui) and it just add "stuff" to layout without naming the "stuff" so that you can use the name to access the gui elements.

I suggest you learn both ways to code the gui (designer and usage of ui file or code the layouts manually).

Anyway to correct the "design" you can declare pointers to the QLabel and to the QLineEdit in the class header (members your class) and then write this:



label = new QLabel("Elso sor:"); //declared i header: QLabel* label;
lineEdit = new QLineEdit; //declared: QLineEdit* lineEdit;

this->formlo->addRow(label, lineEdit);


Then you can use the pointer directly (without casting), example to get the QString from lineEdit:


QString textFromLineEdit = lineEdit->text();

dholliday
16th May 2011, 14:04
I agree. That is the correct way of doing it.

szisziszilvi
17th May 2011, 09:18
yes, this is exactly what I did after dhollyday's post, but it is still a good thing that you wrote here the correct method.