Results 1 to 3 of 3

Thread: How to insert values in QTreeWidget from Text file Using QlistIterator ?

  1. #1
    Join Date
    Jun 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default How to insert values in QTreeWidget from Text file Using QlistIterator ?

    Iterator overwrites the values in QtreeWidget . My text file contains these three lines and only one line is showing

    ************text file **************
    1200,Shahbaz,Ifc,laja
    2001,ali,Step,kdh
    1992,ikaka,psp,ayaj
    ****************************************
    Kindly tell me how to avoid it .

    My code is following

    QString fileName = QFileDialog::getOpenFileName(this, tr ("Open File"), "C:\\", "All files (*.*);; Text File (*.txt);; IFC File (*.ifc);; STEP File (*.step);;XML files (*.xml);;Image files (*.jpg *.png)");

    QFile file(fileName);

    if (!file.open(QIODevice::ReadOnly | QIODevice ::Text))
    QMessageBox::critical(this,tr("STOP"),tr("Error with loading"));

    QTreeWidgetItem *item = new QTreeWidgetItem (ui->treeWidget);
    QTextStream in(&file);
    QString line;
    // ui->textBrowser->setText(in.readAll());
    do
    {
    line = in.readLine();

    QList <QString> parts = line.split(",", QString::KeepEmptyParts);
    QString L;
    QListIterator<QString> Iter (parts);
    while (Iter.hasNext())
    {
    for (int i=0 ; i<4; i++)
    {
    L = Iter.next();
    item->setText(i,L);
    }
    }

    } while (!in.atEnd());
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to insert values in QTreeWidget from Text file Using QlistIterator ?

    Create a new item for each line you read instead of creating one item and overwriting its values upon every iteration.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to insert values in QTreeWidget from Text file Using QlistIterator ?

    What are you trying to achieve with the while() and nested for()?

    You might also think about what happens in the do {} while() when the file contains no lines.

Similar Threads

  1. Replies: 11
    Last Post: 28th June 2012, 11:17
  2. Replies: 5
    Last Post: 26th June 2012, 07:39
  3. Insert data in a text file
    By korwin in forum Newbie
    Replies: 7
    Last Post: 15th July 2011, 02:48
  4. Insert values into database table
    By l0ner in forum Newbie
    Replies: 3
    Last Post: 20th June 2011, 19:03
  5. Replies: 2
    Last Post: 17th November 2006, 11:25

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.