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. How to stretch the child nodes of QTreeView

How to stretch the child nodes of QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 2.0k Views
  • 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by
    #1

    Here's how it looks by default:
    Capture.PNG
    The root nodes, ie. table, view, index and trigger takes the whole space to their right side BUT the child nodes don't. I've tried tree->resizeColumnToContents(1) after populating the treeview BUT that doesn't work. Here's how I added these in the QTreeView:

    tree = new QTreeView(this);
    tree->setContentsMargins(0,0,0,0);
    tree->setHeaderHidden(true);
    ...
    auto model = new QStandardItemModel(tree);
    model->setColumnCount(2);
    QSqlQuery query("SELECT * FROM sqlite_master");
    if(query.exec()){
        QStandardItem *parent;
        QString name;
        while (query.next()) {
            if(query.value(0).toString() != name){
                QIcon icon;
                name = query.value(0).toString();
                if(name == "table") icon = QIcon(":/table.svg");
                else if(name == "view") icon = QIcon(":/view.svg");
                else if(name == "trigger") icon = QIcon(":/trigger.svg");
                else icon = QIcon(":/index.svg");
                parent = new QStandardItem(icon, name);
                parent->setFont(QFont(parent->font().family(), -1, QFont::Bold, false));
                model->appendRow(parent);
            }
            auto child = new QStandardItem(query.value(1).toString());
            parent->appendRow(child);
        }
    }
    db.close();
    tree->setModel(model);
    //tree->resizeColumnToContents(1);
    

    I want the child node also to take the entire space to its right side so that the whole text is visible when I expand root nodes. Am I doing it in proper way or there's some shortcut to achieve the same?

    1 Reply Last reply
    0
    • D deleted385

      @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #11

      @Emon-Haque said in How to stretch the child nodes of QTreeView:

      @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

      Did you mean this?

      tree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
      

      Read and abide by the Qt Code of Conduct

      D 1 Reply Last reply
      1
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #2

        Is the treeview inside a layout ?

        D 1 Reply Last reply
        0
        • M mpergand

          Is the treeview inside a layout ?

          D Offline
          D Offline
          deleted385
          wrote on last edited by deleted385
          #3

          @mpergand, It's inside a QSplitter and that splitter is inside a QVBoxLayout. The QWidget that contains the QTreeView is inside a QStackedWidget and that StackedWidget is the centralWidget of the QMainWindow.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #4

            I think you have to add a layout to the widget containing the treeview for this one to expand.

            To visualize the actual size of the treeview, you can do:
            tree->setFrameShape(QFrame::Box);

            To verify if your interface is correct, you can try to reproduce it in the Designer and see if it works as you expected.

            D 1 Reply Last reply
            0
            • M mpergand

              I think you have to add a layout to the widget containing the treeview for this one to expand.

              To visualize the actual size of the treeview, you can do:
              tree->setFrameShape(QFrame::Box);

              To verify if your interface is correct, you can try to reproduce it in the Designer and see if it works as you expected.

              D Offline
              D Offline
              deleted385
              wrote on last edited by deleted385
              #5

              @mpergand, I've put these 3 segments in a QWidget named QueryWidget. On the left where the TreeView is, called ObjectView and on the right the CodeEditor and in bottom a textbox, and for now everything is in its constructor:

              QueryWidget::QueryWidget(QWidget *parent) : QWidget(parent)
              {
                  objects = new ObjectsView(this);
                  codeEditor = new CodeEditor(this);
                  auto textBox = new QTextEdit(this);
                  auto split1 = new QSplitter(Qt::Horizontal, this);
                  auto split2 = new QSplitter(Qt::Vertical, this);
              
                  split1->addWidget(objects);
                  split1->addWidget(codeEditor);
                  QSizePolicy obj(QSizePolicy::Preferred, QSizePolicy::Preferred);
                  QSizePolicy cod(QSizePolicy::Preferred, QSizePolicy::Preferred);
                  obj.setHorizontalStretch(1);
                  cod.setHorizontalStretch(3);
                  objects->setSizePolicy(obj);
                  codeEditor->setSizePolicy(cod);
              
                  split2->addWidget(split1);
                  split2->addWidget(textBox);
                  QSizePolicy sp1(QSizePolicy::Preferred, QSizePolicy::Preferred);
                  QSizePolicy txt(QSizePolicy::Preferred, QSizePolicy::Preferred);
                  sp1.setVerticalStretch(2);
                  txt.setVerticalStretch(1);
                  split1->setSizePolicy(sp1);
                  textBox->setSizePolicy(txt);
              
                  auto vLay = new QVBoxLayout(this);
                  vLay->addWidget(split2);
              }
              

              in the ObjectView's constructor I've a toolBar and treeview in QVBoxlayout:

              ObjectsView::ObjectsView(QWidget *parent) : QWidget(parent)
              {
                  auto toolBar = new QToolBar(this);
                  tree = new QTreeView(this);
                  tree->setHeaderHidden(true);
              
                  auto attach = new QAction(QIcon(":/database-plus.svg"), "attach database", this);
                  toolBar->addAction(attach);
                  connect(attach, &QAction::triggered, this, &ObjectsView::openFileDialog);
              
                  auto layout = new QVBoxLayout(this);
                  layout->setContentsMargins(0,0,0,0);
                  layout->addWidget(toolBar);
                  layout->addWidget(tree);
                  setLayout(layout);
              }
              

              and in the MainWindow's constructor I've put the QueryWidget in the StackWidget like this:

              Window::Window(QWidget *parent) : QMainWindow(parent)
              {
                  stack = new QStackedWidget(this);
                  queryWidget = new QueryWidget(stack);
                  tableWidget = new TableWidget(stack);
                  stack->addWidget(queryWidget);
                  stack->addWidget(tableWidget);
                  setCentralWidget(stack);
                  ....
              }
              

              I've tried the tree->setFrameShape(QFrame::Box) BUT there's no apparent difference in the Window.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #6

                I've tried the tree->setFrameShape(QFrame::Box) BUT there's no apparent difference in the Window.

                It doesn't change anything, it draws a box around the treeview.

                Look at this splitter
                splitter

                The frame on the left doesn't expand because the parent widget has no layout.

                [EDIT]

                auto layout = new QVBoxLayout(this);
                    layout->setContentsMargins(0,0,0,0);
                    layout->addWidget(toolBar);
                    layout->addWidget(tree);
                    setLayout(layout);
                

                Your code seems correct.
                If you don't add the toolbar, what happens ?
                Do you see the frame box around the tree ?

                D 1 Reply Last reply
                1
                • M mpergand

                  I've tried the tree->setFrameShape(QFrame::Box) BUT there's no apparent difference in the Window.

                  It doesn't change anything, it draws a box around the treeview.

                  Look at this splitter
                  splitter

                  The frame on the left doesn't expand because the parent widget has no layout.

                  [EDIT]

                  auto layout = new QVBoxLayout(this);
                      layout->setContentsMargins(0,0,0,0);
                      layout->addWidget(toolBar);
                      layout->addWidget(tree);
                      setLayout(layout);
                  

                  Your code seems correct.
                  If you don't add the toolbar, what happens ?
                  Do you see the frame box around the tree ?

                  D Offline
                  D Offline
                  deleted385
                  wrote on last edited by
                  #7

                  @mpergand, it shows the border around the treeview without setFrameShape. See, it takes up the remainder of the QVboxLayout and shows black/grey border around white space allocated for the QTreeView:
                  x1.gif

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by mpergand
                    #8

                    You may set a minimum width for the first column or save/restore the state of the header,
                    see QHeaderView :

                    QByteArray QHeaderView::saveState() const
                    Saves the current state of this header view.
                    To restore the saved state, pass the return value to restoreState().
                    This function was introduced in Qt 4.3.
                    See also restoreState().

                    D 1 Reply Last reply
                    2
                    • M mpergand

                      You may set a minimum width for the first column or save/restore the state of the header,
                      see QHeaderView :

                      QByteArray QHeaderView::saveState() const
                      Saves the current state of this header view.
                      To restore the saved state, pass the return value to restoreState().
                      This function was introduced in Qt 4.3.
                      See also restoreState().

                      D Offline
                      D Offline
                      deleted385
                      wrote on last edited by
                      #9

                      @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

                      kshegunovK 1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mpergand
                        wrote on last edited by
                        #10

                        Look at the doc about QHeaderView::ResizeMode
                        and this methods:
                        setMinimumSectionSize(int size)
                        setDefaultSectionSize(int size)
                        and others ...

                        1 Reply Last reply
                        1
                        • D deleted385

                          @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #11

                          @Emon-Haque said in How to stretch the child nodes of QTreeView:

                          @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

                          Did you mean this?

                          tree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
                          

                          Read and abide by the Qt Code of Conduct

                          D 1 Reply Last reply
                          1
                          • kshegunovK kshegunov

                            @Emon-Haque said in How to stretch the child nodes of QTreeView:

                            @mpergand, I don't want to show the headers with splitter on top and without that I can't change the state manually. like I did in the GIF example above.

                            Did you mean this?

                            tree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
                            
                            D Offline
                            D Offline
                            deleted385
                            wrote on last edited by
                            #12

                            @kshegunov, exactly. Now with header hidden, columns stretches automatically as I move the splitter.
                            Thanks

                            1 Reply Last reply
                            0

                            • Login

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