[SOLVED] write edited qstringlist from qlistwidget to a file



  • Hi All,

    i try to write out a qstringlist to a file, but the file remains empty.

    First put in some data (from a file) to the listWidget.

    @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();
    ui->listWidget->clear();
    ui->listWidget->addItems(loadlist);
    }@

    Now i remove the data from the listWidget (i dont know if this works)

    @void MainWindow::on_removeButton_clicked()
    {

    //ui->listWidget->takeItem(ui->listWidget->currentRow());
    
    QListWidgetItem* item = ui->listWidget->takeItem(ui->listWidget->currentRow());
    delete item;
    

    //ui->listWidget->removeItemWidget(ui->listWidget->currentItem());

    QFile newloads("/fs1/PerlQt/QtCreator/test/new.inp");
    newloads.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
    QTextStream out(&newloads);
    out << loadlist.join("\n");
    newloads.close();
    

    }@

    Some ideas??

    Thanks in advance


  • Lifetime Qt Champion

    Hi,

    Do you have a loadList class member ?
    In that case on_radioButtonLF3_clicked does not update it, it shadows it.



  • i dont think so. i didnt write that much code up to now, so how do i add such a class?


  • Lifetime Qt Champion

    Then you didn't show all code involved in the writing process. Show us the complete method were you write the stringlist



  • Hi, its there.
    In the first section on_radiobutton...
    I input a file and fill all the stuff into a qstringlist named loadlist.
    The data is shown with a listwidget in the gui.

    The second section is for deleting data from the listwidget.i try here to get rid of selected data from the listwidget by pushing remove_button... so i hope this data is also deleted from the qstringlist loadlist.
    Then open a file for the newloadlist and storing the data from loadlist in newloadlist.the new loadlist should than written to new.inp file.

    Hope that helps. Maybe the problem is that nothing is in loadlist, because loadlist is defined in on_radiobutton and not global. But i thought the everything is managed with the listwigdet



  • What you trying to do? Write deleted items into file?



  • No,
    i am trying to write the data from the qstringlist which isnt deleted to a file / qstring.



  • 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


  • Lifetime Qt Champion

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.