QTreeView and QFileSystemModel help



  • Hi,

    I am making an application using QTreeView and QFileSystemModel.
    The filesystem is loading fine in treeview.
    here is the basic structure,
    @C:
    |_Folder1
    |_Folder2
    |_Folder3
    |_Folder4
    D:
    |_Folder1
    |_Folder2
    |_Folder3
    |_Folder4
    E:
    |_Folder1
    |_Folder2
    @

    What I want to achieve is,

    I want to add the text of the selected index to my filelist.

    If incase parent index's text is already present in the filelist then while trying to add its children to filelist, a Messagebox should appear with "Yes" or "No" button, On selecting "Yes" children can be added and parent should be removed from filelist(For eg: if "C:" is already present in filelist then, if "Folder1", "Folder2", etc. is being added, at that time a Messagebox should appear with "Yes" or "No" button, On selecting "Yes", "Folder1", "Folder2", etc. is added and "C:" should be remove from filelist, same goes for "D:" and "E:" drive.).

    If incase child index's text is already present in the filelist then when user try to add parent in filelist at that time all of its children should be removed from the filelist(For eg: if "Folder1", "Folder2", etc. is already present in filelist then if "C:" is being added, at that time its children i.e. "Folder1", "Folder2", etc. should be removed from filelist and parent "C:" is added to filelist).

    My requirement is how can I know which is the parent index and which is child?

    At present I am able to add the selected index text to my filelist but I am unable to distinguish between the parent and its children.

    below is my code, please can someone give some input and help me out,
    Thanks in advance.

    @client::client(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::client)
    {
    ui->setupUi(this);
    dirmodel = new QFileSystemModel(this);
    displaymodel = new QFileSystemModel(this);
    //dirmodel->setFilter(QDir::Files);
    rootindex = dirmodel->setRootPath(QDir::rootPath());

    ui->dir_tree->setModel(dirmodel);
    //ui->dir_tree->sortByColumn(0, Qt::AscendingOrder);
    ui->dir_tree->setColumnWidth(0, 250);
    ui->dir_tree->setSelectionMode(QAbstractItemView::MultiSelection);
    standardmodel = new QStandardItemModel();
    ui->selectview->setModel(standardmodel);
     display();
    

    }

    void client::on_addbttn_clicked()
    {
    QModelIndexList list = ui->dir_tree->selectionModel()->selectedRows(0);
    QFileSystemModel* model = (QFileSystemModel*)ui->dir_tree->model();
    int row = -1;

    foreach (QModelIndex index, list)
    {
        QFileInfo fileInfo = model->fileInfo(index);
        qDebug() << fileInfo.fileName() << '\n';       
        filelist << fileInfo.filePath();
    }
    filewrite(filelist);
    

    }

    void client::filewrite(QStringList list)
    {
    QFile infile("backfile.ini");
    QTextStream f1(&infile);
    infile.open(QIODevice::WriteOnly| QIODevice::Text);
    for(int i = 0; i<list.size(); i++)
    {
    f1 << list.at(i) <<"\n";
    }
    infile.close();
    display();
    }

    void client::display()
    {
    QStandardItem *item;
    QFileInfo fileinfo;

    getfilelist();
    standardmodel->clear();
    for(int i = 0;i < filelist.size();i++)
    {
        fileinfo = filelist.at(i);
        indexes = standardmodel->index(i, 0, QModelIndex());
        item = new QStandardItem();
        item->setText(QString("%0").arg(fileinfo.filePath()));
        item->setData(QVariant(Qt::ContainsItemShape), Qt::UserRole);
        standardmodel->setItem(i, item);                                        //insert item into the model
        standardmodel->setData(indexes,QVariant(Qt::ContainsItemShape),Qt::UserRole);
    }
    

    }

    QStringList client::getfilelist()
    {
    filelist.clear();
    QFile infile1("backfile.ini");
    infile1.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream f2(&infile1);
    QString str;

    str = f2.readLine();
    
    while(!str.isNull())
    {
        filelist << str;
        qDebug()<<"str"<<str;
        str = f2.readLine();
    }
    infile1.close();
    return filelist;
    

    }
    @


Log in to reply
 

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