PDA

View Full Version : QList



dragon
9th May 2007, 17:18
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.


private:
QList<QString> list;



In my implentation file.


list.append("Geert");
QList<QString>::iterator i;
for (i = list.begin(); i != list.end(); ++i){
listWidget->insertItem(0, *i);
}


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.


{
naamDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
QString naam = dlg.leNaam->text();

for (int row = 0; row < list.count(); ++row) {
naam = dlg.leNaam->text();
list.append(naam);
}
}

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.

marcel
9th May 2007, 17:36
{
naamDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
QString naam = dlg.leNaam->text();

for (int row = 0; row < list.count(); ++row) {
naam = dlg.leNaam->text();
list.append(naam);
}
}


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?

dragon
9th May 2007, 18:04
Can you show me what i must change in this code.


naamDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
QString naam = dlg.leNaam->text();

for (int row = 0; row < list.count(); ++row) {
naam = dlg.leNaam->text();
list.append(naam);


}


thanks in advance

dragon
9th May 2007, 18:06
leNaam is a QLineEdit in the naamDialog

marcel
9th May 2007, 18:11
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.

dragon
9th May 2007, 18:37
I want to achieve a new name to my QList through a Dialog.


void MainWindow::add()
{
naamDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
QString naam = dlg.leNaam->text();
}

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.

marcel
9th May 2007, 18:49
Well, then this should be enough:


void MainWindow::add()
{
naamDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {
QString naam = dlg.leNaam->text();
list.append( naam ); //<- Only one append is necessary
}


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

dragon
9th May 2007, 19:12
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.


QList<QString>::iterator i;
for (i = list.begin(); i != list.end(); ++i){
listWidget->insertItem(0, *i);

marcel
9th May 2007, 19:21
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

dragon
9th May 2007, 19:36
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.

marcel
9th May 2007, 20:11
Yes, it is possible.
Something like:
Save:


QFile f( "savedlist.txt" );
f.open( QIODevice::ReadWrite );

QString item;
foreach( item, list )
{
f.write( item.toAscii().constData(), item.length() );
f.write( "\r\n", 2 );
}

f.close();

Load:


QFile f( "savedlist.txt" );
f.open( QIODevice::ReadWrite );
while (!f.atEnd())
{
QByteArray line = f.readLine();
list.append( QString( line.constData() ) );
}

f.close();

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

dragon
9th May 2007, 20:15
Thanks Marcel for your time and suggestions.
I will try tomorrow to make the code compleet with the save text