Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView separate class implementation
Forum Updated to NodeBB v4.3 + New Features

QTreeView separate class implementation

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 679 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sogo
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • mrjjM mrjj

        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.

        S Offline
        S Offline
        sogo
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • S sogo

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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 ?

          1 Reply Last reply
          1
          • S Offline
            S Offline
            sogo
            wrote on last edited by sogo
            #5

            @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.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sogo
                wrote on last edited by
                #7

                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

                mrjjM 1 Reply Last reply
                0
                • S sogo

                  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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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.

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    sogo
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved