Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:49:21+00:00 2026-06-03T13:49:21+00:00

I have a file which has absolute filepaths listed, 1 per line. The listed

  • 0

I have a file which has absolute filepaths listed, 1 per line. The listed files are in order, so all files in e.g. the /Documents/ dir will be listed after eachother in the file.

What I want to do is to place all these files in a QTreeWidget in a nice hierarchic structure, just like a normal filesystem. How would I do that from my file of absolute paths I have?

This is how far I’ve gotten with my coding on this:

QFile file(FILENAME_ENCRYPTED);
QString line;
QDir dir;

QTreeWidgetItem *item;
if (file.open(QIODevice::ReadOnly)) {
    QTextStream stream( &file );
    do {
        line = stream.readLine();
        if (!line.isNull()) {
            dir = QDir(line);
            item = new QTreeWidgetItem();
            item->setText(0, dir.dirName());
            this->ui->treeWidget->addTopLevelItem(item);
        }
    } while (!line.isNull());
}
file.close();

This works fine, but it only lists all the filenames after eachother. I guess I have to do some recursive function but recursion is not my best friend, I prefer iteration! Could someone give me a push in the right direction? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:49:23+00:00Added an answer on June 3, 2026 at 1:49 pm

    No recursion should be necessary. You can use QString::split() to split the file path into separate QStrings in a QStringList based on a separator (i.e., “/”), then iterate through each QString to determine the file structure.

    EDIT: Here is an example:

    #include <QtGui>
    
    const QString s1 = "Docs/Testing/textFile1.txt";
    const QString s2 = "Docs/Testing/textFile2.txt";
    const QString s3 = "Docs/Testing/textFile3.txt";
    const QString s4 = "Docs/Testing/AnotherFolder/textFile4.txt";
    const QString s5 = "ThisIsGonnaBeCrazy/WholeNewFolder/AndAnother/file.pdf";
    const QString s6 = "ThisIsGonnaBeCrazy/file.doc";
    
    class MainWindow : public QMainWindow
    {
    public:
        MainWindow()
        {
            QTreeWidget *treeWidget = new QTreeWidget;
    
            QStringList fileNames;
            fileNames << s1 << s2 << s3 << s4 << s5 << s6;
    
            QTreeWidgetItem *topLevelItem = NULL;
            foreach (const QString &fileName, fileNames)
            {
                QStringList splitFileName = fileName.split("/");
    
                // add root folder as top level item if treeWidget doesn't already have it
                if (treeWidget->findItems(splitFileName[0], Qt::MatchFixedString).isEmpty())
                {
                    topLevelItem = new QTreeWidgetItem;
                    topLevelItem->setText(0, splitFileName[0]);
                    treeWidget->addTopLevelItem(topLevelItem);
                }
    
                QTreeWidgetItem *parentItem = topLevelItem;
    
                // iterate through non-root directories (file name comes after)
                for (int i = 1; i < splitFileName.size() - 1; ++i)
                {
                    // iterate through children of parentItem to see if this directory exists
                    bool thisDirectoryExists = false;
                    for (int j = 0; j < parentItem->childCount(); ++j)
                    {
                        if (splitFileName[i] == parentItem->child(j)->text(0))
                        {
                            thisDirectoryExists = true;
                            parentItem = parentItem->child(j);
                            break;
                        }
                    }
    
                    if (!thisDirectoryExists)
                    {
                        parentItem = new QTreeWidgetItem(parentItem);
                        parentItem->setText(0, splitFileName[i]);
                    }
                }
    
                QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem);
                childItem->setText(0, splitFileName.last());
            }
    
            setCentralWidget(treeWidget);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    Note that you can use QTreeWidgetItem::setData() to set the file name for each file if you like. My example does not do that, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which has a particular line something like sometext sometext
I have a file which has multiple columns, whitespace separated. e.g: data1 data2 data3
Little Background: I have csv file which has lots of rows and each row
Assume you have a file which has been committed in your Git repo. You
I'm using Python, and I have a file which has city names and information
I have a javascript file which has a function calling a server (url) to
I have this xml file which has text init. i.e Hi my name is
I have a php file which has some variables like $name1, $name2... How can
i have a binary file which has 4 KB of header information and then
I have a JavaScript file which has a variable as such: var ads =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.