First column not populating in QTableView
-
Hi,
I have a QTableView that is connected to a QAbstractTableModel. The QTableView is set as the widget for a QDockWidget and added to my main window object. My problem is that the first column of the table is not populated. By adding some debug statements, I noticed that thedata()
method of the QAbstractTableModel is not called withQt::DisplayRole
for any cell in the first column. It's called with other roles, but not with the display role. I tried adding the QTableView as the central widget and even callingshow()
on it directly,and the same problem persists.Here's the relevant portions of my code:
// mainwindow.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { this -> setWindowTitle(kWindowTitle.data()); this -> setWindowIcon(QIcon(":/resources/img/icon.xpm")); this -> resize(1200,600); // Set up the base widgets setUpMemoryTable(memory_view); // Set up the dock widgets memory_tab = new QDockWidget("Memory"); memory_tab -> setWidget(memory_view); // Add everything to the window this -> addDockWidget(Qt::LeftDockWidgetArea, memory_tab); memory_view -> setShowGrid(true); memory_view -> setCornerButtonEnabled(false); memory_view -> setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectItems); memory_view -> setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); this -> show(); } void MainWindow::setUpMemoryTable(QTableView *&memory_view){ memory_view = new QTableView(); memory_model = new MemoryModel(emulator -> memory, emulator->kMemorySize, this); memory_view -> setModel(memory_model); }
// memorymodel.cpp MemoryModel::MemoryModel(uint8_t *memory, size_t kMemorySize, QObject *parent) : QAbstractTableModel{parent}, memory{memory}, kMemorySize{kMemorySize} {} int MemoryModel::rowCount(const QModelIndex &parent) const{ return kMemorySize/0x10; } int MemoryModel::columnCount(const QModelIndex &parent) const{ return 0x10; } QVariant MemoryModel::data(const QModelIndex &index, int role) const{ // Only want the display role if(role != Qt::DisplayRole){ return QVariant(); } if(index.isValid() && index.row() < rowCount() && index.row() >= 0 && index.column() < columnCount() && index.column() > 0){ auto ret = QVariant(QString::fromStdString(std::to_string(memory[(index.row()) * columnCount() + index.column()]))); return ret; } else { return QVariant(); } } Qt::ItemFlags MemoryModel::flags(const QModelIndex &index) const{ return Qt::ItemIsEnabled | Qt::ItemIsEditable; }
Thanks in advance for any help!
-
@egefeyzioglu said in First column not populating in QTableView:
if(index.isValid() && index.row() < rowCount() && index.row() >= 0 && index.column() < columnCount() && index.column() > 0){ ... } else { return QVariant(); }
You explicitly exclude column 0 from returning a value i.e. column must be greater than 0. So I suspect it is called but it is returning a null QVariant.