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. Update QTreeView item editor when resize item geometry
QtWS25 Last Chance

Update QTreeView item editor when resize item geometry

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.2k 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.
  • R Rory_1

    I have a QTreeView with a QStyledItemDelegate that sets the editor geometry to fill the QTreeView item rectangle.

    class InfoDelegate : public QStyledItemDelegate
    {
    public:
        explicit InfoDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) { }
    
        void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            editor->setGeometry(option.rect);
        }
    }
    

    In the QTreeView subclass the item delegate is set:

    setItemDelegate(new InfoDelegate(this));
    

    This is working except the editor size is not being adjusted if the QTreeView item is being edited and a resize occurs that changes the item cell width. The updateEditorGeometry is being triggered by the resize, but the editor is not being repainted. If I select and edit a different item after a resize the editor paints correctly, filling the entire cell.

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Rory_1 How / why does the resize occur?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    R 2 Replies Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @Rory_1 How / why does the resize occur?

      R Offline
      R Offline
      Rory_1
      wrote on last edited by
      #3

      @Christian-Ehrlicher A docking widget is dragged wider by the user.

      1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @Rory_1 How / why does the resize occur?

        R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #4

        @Christian-Ehrlicher Screenshot after drag:
        9d3c7ac1-c2b1-42dc-a53a-20b7907336eb-image.png

        VRoninV 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Why does a DockWidget triggers a resize of a column in the tableview?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Why does a DockWidget triggers a resize of a column in the tableview?

            R Offline
            R Offline
            Rory_1
            wrote on last edited by
            #6

            @Christian-Ehrlicher said in Update QTreeView item editor when resize item geometry:

            Why does a DockWidget triggers a resize of a column in the tableview?

            Just to be clear, it is a column in a QTreeView. The QTreeView is inside the DockWidget, and the last visible column is expanded / contracted to fit the dock. I think that is default Qt behavior to resize the DockWidget contents when it is resized. The resize occurs when the grab handle on the side of the doc is dragged to make it wider or narrower. Here is my code to include the QTreeView (infoView) in the dock (metadataDock):

            metadataDock->setWidget(infoView);
            
            1 Reply Last reply
            0
            • R Rory_1

              @Christian-Ehrlicher Screenshot after drag:
              9d3c7ac1-c2b1-42dc-a53a-20b7907336eb-image.png

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7

              @Rory_1 Thanks for the image, is the editor open in that picture?
              Can you try disabling the stylesheet temporarily and see if you have the same behaviour?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              R 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Ah, now I understand. Does it work when your QTreeWidget is in a plain QWidget which you resize by mouse?
                And can you maybe provide a minimal, compilable example so we can try it out? Can do it by myself but will take some time... :)

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                R 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  Ah, now I understand. Does it work when your QTreeWidget is in a plain QWidget which you resize by mouse?
                  And can you maybe provide a minimal, compilable example so we can try it out? Can do it by myself but will take some time... :)

                  R Offline
                  R Offline
                  Rory_1
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher I will try to tweak the example "EditableTreeModel" to reproduce. I'll try to get that done later today. Your attention to this matter is very much appreciated.

                  JonBJ 1 Reply Last reply
                  0
                  • R Rory_1

                    @Christian-Ehrlicher I will try to tweak the example "EditableTreeModel" to reproduce. I'll try to get that done later today. Your attention to this matter is very much appreciated.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @Rory_1 First thing is to see if it happens outside a dock widget.

                    R 1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rory_1
                      wrote on last edited by
                      #11

                      I have duplicated this behavior in the Qt example "EditableTreeModel". I have added two small blocks of code, identified by comments, in mainwindow.cpp, and listed below.

                      If you doubleclick on the right Description column to edit the item, and then drag the right window border to make the window wider, you can see the issue, where the edit box does not adjust to fill the cell, as instructed by the ItemDelegate updateEditorGeometry. I have included a stylesheet so you can see the behavior is the same with and without the stylesheet.

                      Using the procedure described, here are screenshots, first without the stylesheet and then with the stylesheet:

                      Without stylesheet
                      a7a13309-f0fd-4d5f-aea6-ce1954ca0bb4-image.png
                      With stylesheet
                      2838f956-7ca7-4de2-bc66-2877ba3dae7e-image.png

                      #include "mainwindow.h"
                      #include "treemodel.h"
                      
                      #include <QFile>
                      
                      // add this class to code
                      #include <QStyledItemDelegate>
                      class InfoDelegate : public QStyledItemDelegate
                      {
                      public:
                          explicit InfoDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) { }
                      
                          void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
                          {
                              editor->setGeometry(option.rect);
                          }
                      };
                      // end add class to code
                      
                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                      {
                          setupUi(this);
                      
                          const QStringList headers({tr("Title"), tr("Description")});
                      
                          QFile file(":/default.txt");
                          file.open(QIODevice::ReadOnly);
                          TreeModel *model = new TreeModel(headers, file.readAll());
                          file.close();
                      
                          view->setModel(model);
                      
                          // add this delegate
                          view->setItemDelegate(new InfoDelegate(view));
                          // with / without a stylesheet
                      //    view->setStyleSheet("QLineEdit:focus {background-color: blue;};");
                          // end add delegate to code
                      
                          for (int column = 0; column < model->columnCount(); ++column)
                              view->resizeColumnToContents(column);
                      
                          connect(exitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
                      
                          connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
                                  this, &MainWindow::updateActions);
                      
                          connect(actionsMenu, &QMenu::aboutToShow, this, &MainWindow::updateActions);
                          connect(insertRowAction, &QAction::triggered, this, &MainWindow::insertRow);
                          connect(insertColumnAction, &QAction::triggered, this, &MainWindow::insertColumn);
                          connect(removeRowAction, &QAction::triggered, this, &MainWindow::removeRow);
                          connect(removeColumnAction, &QAction::triggered, this, &MainWindow::removeColumn);
                          connect(insertChildAction, &QAction::triggered, this, &MainWindow::insertChild);
                      
                          updateActions();
                      }
                      
                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Rory_1 First thing is to see if it happens outside a dock widget.

                        R Offline
                        R Offline
                        Rory_1
                        wrote on last edited by Rory_1
                        #12

                        @JonB said in Update QTreeView item editor when resize item geometry:

                        @Rory_1 First thing is to see if it happens outside a dock widget.

                        My working example does not have a dock widget, so that is not problem.

                        1 Reply Last reply
                        1
                        • VRoninV VRonin

                          @Rory_1 Thanks for the image, is the editor open in that picture?
                          Can you try disabling the stylesheet temporarily and see if you have the same behaviour?

                          R Offline
                          R Offline
                          Rory_1
                          wrote on last edited by
                          #13

                          @VRonin said in Update QTreeView item editor when resize item geometry:

                          @Rory_1 Thanks for the image, is the editor open in that picture?
                          Can you try disabling the stylesheet temporarily and see if you have the same behaviour?

                          Same behavior with and without the stylesheet, as per my working example.

                          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