简体   繁体   中英

how to write rows qtreewidget in a file in Qt?

1- I want to save/write each row of QtreeWidget in a file(.xml or .txt or .xlsx or ...).

note: the rows are dynamically added.

2- Or i can use a push button on the GUI, whenever I wanted, I save the Complete the widget in file.

sample image of qtreewidget in link: Click here !

If your treeWidget doesnt have ChildItems like in the example picture than this is quite easy to solve:

   void myClass::on_pushButton_clicked()
   {
    QString sResult = "";
    for(int i = 0; i < ui.treeWidget->topLevelItemCount(); i++)
    {
        for (int j = 0; j < ui.treeWidget->columnCount(); i++)
        {
            sResult += ui.treeWidget->topLevelItem(i)->text(j) + "\t";  // a Tab Character added for better readability.
        }
        sResult += "\n";   //new Line
    }


    QFile file("myFile.txt");
    if (file.open(QIODevice::ReadWrite)) {
        QTextStream stream(&file);
        stream << sResult << endl;
    }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM