Update QTreeView item editor when resize item geometry
-
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.
-
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.
@Rory_1 How / why does the resize occur?
-
@Rory_1 How / why does the resize occur?
@Christian-Ehrlicher A docking widget is dragged wider by the user.
-
@Rory_1 How / why does the resize occur?
@Christian-Ehrlicher Screenshot after drag:
-
Why does a DockWidget triggers a resize of a column in the tableview?
-
Why does a DockWidget triggers a resize of a column in the tableview?
@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);
-
@Christian-Ehrlicher Screenshot after drag:
-
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... :) -
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... :)@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.
-
@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.
-
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
With stylesheet
#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(); }
-
@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?@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.