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

QItemDelegate calculates wrong sizeHint.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.12.0 Alpha
    • 4.7.1
    • Widgets: Itemviews
    • None
    • VS2008, Win-XP

    Description

      Hi,

      My table should automatically word wrap and adapt line height according to contents.
      It works basically, but when I reduce the width of a column then in the area where the word wrap should take place there is a mismatch between the sizehint and the drawing. The sizehint returns one line, but the drawing would already require two lines. Hence in a small range ellipses are seen. If I continue to reduce the column width then the line height is corrected.
      This is very obvious with QItemDelegate, but for QStyledItemDelegate the mismatch also exists, though only for one pixel.

      Example program header: wordwrap_tables.h

      #ifndef WORDWRAP_TABLES_H
      #define WORDWRAP_TABLES_H
      
      #include <QtGui/QTableWidget>
      
      class MyTableWidget : public QTableWidget
      {
         Q_OBJECT
      public:
         MyTableWidget();
      public slots:
         void slotHorizontalHeaderResized(int,int,int);
      };
      
      #endif // WORDWRAP_TABLES_H
      

      Example program cpp: main.cpp

      #include <QtGui/QApplication>
      #include <QtGui/QHeaderView>
      #include <QtGui/QItemDelegate>
      
      #include "wordwrap_tables.h"
      
      
      MyTableWidget::MyTableWidget()
      {
         connect(horizontalHeader(), SIGNAL(sectionResized(int,int,int)), this, SLOT(slotHorizontalHeaderResized(int,int,int)));
         verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
      }
      
      void MyTableWidget::slotHorizontalHeaderResized( int /*logicalIndex*/, int /*oldSize*/, int /*newSize*/ )
      {
         verticalHeader()->resizeSections(QHeaderView::ResizeToContents);
      }
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          MyTableWidget* tableWidget = new MyTableWidget();
          tableWidget->setItemDelegate( new QItemDelegate() ); 
          tableWidget->setRowCount(3);
          tableWidget->setColumnCount(3);
      
          QTableWidgetItem *newItem = new QTableWidgetItem( "Somewhat long test string" );
          tableWidget->setItem(0,0, newItem);
      
          tableWidget->show();
          return a.exec();
      }
      

      I made a workaround by overwriting QItemDelegate::sizeHint(), but you might want to fix this too.

      Best regards,
      Joachim

      Attachments

        1. image-2018-12-27-17-54-49-313.png
          image-2018-12-27-17-54-49-313.png
          3 kB
        2. qtableview.JPG
          qtableview.JPG
          36 kB
        3. qtableview.JPG
          qtableview.JPG
          39 kB
        4. qtableview1.JPG
          qtableview1.JPG
          13 kB
        5. qtbug_16134.tgz
          1 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            chehrlic Christian Ehrlicher
            joachim Joachim Eibl
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes