[SOLVED] write edited qstringlist from qlistwidget to a file
-
In this case better use QListView instead QListWidget. Define loadlist not in on_radioButtonLF3_clicked but as MainWindow member.
-
here for all who will have the same problem. This is my code
@
//Multiple selection inside the listwidget and delete selected items
ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
foreach (QListWidgetItem* item, ui->listWidget->selectedItems())
delete item;//qstringlist of the left items inside the listwidget
QStringList pickedItem;
QList<QListWidgetItem*> tmpList = ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
for (int i = 0; i < tmpList.size(); ++i)
pickedItem.append(tmpList.at(i)->text());//write to a file
QFile newloads("/fs1/PerlQt/QtCreator/test/newloads.inp");
newloads.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
QTextStream out(&newloads);
out<< pickedItem.join("\n");newloads.close();
@ -
Just curios, is there any reason using QListWidget instead QListView? Because your solution of course will work but it little heavy and not efficient.
-
The only reason is that my code is copy paste out of the internet and i have no IDEA what i am doing here :)
I am an absolute beginner and doing qt for 2 weeks.
Could you rewrite my code? This would be nice. -
[quote author="mr_maddog" date="1361904066"]Could you rewrite my code? This would be nice.[/quote] :)
If this is not a just school task and you need realy learn Qt i advice you start from this book:
C++ GUI Programming with Qt 4 - By Jasmin Blanchette, Mark Summerfield
this book about Qt 4.1 but it is still best book for beginners.
and of course:
"http://qt-project.org/doc/":http://qt-project.org/doc/if you have any questions feel free ask here :)
-
Hi qxoz,
thanks for the reply. I dont have the money for buying a book. I will still trying to finish my project by past and copy. I learned in the past 6 months perl and now qt. my brain is FULL ;)Could u give a hint for changing qwidgdetlist to qlistview.
Thanks
-
If you can't afford the book, you still have the examples and demos to go through in Qt's doc (Have look at the MVC part)
-
If just change listwidget with listview and not change the algorithm the code will look like this:
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
stringListModel = new QStringListModel(this);
ui->listView->setModel(stringListModel);
}@@void MainWindow::on_radioButtonLF3_clicked()
{
// Loaddefinition
QFile loadfile("/na/share/LINUX/public/L.txt");
loadfile.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&loadfile);
QString line = in.readAll();
QStringList loadlist = line.split(QRegExp("\n"), QString::SkipEmptyParts);
loadfile.close();
stringListModel->setStringList(loadlist);
}@@void MainWindow::on_removeButton_clicked()
{
QModelIndex strIndex = ui->listView->selectionModel()->currentIndex();
if(!strIndex.isValid())return;
stringListModel->removeRow(strIndex.row());//write to a file QFile newloads("/fs1/PerlQt/QtCreator/test/newloads.inp"); newloads.open(QFile::WriteOnly|QFile::Truncate|QFile::Text); QTextStream out(&newloads); out<< stringListModel->stringList().join("\n"); newloads.close();
}@
Here is the very good Qt and C++ tutorials for beginners:
"http://voidrealms.com/":http://voidrealms.com/ -
Hi and Thanks qxoz,
and this is now better and faster than with qstringlist?
Why? You rewrote just a few lines (i dont get it....).
Is the qlistview better implemented, works the code faster (because of what?)..Greetz
-
Hi mr_maddog!
sorry for the long absence :)
Using model/view gives to your code more flexibility, it is separation of data and view.
And in your case you not need build result string list,
@//qstringlist of the left items inside the listwidget
QStringList pickedItem;
QList<QListWidgetItem*> tmpList = ui->listWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard);
for (int i = 0; i < tmpList.size(); ++i)
pickedItem.append(tmpList.at(i)->text());@
you just get it:
@stringListModel->stringList().join("\n");@ -
Oki doki, thanks