Why small boxes appearing in the QTableView?
-
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?// 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(); }
-
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?// 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(); }
@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!
-
@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!
@Christian-Ehrlicher Thank you, Christian, it worked.
-