Results 1 to 8 of 8

Thread: dynamic allocation of QlineEdit causing application crash, how to solve it?

  1. #1
    Join Date
    May 2012
    Posts
    15
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default dynamic allocation of QlineEdit causing application crash, how to solve it?

    Good evening everyone,

    I try to create a form with a number of fields that are set by depending on a parameter.
    So I wanted to do dynamic allocation of QLineEdit. I must say I am quite happy at first because the window displays properly with the correct number of fields.
    However when I close the application, it does not close properly.
    it says that the application encountered a problem, and i should send an error report.
    Here's a snippet. H and. Cpp I have abbreviated for you.
    Thank you for your valuable advice.


    Qt Code:
    1. #ifndef CHAMPSVARIABLES_H
    2. #define CHAMPSVARIABLES_H
    3.  
    4. #include<QtGui>
    5.  
    6. class ChampsVariables : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ChampsVariables();
    12. QLineEdit *LabelGenerique;
    13. QVBoxLayout *Layout;
    14. private:
    15.  
    16. private slots:
    17.  
    18. };
    19. #endif // CHAMPSVARIABLES_H
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. #include"ChampsVariables.h"
    2.  
    3. ChampsVariables::ChampsVariables() : QWidget()
    4. {
    5. LabelGenerique = new QLineEdit[5];
    6. Layout = new QVBoxLayout;
    7.  
    8. for(int i=0;i<5;i++)
    9. {
    10. LabelGenerique[i].setText("");
    11. Layout->addWidget(&LabelGenerique[i]);
    12. }
    13.  
    14. setLayout(Layout);
    15.  
    16. QLayoutItem * item;
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    a simple main:
    Qt Code:
    1. #include"ChampsVariables.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. ChampsVariables fenetre;
    8. fenetre.show();
    9.  
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: dynamic allocation of QlineEdit causing application crash, how to solve it?

    Use an array that will hold pointers to QLineEdit
    Qt Code:
    1. //declare
    2. QLineEdit **LabelGenerique;
    3. //define
    4. ChampsVariables::ChampsVariables(QWidget* parent) : QWidget(parent) //don't forget to pass the parent pointer to QWidget's constructor
    5. {
    6. LabelGenerique = new QLineEdit*[5];
    7. Layout = new QVBoxLayout;
    8.  
    9. for(int i=0;i<5;i++)
    10. {
    11. LabelGenerique[i] = new QLineEdit();
    12. LabelGenerique[i]->setText("");
    13. Layout->addWidget( LabelGenerique[i] );
    14. }
    15.  
    16. setLayout(Layout);
    17. //don't forget to delete the array, else you have a small memory leak.
    To copy to clipboard, switch view to plain text mode 
    or a much better approach is to use a QList<QLineEdit*> (again pointers to widgets go into the container)

    LE: added the parent comment and removed some redundant operator
    Last edited by Zlatomir; 9th June 2012 at 23:22.

  3. The following user says thank you to Zlatomir for this useful post:

    CyrilQt (10th June 2012)

  4. #3
    Join Date
    May 2012
    Posts
    15
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: dynamic allocation of QlineEdit causing application crash, how to solve it?

    Thank you very much it works perfectly.
    However, I meet new challenges with the rest of my code. I can no longer extract the contents of QLineEdit as before.
    Following that worked here before:
    Qt Code:
    1. QString data_to_insert = "";
    2. for (int i=1;i<5;i++) /
    3. {
    4. data_to_insert +="'" + this->LineEditlGenerique[i].text().toLower() + "'" ;
    5. }
    To copy to clipboard, switch view to plain text mode 
    I do not control completely the pointers, I tried several combinations like ** & LineEditGenerique or even &&. but it was not very conclusive.
    I am very grateful for all the coming advice.

  5. #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: dynamic allocation of QlineEdit causing application crash, how to solve it?

    You have the example code: LabelGenerique[ i ]->setText(""); so that code must use the "arrow" operator ( -> ): LineEditlGenerique[ i ]->text()//...;
    //dereference is not enough you need dereference and then access the member

  6. The following user says thank you to Zlatomir for this useful post:

    CyrilQt (10th June 2012)

  7. #5
    Join Date
    May 2012
    Posts
    15
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: dynamic allocation of QlineEdit causing application crash, how to solve it?

    thank you a thousand times, it works great

    oups, just one point ? where should i delete it? when should i delete this array?
    because i still have a crash due to this line
    Qt Code:
    1. data_to_insert +="'" + LineEditlGenerique[i]->text().toLower() + "'" ;
    To copy to clipboard, switch view to plain text mode 
    without setting
    Qt Code:
    1. delete LineEditlGenerique;
    To copy to clipboard, switch view to plain text mode 
    at the proper place
    Last edited by CyrilQt; 10th June 2012 at 18:47.

  8. #6
    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: dynamic allocation of QlineEdit causing application crash, how to solve it?

    As i said in the first post you can save yourself of a lot of trouble if you use QList<QLineEdit*> and not an c-style array.

    Anyway that shouldn't cause a crash, just a memory leak, so make sure you don't have other mistakes... i see you have done some renaming there

    And second issue that delete instruction should be a delete[] LineEditlGenerique; call (notice []), because you delete an array and you should call that in your class destructor.

  9. The following user says thank you to Zlatomir for this useful post:

    CyrilQt (10th June 2012)

  10. #7
    Join Date
    May 2012
    Posts
    15
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: dynamic allocation of QlineEdit causing application crash, how to solve it?

    Thanks for your time,
    I didn't use QList<QLineEdit*> because your first advice and solution was working good enough for me.
    I really appreciate your patience, and will set the delete [] lineEditlGenerique in my destructor.

    Best regards.

  11. #8
    Join Date
    May 2012
    Posts
    15
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: dynamic allocation of QlineEdit causing application crash, how to solve it?

    it works, thanks for everything!

Similar Threads

  1. vector causing system error/crash
    By babygal in forum Newbie
    Replies: 9
    Last Post: 21st October 2010, 07:48
  2. Qt 4.7 SSE2 causing my app to crash
    By jonks in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2010, 18:53
  3. QThread::wait() causing crash
    By Olliebrown in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 15:24
  4. Memory allocation failure and crash in QVector
    By ashatilo in forum Qt Programming
    Replies: 16
    Last Post: 20th October 2007, 23:27
  5. QTreeWidget->clear() causing crash after sort
    By Chuk in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2007, 09:59

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.