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. Why small boxes appearing in the QTableView?
Forum Updated to NodeBB v4.3 + New Features

Why small boxes appearing in the QTableView?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 255 Views 1 Watching
  • 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.
  • N Offline
    N Offline
    neetaj
    wrote on last edited by
    #1

    Hello,
    I have written a code where I am using QTableView, below is the code and final output. My query is why small boxes (highlighted in red) are appearing in the 2nd column i.e., the "Id" column. I don't want those boxes in the table view, how shall I remove that? Am I missing any settings for the QTableView?

    Output.png

    // EmployeeDetailsView.h
    
    #ifndef EMPLOYEEDETAILSVIEW_H
    #define EMPLOYEEDETAILSVIEW_H
    
    #include <QWidget>
    #include <QAbstractTableModel>
    #include "EmployeeModel.h"
    
    //class EmployeeModel;
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class EmployeeDetailsView;
    }
    QT_END_NAMESPACE
    
    class EmployeeDetailsView : public QWidget
    {
        Q_OBJECT
    
    public:
        EmployeeDetailsView(QWidget *parent = nullptr);
        ~EmployeeDetailsView();
    
        EmployeeModel* m_EmpModelPtr;
    
    private:
        Ui::EmployeeDetailsView *ui;
    };
    #endif // EMPLOYEEDETAILSVIEW_H
    
    

    // EmployeeDetailsView.cpp
    #include "EmployeeDetailsView.h"
    #include "ui_EmployeeDetailsView.h"

    EmployeeDetailsView::EmployeeDetailsView(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::EmployeeDetailsView)
    {
    ui->setupUi(this);
    m_EmpModelPtr = new EmployeeModel(this);

    ui->m_tableView->setModel(m_EmpModelPtr);
    

    }

    EmployeeDetailsView::~EmployeeDetailsView()
    {
    delete ui;
    }

    // EmployeeModel.h
    
    #ifndef EMPLOYEEMODEL_H
    #define EMPLOYEEMODEL_H
    
    #include <QAbstractTableModel>
    #include <QList>
    #include <QString>
    
    struct EmpData
    {
        QString m_EmpName;
        int m_Id;
    };
    
    class EmployeeModel : public QAbstractTableModel
    {
        Q_OBJECT
        QList<EmpData> m_EmpList;
    
    public:
        explicit EmployeeModel(QObject *parent = nullptr);
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    };
    
    #endif // EMPLOYEEMODEL_H
    
    

    // EmployeeModel.cpp

    #include "EmployeeModel.h"
    #include <QDebug>

    EmployeeModel::EmployeeModel(QObject *parent)
    : QAbstractTableModel(parent)
    {
    m_EmpList.append({"Keo", 9});
    m_EmpList.append({"John", 5});
    m_EmpList.append({"Nick", 1});
    m_EmpList.append({"Hash", 7});
    m_EmpList.append({"Sam", 3});
    }

    int EmployeeModel::rowCount(const QModelIndex &) const
    {
    qDebug() << "total emp count : " << m_EmpList.count();
    return m_EmpList.count();
    }

    int EmployeeModel::columnCount(const QModelIndex &) const
    {
    return 2; // name , id
    }

    QVariant EmployeeModel::data(const QModelIndex &index, int role) const
    {
    qDebug() << "index row : " << index.row() << "\t col : " << index.column();

    if(!index.isValid())
        return QVariant();
    
    if(role == Qt::DisplayRole)
        if(index.column() == 0)
            return m_EmpList.at(index.row()).m_EmpName;
    
        if(index.column() == 1)
        return m_EmpList.at(index.row()).m_Id;
    
    return QVariant();
    

    }

    QVariant EmployeeModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    qDebug() << "section : " << section << "\t orientation : " << orientation;
    if(role == Qt::DisplayRole)
    {
    if(orientation == Qt::Horizontal)
    {
    return (section == 0)? "Name" : "Id";
    }
    /* else
    {
    return QString::number(section + 1); // Row numbers
    }
    */
    }
    return QVariant();
    }

    // main.cpp
    
    #include "EmployeeDetailsView.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        EmployeeDetailsView w;
        w.setWindowTitle("Employee Data View");
        w.show();
        return a.exec();
    }
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • N neetaj

      Hello,
      I have written a code where I am using QTableView, below is the code and final output. My query is why small boxes (highlighted in red) are appearing in the 2nd column i.e., the "Id" column. I don't want those boxes in the table view, how shall I remove that? Am I missing any settings for the QTableView?

      Output.png

      // EmployeeDetailsView.h
      
      #ifndef EMPLOYEEDETAILSVIEW_H
      #define EMPLOYEEDETAILSVIEW_H
      
      #include <QWidget>
      #include <QAbstractTableModel>
      #include "EmployeeModel.h"
      
      //class EmployeeModel;
      
      QT_BEGIN_NAMESPACE
      namespace Ui {
      class EmployeeDetailsView;
      }
      QT_END_NAMESPACE
      
      class EmployeeDetailsView : public QWidget
      {
          Q_OBJECT
      
      public:
          EmployeeDetailsView(QWidget *parent = nullptr);
          ~EmployeeDetailsView();
      
          EmployeeModel* m_EmpModelPtr;
      
      private:
          Ui::EmployeeDetailsView *ui;
      };
      #endif // EMPLOYEEDETAILSVIEW_H
      
      

      // EmployeeDetailsView.cpp
      #include "EmployeeDetailsView.h"
      #include "ui_EmployeeDetailsView.h"

      EmployeeDetailsView::EmployeeDetailsView(QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::EmployeeDetailsView)
      {
      ui->setupUi(this);
      m_EmpModelPtr = new EmployeeModel(this);

      ui->m_tableView->setModel(m_EmpModelPtr);
      

      }

      EmployeeDetailsView::~EmployeeDetailsView()
      {
      delete ui;
      }

      // EmployeeModel.h
      
      #ifndef EMPLOYEEMODEL_H
      #define EMPLOYEEMODEL_H
      
      #include <QAbstractTableModel>
      #include <QList>
      #include <QString>
      
      struct EmpData
      {
          QString m_EmpName;
          int m_Id;
      };
      
      class EmployeeModel : public QAbstractTableModel
      {
          Q_OBJECT
          QList<EmpData> m_EmpList;
      
      public:
          explicit EmployeeModel(QObject *parent = nullptr);
      
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          int columnCount(const QModelIndex &parent = QModelIndex()) const override;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
          QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
      };
      
      #endif // EMPLOYEEMODEL_H
      
      

      // EmployeeModel.cpp

      #include "EmployeeModel.h"
      #include <QDebug>

      EmployeeModel::EmployeeModel(QObject *parent)
      : QAbstractTableModel(parent)
      {
      m_EmpList.append({"Keo", 9});
      m_EmpList.append({"John", 5});
      m_EmpList.append({"Nick", 1});
      m_EmpList.append({"Hash", 7});
      m_EmpList.append({"Sam", 3});
      }

      int EmployeeModel::rowCount(const QModelIndex &) const
      {
      qDebug() << "total emp count : " << m_EmpList.count();
      return m_EmpList.count();
      }

      int EmployeeModel::columnCount(const QModelIndex &) const
      {
      return 2; // name , id
      }

      QVariant EmployeeModel::data(const QModelIndex &index, int role) const
      {
      qDebug() << "index row : " << index.row() << "\t col : " << index.column();

      if(!index.isValid())
          return QVariant();
      
      if(role == Qt::DisplayRole)
          if(index.column() == 0)
              return m_EmpList.at(index.row()).m_EmpName;
      
          if(index.column() == 1)
          return m_EmpList.at(index.row()).m_Id;
      
      return QVariant();
      

      }

      QVariant EmployeeModel::headerData(int section, Qt::Orientation orientation, int role) const
      {
      qDebug() << "section : " << section << "\t orientation : " << orientation;
      if(role == Qt::DisplayRole)
      {
      if(orientation == Qt::Horizontal)
      {
      return (section == 0)? "Name" : "Id";
      }
      /* else
      {
      return QString::number(section + 1); // Row numbers
      }
      */
      }
      return QVariant();
      }

      // main.cpp
      
      #include "EmployeeDetailsView.h"
      
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          EmployeeDetailsView w;
          w.setWindowTitle("Employee Data View");
          w.show();
          return a.exec();
      }
      
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @neetaj said in Why small boxes appearing in the QTableView?:

      if(role == Qt::DisplayRole)
          if(index.column() == 0)
              return m_EmpList.at(index.row()).m_EmpName;
          if(index.column() == 1)
              return m_EmpList.at(index.row()).m_Id;
      

      Because you missed the curly brackets here.

      Please format your code properly with the </> - code tags!

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

      N 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @neetaj said in Why small boxes appearing in the QTableView?:

        if(role == Qt::DisplayRole)
            if(index.column() == 0)
                return m_EmpList.at(index.row()).m_EmpName;
            if(index.column() == 1)
                return m_EmpList.at(index.row()).m_Id;
        

        Because you missed the curly brackets here.

        Please format your code properly with the </> - code tags!

        N Offline
        N Offline
        neetaj
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thank you, Christian, it worked.

        1 Reply Last reply
        0
        • N neetaj has marked this topic as solved on

        • Login

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