Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeView separate class implementation



  • I'm trying to implement treeView widget as a separate class. The idea is I have a mainWindow with pushButton that when pressed opens a file dialog from which users selects directory. The selected directory path is given to instance of separate treeView class with QFileSystemModlel. The code looks something like this:

    void mainWindow::openFileDialog()
    {
    QString filePath;
    QString folderPath =  QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                  filePath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    view->openFile(folderPath);
    }
    

    The above is from MainWindow class and "view" is the instance of "TreeView" class which has only treeView widget in its UI file. Now treeView class sets up treeView in UI and gets this string path to set QFileSystemModel's root path by:

    void TreeView::openFile(const QString& folderPath)
    													 
    {
    model->setRootPath(folderPath);
    QModelIndex index = model->index(folderPath);
    model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    ui->treeView->setRootIndex(index);
    ui->treeView -> expand(index);
    }
    

    The treeView class UI is called as child of MainWindow UI so I can see treeView widget in my mainwindow, but the treeView does not gets populated. If I run everything in treeView class and bypass creating new instance of treeView class "view", it works fine. Does creating new instance is causing the issue, I'm just passing path variable to that instance and I thought the treeView should get populated. The folderPath gets passed fine as I was able to read in openFile(const QString&) function. I just need some help in understanding what I'm doing wrong, thanks


  • Lifetime Qt Champion

    Hi
    Are you sure that new instance of "TreeView" is the one you are looking at on the screen as the code looks fine
    and you say it works if you do it directly in "TreeView".
    There should be nothing wrong with creating any number of instances.



  • @mrjj Hi mrjj
    Thanks for quick response. I'm adding the UI of TreeView Widget in MainWindow UI file by promoted widget method.


  • Lifetime Qt Champion

    @sogo
    Hi
    So in real MainWin, you have promoted a Widget to be a "TreeViev" and that works`?

    Im asking as you have

    view->openFile(folderPath);

    and "view" here does not seem to be in UI-> so that is the new instance which does not want to work?

    Can you show how you create and insert the "view" into the real MainWin ?



  • @mrjj
    Sorry for late response, I was trying something and now getting this error. So this is how I am setting my UI widget:
    In MainWindow UI:
    Capture.PNG

    In TreeView UI:
    Capture1.PNG

    This is MainWindow looks like:
    Capture3.PNG

    Code for MainWindow.cxx:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        view = new TreeView;
        this->connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openFileDialog()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::openFileDialog()
    {
        QString filePath;
        QString folderPath =  QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                      filePath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        view->openFile(folderPath);
    }
    

    code for TreeView.cxx:

    #include "TreeView.h"
    #include "ui_TreeView.h"
    
    TreeView::TreeView(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::TreeView)
    {
        ui->setupUi(this);
        model = new QFileSystemModel;
    
    }
    
    TreeView::~TreeView()
    {
        delete ui;
    }
    
    void TreeView::openFile(const QString& folderPath)
    
    {
    model->setRootPath(folderPath);
    QModelIndex index = model->index(folderPath);
    model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    ui->treeView->setModel(model);
    ui->treeView->setRootIndex(index);
    ui->treeView -> expand(index);
    }
    

    Edited:
    Error was due to model not set to treeView, sorry it was mistake in code.


  • Lifetime Qt Champion

    Hi
    Before we look at the error
    Why do you both promote and then create a new one?

    view = new TreeView; // this is a new one
    this->connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openFileDialog()));

    If you mean the one you have on mainWindow via promoting that would be
    ui->widget
    ( you can rename it.. its just called widget pr default )

    so Do you really mean to create that new one, that you don't insert into MainWindow ?

    Just checing as it perfectly fine if you want it to be a popup that opens in a window over mainwin
    and you are ok with its not the one in MainWindow we look at.



  • I see, I should not create new instance, even if I do, I should insert that instance into MainWindow instead of adding it manually?
    If I add manually TreeView UI as ui->widget, I can get that in my MainWindow but how can I get the corresponding TreeView public functions that sets the treeView to specific model "openFile(const QString&)". Wouldn't I require an instance of that class in MainWindow


  • Lifetime Qt Champion

    @sogo
    Hi
    The promoted widget Is of the right type.
    That is the cool thing about promotion.
    so
    ui->widget->openFile(xxx) will work.

    (just rename it in Designer to call it something better and do full recompile)

    So no, if you don't mean to have 2 of your TreeView then you should not create a new one.



  • Oh I see, I misunderstood the promote thing, it is actually taking the header file so it is taking the complete TreeView class. I misunderstood it with UI as being separate class and that's why I started creating new instance of TreeView class. Thanks, I think now I get it. Closing this issue.


Log in to reply