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

Misaligned geometry can occur for widgets in a QTableWidget after a new row is inserted

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.12.0 Alpha
    • 5.4.0, 5.5.0
    • Widgets: Itemviews
    • None
    • Windows 7 MSVC2013
      Windows XP MinGW
      OSX Yosemite 10.10.5 Clang
      Ubuntu 14.04 GCC
    • 451fcee8a8ec8ae6204e5bd0b26fff5c1292c677

    Description

      When inserting widgets into a QTableWidget, they will be misplaced vertically if:

      • there are more rows in the QTableWidget than there are rows visible, so that a vertical scrollbar is present
      • the last row has only partial height (i.e the height of the table widget minus the vertical header is not an even multiple of the row height)
      • the last row is visible on the screen
      • a new row is added to the QTableWidget

      To test in Qt Creator, create an empty widgets app.
      In the .pro file, add

      CONFIG += c++11
      

      in main.cpp, add these #include lines:

      #include "QPushButton"
      #include "QTableWidget"
      #include "QVBoxLayout"
      #include "QLineEdit"
      

      remove these 2 lines:

      MainWindow w;
      w.show();
      

      instead insert these lines:

      auto b  = new QPushButton("Add one row");
      auto tw = new QTableWidget(2,2);
      
      auto v  = new QVBoxLayout;
      v->addWidget(b);
      v->addWidget(tw);
      
      auto w  = new QWidget;
      w->setLayout(v);
      w->show();
      
      // for the bug to occur, we need the last row to have partial height (i.e. not completely visible)
      auto lastRowHeight = tw->height() % tw->rowHeight(0);
      if ((lastRowHeight < 8) || ((tw->rowHeight(0) - lastRowHeight) < 8))
          b->setText("Need a better height for the last row, try resizing table widget");
      
      // bug strikes when: scroll down to the bottom, clean the table, add one more row than before
      b->connect(b,&QPushButton::clicked,[tw]
      {
          auto rows = tw->rowCount();
          tw->scrollToBottom();
          tw->setRowCount(0);
          tw->setRowCount(++rows);
          tw->setItem(0,0,new QTableWidgetItem("Hello"));
          tw->setCellWidget(0,1,new QLineEdit("world"));
      
      // these lines are not needed for the bug to occur, only used to avoid manual scrolling up to the top
          qApp->processEvents();
          tw->scrollToTop();
      });
      

      Note: you can use a QComboBox or a QPushButton instead of a QLineEdit, the result is the same.

      Attachments

        Issue Links

          Activity

            People

              chehrlic Christian Ehrlicher
              hskoglund Henry Skoglund
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: