Question

I've implemented a table through deriving QTableView and QAbstractTableModel. It all seems to work fine except when I resize the table vertically the rows that were originally out of view don't show any data.

There is no issue when resizing horizontally possibly because I've overridden the resizeEvent() method and am recalculating column widths which I obviously don't do if the table is resized vertically.

I'm using the following code in the model to add data to the table:

bool DDUTableModel::insertRow(int row, const QModelIndex& parent)
{
    beginInsertRows(parent, row, row);
    digital_display_list_.append(DigitalDisplayData(path_));
    endInsertRows();
    return true;
}

The resizeEvent() looks like this:

void DDUTableView::resizeEvent(QResizeEvent* ev)
{


int num_columns = NUM_ELEMENTS(COLUMN_WIDTHS);

if (num_columns > 0) {

    int width = ev->size().width();
    int used_width = 0;

    // Set our widths to be a percentage of the available width
    for (int i = 0; i < num_columns - 1; i++) {
        int column_width = (width * COLUMN_WIDTHS[i]) / 100;
        this->setColumnWidth(i, column_width);
        used_width += column_width;
    }

    // Set our last column to the remaining width
    this->setColumnWidth(num_columns - 1, width - used_width);
}
}

Any ideas?

Was it helpful?

Solution

The problem was with the resizeEvent(). I need to also invoke the method in the QTableView class that I derived from to force a refresh on vertical resizing. Amended method looks like this:

void DDUTableView::resizeEvent(QResizeEvent* ev)
{
   int num_columns = NUM_ELEMENTS(COLUMN_WIDTHS);

   if (num_columns > 0) {

      int width = ev->size().width();
      int used_width = 0;

      // Set our widths to be a percentage of the available width
      for (int i = 0; i < num_columns - 1; i++) {
         int column_width = (width * COLUMN_WIDTHS[i]) / 100;
         this->setColumnWidth(i, column_width);
         used_width += column_width;
      }

      // Set our last column to the remaining width
      this->setColumnWidth(num_columns - 1, width - used_width);
  }

  QTableView::resizeEvent(ev);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top