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

QItemDelegate does not draw in correct position after scrolling view

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • P2: Important
    • None
    • 4.7.4, 5.9.4, 5.13.0
    • Widgets: Itemviews
    • None
    • Ubuntu 16.04 x86_64
    • Linux/X11

    Description

      Description

      I'm trying to implement a QTableView where the first column is spanned to all rows. I also want the text data to be visible regardless of the vertical scroll applied to the view.

      Here is the view after starting the application

      Expected
      After scrolling down

      Actual

      The text is only drawn correctly after the application loses focus, which leads me to believe this is a bug. I've tested with the listed Qt versions and each result in slightly different incorrect behavior.

      Source files
      main.cpp

      #include <QApplication>
      #include <QTableView>
      #include "mymodel.h"
      #include "mydelegate.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QTableView tableView;
          MyModel myModel;
          MyDelegate myDelegate;
          tableView.setModel(&myModel);
          tableView.setItemDelegate(&myDelegate);
          tableView.setSpan(0, 0, 20, 1);
      
          tableView.show();
          return a.exec();
      }
      

      mydelegate.cpp

      #include "mydelegate.h"
      
      void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
          if (index.column() == 0) {
              QStyleOptionViewItem myOption = option;
              myOption.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
              myOption.rect.setRect(0, 0, myOption.rect.width(), myOption.rect.height());
              QStyledItemDelegate::paint(painter, myOption, index);
          }
          else {
              QStyledItemDelegate::paint(painter, option, index);
          }
      }
      

      mydelegate.h

      #pragma once
      #include <QStyledItemDelegate>
      
      class MyDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
      
      public:
          using QStyledItemDelegate::QStyledItemDelegate;
          void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
      };
      

      mymodel.cpp

      #include "mymodel.h"
      
      int MyModel::rowCount(const QModelIndex & /*parent*/) const
      {
         return 20;
      }
      
      int MyModel::columnCount(const QModelIndex & /*parent*/) const
      {
          return 20;
      }
      
      QVariant MyModel::data(const QModelIndex &index, int role) const
      {
          if (role == Qt::DisplayRole)
          {
             return QString("Row %1, Column %2")
                         .arg(index.row() + 1)
                         .arg(index.column() + 1);
          }
          return QVariant();
      }
      

      mymodel.h

      #pragma once
      #include <QAbstractTableModel>
      
      class MyModel : public QAbstractTableModel
      {
          Q_OBJECT
      public:
          using QAbstractTableModel::QAbstractTableModel;
          int rowCount(const QModelIndex &parent = QModelIndex()) const ;
          int columnCount(const QModelIndex &parent = QModelIndex()) const;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
      };
      

      qt-test.pro

      TARGET = qt-test
      
      TEMPLATE = app
      
      QT += widgets
      QMAKE_LFLAGS += -Wl,-rpath,$$(QT_INSTALL_PREFIX)/lib
      
      SOURCES += main.cpp \
                 mymodel.cpp \
                 mydelegate.cpp
      
      HEADERS += mymodel.h \
                 mydelegate.h
      

       

      Attachments

        1. actual.png
          actual.png
          8 kB
        2. expected.png
          expected.png
          10 kB
        3. qtbug_77740.tgz
          1 kB
        4. start.png
          start.png
          10 kB
        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
            kent-cheung-arm Kent Cheung
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes