Results 1 to 6 of 6

Thread: How to extract data from QFormLayout?

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to extract data from QFormLayout?

    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
    Qt Code:
    1. {
    2. Q_OBJECT
    3.  
    4. public:
    5. explicit MainWindow(QWidget *parent = 0);
    6. ~MainWindow();
    7.  
    8. private:
    9. Ui::MainWindow *ui;
    10.  
    11. QFormLayout * formlo;
    12. QGroupBox *formGroupBox;
    13. };
    To copy to clipboard, switch view to plain text mode 

    and this:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. this->formlo = new QFormLayout;
    8. this->formlo->addRow(new QLabel("Elso sor:"),new QLineEdit);
    9. formGroupBox = new QGroupBox(tr("Form layout"));
    10. formGroupBox->setLayout(formlo);
    11.  
    12. ui->formLayout->addWidget(formGroupBox);
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Szilvi

  2. #2
    Join Date
    Jan 2010
    Posts
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to extract data from QFormLayout?

    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

    Qt Code:
    1. QList<QLineEdit *> allLineEdits = this->findChildren<QLineEdit *>(); //Return all LineEdit controls that have been created.
    2.  
    3. if (allLineEdits.count() > 0){
    4. for (int i =0; i < allLineEdits.count(); i++)
    5. {
    6. QString myValue = allLineEdits.at(i)->text();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by dholliday; 16th May 2011 at 12:23.

  3. #3
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to extract data from QFormLayout?

    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.
    Szilvi

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to extract data from QFormLayout?

    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:
    Qt Code:
    1. label = new QLabel("Elso sor:"); //declared i header: QLabel* label;
    2. lineEdit = new QLineEdit; //declared: QLineEdit* lineEdit;
    3.  
    4. this->formlo->addRow(label, lineEdit);
    To copy to clipboard, switch view to plain text mode 

    Then you can use the pointer directly (without casting), example to get the QString from lineEdit:
    Qt Code:
    1. QString textFromLineEdit = lineEdit->text();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to extract data from QFormLayout?

    I agree. That is the correct way of doing it.

  6. #6
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to extract data from QFormLayout?

    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.
    Szilvi

Similar Threads

  1. remove row of QFormLayout
    By jorg in forum Newbie
    Replies: 1
    Last Post: 31st January 2011, 05:19
  2. Replies: 2
    Last Post: 5th September 2010, 14:06
  3. extract data from photos
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2009, 09:38
  4. How can I extract raw data from a QPixmap?
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 16th September 2009, 08:31
  5. extract data from sqlTablemodel
    By peace_comp in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 19:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.