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. QTableView doesn't update and has strange cell style by default
Qt 6.11 is out! See what's new in the release blog

QTableView doesn't update and has strange cell style by default

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 381 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.
  • D Offline
    D Offline
    deleted399
    wrote on last edited by
    #1

    Hi,

    Why do the cells have these two squares on the right? This is a QTableView created with the Design tab.

    weird table

    Moreover, When I click de "Add product" button I call the update function on the model but the table doesn't display the value, what I'm missing?
    The model of the table uses the m_activeProduct as underlying data.

    Button logic

    connect(ui->addProductButton, &QPushButton::clicked, this, [this](){
            Product p;
            p.m_name = "New product";
            p.m_price = 0.0;
            m_activeReceipt->addProduct(p);
            auto model = static_cast<ProductModel*>(ui->productsTableView->model());
            model->update();
        });
    

    Model update

    void ProductModel::update(){
        qDebug() << rowCount() << columnCount();
        auto top_left = index(0,0,QModelIndex());
        auto bottom_right = index(rowCount(),columnCount(),QModelIndex());
        emit dataChanged(top_left, bottom_right, {Qt::EditRole});
    }
    
    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show us your model. I don't see how changing m_activeReceipt should somehow magically update the model.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted399
        wrote on last edited by
        #3

        Here is the .h

        #ifndef PRODUCTMODEL_H
        #define PRODUCTMODEL_H
        
        #include <QAbstractTableModel>
        #include "receipt.h"
        class ProductModel : public QAbstractTableModel
        {
        public:
            ProductModel(Receipt * data, QObject* parent = nullptr);
            int rowCount(const QModelIndex &parent = QModelIndex()) const;
            int columnCount(const QModelIndex &parent = QModelIndex()) const;
            QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
            bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
            Qt::ItemFlags flags(const QModelIndex &index) const;
            void update( ) ;
        private:
            Receipt * m_data;
        };
        
        #endif // PRODUCTMODEL_H
        

        And the .cpp

        #include "productmodel.h"
        #include <QDebug>
        
        ProductModel::ProductModel(Receipt *data, QObject *parent):
            QAbstractTableModel(parent),
            m_data(data)
        {
        
        }
        
        int ProductModel::rowCount(const QModelIndex &parent) const
        {
            return m_data->products().size();
        }
        
        int ProductModel::columnCount(const QModelIndex &parent) const
        {
            return 2;
        }
        
        QVariant ProductModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
            if (role != Qt::DisplayRole)
                return QVariant();
        
            if (orientation == Qt::Horizontal) {
                if (section % 2 == 0)
                    return "Product";
                else
                    return "Price";
            } else {
                return QString("%1").arg(section + 1);
            }
        }
        
        QVariant ProductModel::data(const QModelIndex &index, int role) const
        {
            if (role == Qt::DisplayRole) {
                auto p = m_data->products();
                if(index.column() == 0)
                    return p[index.row()].m_name;
                else if(index.column() == 1){
                    return p[index.row()].m_price;
                }
            }
            else if (role == Qt::EditRole ) {
                auto p = m_data->products();
                if(index.column() == 0)
                    return p[index.row()].m_name;
                else if(index.column() == 1){
                    return p[index.row()].m_price;
                }
            }else{
                return QColor(Qt::white);
            }
            return QVariant();
        }
        
        bool ProductModel::setData(const QModelIndex &index, const QVariant &value, int role)
        {
            bool ok;
            if (index.isValid() && role == Qt::EditRole) {
                if(index.column() == 0){
                    auto p = m_data->products();
                    p[index.row()].m_name = value.toString();
                    emit dataChanged(index, index);
                    return true;
                }
                else if( float val = value.toFloat(&ok); index.column() == 1 && ok){
                    auto p = m_data->products();
                    p[index.row()].m_price =val;
                    emit dataChanged(index, index);
                    return true;
                }
            }
            return false;
        }
        
        Qt::ItemFlags ProductModel::flags(const QModelIndex &index) const
        {
            return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
        }
        
        void ProductModel::update(){
            auto top_left = index(rowCount()-1,columnCount()-1,QModelIndex());
            auto bottom_right = index(rowCount()-1,columnCount()-1,QModelIndex());
            emit dataChanged(top_left, bottom_right, {Qt::EditRole});
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          So my question stands - how do you put your new data into the model?
          Also you should not return QColor(Qt::white); for every role you don't process.

          /edit: looks like you hold the data outside the model - don't do this.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • D deleted399

            Hi,

            Why do the cells have these two squares on the right? This is a QTableView created with the Design tab.

            weird table

            Moreover, When I click de "Add product" button I call the update function on the model but the table doesn't display the value, what I'm missing?
            The model of the table uses the m_activeProduct as underlying data.

            Button logic

            connect(ui->addProductButton, &QPushButton::clicked, this, [this](){
                    Product p;
                    p.m_name = "New product";
                    p.m_price = 0.0;
                    m_activeReceipt->addProduct(p);
                    auto model = static_cast<ProductModel*>(ui->productsTableView->model());
                    model->update();
                });
            

            Model update

            void ProductModel::update(){
                qDebug() << rowCount() << columnCount();
                auto top_left = index(0,0,QModelIndex());
                auto bottom_right = index(rowCount(),columnCount(),QModelIndex());
                emit dataChanged(top_left, bottom_right, {Qt::EditRole});
            }
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @pachedo said in QTableView doesn't update and has strange cell style by default:

                auto bottom_right = index(rowCount(),columnCount(),QModelIndex());
                emit dataChanged(top_left, bottom_right, {Qt::EditRole});
            

            In addition to @Christian-Ehrlicher .

            This may not matter/be relevant to the behaviour you show, but I have two observations here:

            • I think it ought be bottom_right = index(rowCount() - 1, columnCount() - 1, QModelIndex());

            • If addProduct() adds a row to your model(?), dataChanged is not the right signal to emit. You should be using begin/endInsertRows().

            Your

                }else{
                    return QColor(Qt::white);
                }
            

            probably means you are returning QColor(Qt::white); when this is called asking about whether the item has a checkbox, and maybe that is causing your checkbox(es) to be shown?

            1 Reply Last reply
            2

            • Login

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