Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-103987

QTreeView displays wrong column width when persistent editors are opened

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15, 6.3
    • Widgets: Itemviews
    • None
    • All

    Description

      When persistent editors are opened for the first column of QTreeView, calling `resizeColumnToContents(0)` results in insufficient column width. The editor widgets are cut short. The test code and result image are attached below.

      I believe the root cause for this bug is that the code in `QTreeViewPrivate::widthHintForIndex` missed out the indentation alongside the editor's `sizeHint()`.

      Test code:

      class TreeModel
          : public QAbstractItemModel
      {
          static quintptr const Max_Depth = 3;
      public:
          virtual QModelIndex index(int row, int column, QModelIndex const & parent = QModelIndex()) const override
          {
              quintptr parent_depth = parent.isValid() ? parent.internalId() : 0;
              if (parent_depth >= Max_Depth)
                  return QModelIndex();
              else
                  return createIndex(row, column, parent_depth + 1);
          }
          virtual QModelIndex parent(QModelIndex const & index) const override
          {
              if (false == index.isValid())
                  return QModelIndex();
              quintptr depth = index.internalId();
              if (depth <= 1U)
                  return QModelIndex();
              return createIndex(0, 0, depth - 1U);
          }
          virtual int rowCount(QModelIndex const & parent = QModelIndex()) const override
          {
              quintptr parent_depth = parent.isValid() ? parent.internalId() : 0;
              if (parent_depth >= Max_Depth)
                  return 0;
              else
                  return 1;
          }
          virtual int columnCount(QModelIndex const & parent = QModelIndex()) const override
          {
              return 2;
          }
          virtual QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const override
          {
              return {};
          }
      };
      
      class Delegate
          : public QItemDelegate
      {
      public:
          virtual QWidget * createEditor(QWidget * parent, QStyleOptionViewItem const & /*option*/, QModelIndex const & /*index*/) const override
          {
              auto r = new QLabel(parent);
              r->setText("[ABCDEFGHIJKLMNOPQRSTUVWXYZ]");
              return r;
          }
      };
      
      int main(int argc, char ** argv)
      {
          QApplication app{ argc, argv };
      
          TreeModel tree_model;
          Delegate delegate;
          QTreeView tree_view;
          tree_view.setModel(&tree_model);
          tree_view.setItemDelegateForColumn(0, &delegate);
          for (auto idx = tree_model.index(0, 0); idx.isValid(); idx = tree_model.index(0, 0, idx))
              tree_view.openPersistentEditor(idx);
          tree_view.showMaximized();
          tree_view.expandAll();
          tree_view.resizeColumnToContents(0);
      
          return app.exec();
      }
      
      

       Result image:

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            zwhfly WH Z
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes