Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved QTreeView in QTreeWidgetItem

    General and Desktop
    qtreeview qtreewidgetitem adjusttocontent qtreewidget
    4
    8
    1786
    Loading More Posts
    • 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.
    • C
      Craetor last edited by Craetor

      Hi, guys!

      I have QTreeWidget with QTreeWidgetItems.
      QTreeWidgetItem should contain QTreeView.

      It work, but i need to resize QTreeView automaticaly by content.
      tree->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); // do not work

      void MainWindow::addTreeView()
      {
          QFileSystemModel *model = new QFileSystemModel();
          model->setRootPath(QDir::currentPath());
      
          QTreeView *tree = new QTreeView();
          tree->setModel(model);
          tree->setRootIndex(model->index(QDir::currentPath()));
      
          // ??
          tree->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);   // not work
          tree->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
      
          QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
          ui->treeWidget->setItemWidget(item, 0, tree);
      
          // ??
          ui->treeWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
          ui->treeWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
      }
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          addTreeView();
          addTreeView();
      }
      
      

      How it can be implemented?

      image

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Craetor last edited by

        @Craetor said in QTreeView in QTreeWidgetItem:

        QTreeWidgetItem should contain QTreeView.

        I don't understand this: do you mean an item in your tree should contain another tree?!

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • C
          Craetor last edited by

          @jsulm, yes, on screenshot you can see tree QTreeWidget with QTreeWidgetItem which the contain QTreeView.
          But QTreeView can't execute setSizeAdjustPolicy - AdjustToContents.

          1 Reply Last reply Reply Quote 0
          • qwasder85
            qwasder85 last edited by

            This can't be solved with a single QTreeWidget and a clever model instead? I'm surprised you got this to work.

            1 Reply Last reply Reply Quote 0
            • C
              Craetor last edited by

              @qwasder85,
              how it can be solved?

              1 Reply Last reply Reply Quote 0
              • C
                Craetor last edited by Craetor

                I fixed this issue.

                1. QTreeView return correct sizeHint for AdjustToContents.
                2. I implemented QStyledItemDelegate for sending sizeHint to QTreeWidgetItem from the QTreeView and force call QTreeWidget updateGeometries.
                3. Child class of QTreeView call SetData(0, Qt::SizeHintRole, sizeHint()) for QTreeWidgetItem on expanded and collapsed

                it work fine, may be it very strange way, but i don't known another)

                QSize CSResizeDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex& index) const
                {
                    CSResizableTreeWidgetItem *item = static_cast<CSResizableTreeWidgetItem*>(index.internalPointer());
                    if(item->treeWidget())
                        ((CSResizableTreeWidget*)item->treeWidget())->updateGeometriesPublic(); // force call QTreeWidget::updateGeometries
                    return item->getTree()->sizeHint(); // return QTreeView::sizeHint
                }
                

                alt text

                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @Craetor last edited by JonB

                  @Craetor
                  Just to say: I don't really know what you're trying to achieve here. You say you're nesting QTreeViews within QTreeWidget, why do you not just use 2 separate QTreeWidget/Views? But I may not be understanding...

                  1 Reply Last reply Reply Quote 0
                  • C
                    Craetor last edited by Craetor

                    @JonB
                    This code is a small example to find solution.
                    In my real project i need to show tree with different items and nested trees.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post