Results 1 to 12 of 12

Thread: QList

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QList

    hello anyone,

    I 'am using Qt-4.2
    I have a problem with QList in use with QListWidget.
    I cannot add new item to my QList.

    I declared in my header.
    Qt Code:
    1. private:
    2. QList<QString> list;
    To copy to clipboard, switch view to plain text mode 

    In my implentation file.
    Qt Code:
    1. list.append("Geert");
    2. QList<QString>::iterator i;
    3. for (i = list.begin(); i != list.end(); ++i){
    4. listWidget->insertItem(0, *i);
    5. }
    To copy to clipboard, switch view to plain text mode 

    This works fine i see the name Geert in my listWidget.
    Now i want to add a another name to my Qlist through a naamDialog .
    Here is a extract of the implentation file in the add function.
    Qt Code:
    1. {
    2. naamDialog dlg(this);
    3. if( dlg.exec() == QDialog::Accepted ) {
    4. QString naam = dlg.leNaam->text();
    5.  
    6. for (int row = 0; row < list.count(); ++row) {
    7. naam = dlg.leNaam->text();
    8. list.append(naam);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    It compile with no error's but when i run the application than the name is not added to the QList.
    Can you tell what i'am doing wrong in my code.
    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList

    Qt Code:
    1. {
    2. naamDialog dlg(this);
    3. if( dlg.exec() == QDialog::Accepted ) {
    4. QString naam = dlg.leNaam->text();
    5.  
    6. for (int row = 0; row < list.count(); ++row) {
    7. naam = dlg.leNaam->text();
    8. list.append(naam);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    I don't understand this. What are you trying to do. Is leNaam a QLabel, QLineEdit?
    What are you trying to do in the for loop? It is incorrect, anyway. By appending something to the list you modify it's item count. Theoretically that for should be infinite.

    Anyway, you were trying to add naam list.count() times... This doesn't make any sense..
    Care to revise?

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    Can you show me what i must change in this code.
    Qt Code:
    1. naamDialog dlg(this);
    2. if( dlg.exec() == QDialog::Accepted ) {
    3. QString naam = dlg.leNaam->text();
    4.  
    5. for (int row = 0; row < list.count(); ++row) {
    6. naam = dlg.leNaam->text();
    7. list.append(naam);
    8.  
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    thanks in advance

  4. #4
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    leNaam is a QLineEdit in the naamDialog

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList

    I would be happy to show you but you first must tell me what you are trying to achieve because the current code doesn't make too much sense to me.

  6. #6
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    I want to achieve a new name to my QList through a Dialog.
    Qt Code:
    1. void MainWindow::add()
    2. {
    3. naamDialog dlg(this);
    4. if( dlg.exec() == QDialog::Accepted ) {
    5. QString naam = dlg.leNaam->text();
    6. }
    To copy to clipboard, switch view to plain text mode 
    I have a MainWindow with a add button when i push the add button it pops up a Dialog( naamDialog.
    In this dialog i type the new name in the lineedit( leNaam).
    When i click the OK button in the Dialog i want to put the new name to my QList.
    That is want to achieve.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList

    Well, then this should be enough:
    Qt Code:
    1. void MainWindow::add()
    2. {
    3. naamDialog dlg(this);
    4. if( dlg.exec() == QDialog::Accepted ) {
    5. QString naam = dlg.leNaam->text();
    6. list.append( naam ); //<- Only one append is necessary
    7. }
    To copy to clipboard, switch view to plain text mode 

    You need to call append only once, if you want to add only one copy of the name in the list.

  8. #8
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    I tried this also several times but the problem when i close MainWindow application and run it again and read the list at the begining of my program no new name was added.
    Qt Code:
    1. QList<QString>::iterator i;
    2. for (i = list.begin(); i != list.end(); ++i){
    3. listWidget->insertItem(0, *i);
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList

    But do you save the QList anywhere when you close the application?
    Because the QList is just stored in memory while your app is running. No one will save it for you and then load it again.

    Regards

  10. #10
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    I think that is the problem i have i don't save the QList.
    How can i save the QList when i close the application.
    Is this possible.

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList

    Yes, it is possible.
    Something like:
    Save:
    Qt Code:
    1. QFile f( "savedlist.txt" );
    2. f.open( QIODevice::ReadWrite );
    3.  
    4. QString item;
    5. foreach( item, list )
    6. {
    7. f.write( item.toAscii().constData(), item.length() );
    8. f.write( "\r\n", 2 );
    9. }
    10.  
    11. f.close();
    To copy to clipboard, switch view to plain text mode 
    Load:
    Qt Code:
    1. QFile f( "savedlist.txt" );
    2. f.open( QIODevice::ReadWrite );
    3. while (!f.atEnd())
    4. {
    5. QByteArray line = f.readLine();
    6. list.append( QString( line.constData() ) );
    7. }
    8.  
    9. f.close();
    To copy to clipboard, switch view to plain text mode 
    This is a very simple approach that should work, normally. But you could customize the file path, check for permissions in the save path, check if the file exists and append to it if it exists, etc.

    Regards

  12. #12
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList

    Thanks Marcel for your time and suggestions.
    I will try tomorrow to make the code compleet with the save text

Similar Threads

  1. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 13:43
  2. using Qlist with a class
    By Havard in forum Qt Programming
    Replies: 10
    Last Post: 24th February 2007, 19:38
  3. QList crash in destructor
    By mclark in forum Newbie
    Replies: 7
    Last Post: 6th December 2006, 15:27
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  5. Values not being appended in a QList
    By Kapil in forum Newbie
    Replies: 5
    Last Post: 21st April 2006, 10:20

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.