PDA

View Full Version : store QlistWidget



jindoniit
15th July 2011, 06:32
I have an app when connect internet, it will retrieve data by web service to display on QlistWidget.
i want that when exit app then app will store QlistWidget to when start app, if it disconnect internet then it will retrieve data previously stored (same bookmark)

i tried by store into QList<QListWidgetItem*> but it displays empty (sorry my english)

Lykurg
15th July 2011, 07:36
Can't you iterate over the items and store it in a custom text file and when you start up, simple read that file and restore the list widget. Or show us some code you have so far.

Santosh Reddy
15th July 2011, 07:39
It is not very clear what you want, this what I understand

1. If you want to store data when app exits, and restore data when app starts, then you can QSettings to save and restore data (Note that you can only save data in QListWidget / QListWidgetItem, you cannot save QListWidget / QListWidgetItem itself)

2. If you want to store data when internet is disconnected, and restore data when internet restores, then you can use a container to store QListWidgetItem, using QList<QListWidgetItem*> (this would work if item is not already added to table)

jindoniit
15th July 2011, 08:38
this is my example


for (int i =0; i<12;i++)
{

QListWidgetItem *item = new QListWidgetItem(this->ui->listWidget);

QPixmap pix=icon.pixmap(QSize(400,400),QIcon::Selected,QIc on::On);
item->setSizeHint(QSize(200,60));
itemwid = new itemWidget(this);
itemwid->ui->lblImage->setPixmap(pix.scaled(pix.size(),Qt::KeepAspectRati o,Qt::SmoothTransformation));
itemwid->ui->lbl1->setText(tr("row number %1").arg(i));
itemwid->ui->lbl2->setText(tr("row number %1").arg(i+1));
this->ui->listWidget->setItemWidget(item,itemwid);

// store data into Qlist<QlistWidgetItem*>
itemTemp->append(item);


}

besides, if i use xml to store item ,then is it feasible?

Santosh Reddy
15th July 2011, 09:54
I remember helping on the same example project, are you the same user:)

Ok, here you have bunch of problems

1. when you clear the list widget, all the items and itemwidgets are deleted, so there is no use of storing the pointers to items in a list, it will crash your program.
2. what you need to store is data, using with you can recreate the item and item widgets
3. your program crashes, if button is clicked before all images load (debug this, may be later)
3. one coding error as below


void MainWindow::on_pushButton_clicked()
{
...
// for (int i=0;i<<itemTemp->length();i++) //note the '<<' operator
for (int i=0;i<itemTemp->length();i++)
...
}

again I took some time to fix the problems and change the class implementation a bit, check this out.:cool:
6665

jindoniit
15th July 2011, 10:36
thanks Santosh Reddy
yes, you've helped me.
I have identified a method of solving this the problem. although my real subject is slightly more complicated than this example but I'll solve it more easily.

if i want store data into xml , to when app starts (disconnect internet) then it will retrieve data previously stored. is it feasible?
thank you so much

Santosh Reddy
15th July 2011, 10:46
it is possible, it depends what data you want to store, if you want to store simple QString, QUrl etc then xml is ok, but if you want to store QIcon, QPixmap, then you need store the them in in separate image files, and store the image file names in xml.

jindoniit
15th July 2011, 10:52
oh , i understood, i will try to store data into xml.