Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. First column not populating in QTableView
Forum Updated to NodeBB v4.3 + New Features

First column not populating in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
qtableview c++qabstractmodel
3 Posts 2 Posters 696 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    egefeyzioglu
    wrote on 25 Sept 2022, 23:56 last edited by egefeyzioglu
    #1

    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 the data() method of the QAbstractTableModel is not called with Qt::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 calling show() 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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 26 Sept 2022, 01:56 last edited by ChrisW67
      #2

      @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.

      E 1 Reply Last reply 26 Sept 2022, 16:41
      3
      • C ChrisW67
        26 Sept 2022, 01:56

        @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.

        E Offline
        E Offline
        egefeyzioglu
        wrote on 26 Sept 2022, 16:41 last edited by
        #3

        @ChrisW67 oh my god... Yes that was it thank you 😂

        1 Reply Last reply
        0

        3/3

        26 Sept 2022, 16:41

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved