Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QTableViewExample10

 

QtQt CreatorLubuntu

 

QTableView example 10: two tables with checkboxes and editable text using a custom model and a custom data type is a QTableView example.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQTableViewExample10/CppQTableViewExample10.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++11 -Wextra -Wall -Werror
SOURCES += \
    qtdialog.cpp \
    qtmain.cpp \
    mymodel.cpp \
    mydata.cpp

HEADERS  += qtdialog.h \
    mymodel.h \
    mydata.h

FORMS    += qtdialog.ui

 

 

 

 

 

./CppQTableViewExample10/mydata.h

 

#ifndef MYDATA_H
#define MYDATA_H

#include <string>
#include <tuple>
#include <vector>

struct MyData
{
  MyData();
  bool GetNo(const int row) const { return std::get<0>(m_data[row]); };
  const std::string& GetQuestion(const int row) const { return std::get<2>(m_data[row]); };
  int GetRowCount() const { return static_cast<int>(m_data.size()); }
  bool GetYes(const int row) const { return std::get<1>(m_data[row]); };
  void SetNo(const int row, const bool yes_no) { std::get<0>(m_data[row]) = yes_no; }
  void SetQuestion(const int row, const std::string& question)  { std::get<2>(m_data[row]) = question; }
  void SetYes(const int row, const bool yes_no) { std::get<1>(m_data[row]) = yes_no; }

  private:
  std::vector<std::tuple<bool,bool,std::string> > m_data;

  static const std::vector<std::tuple<bool,bool,std::string> > CreateData();
};

#endif // MYDATA_H

 

 

 

 

 

./CppQTableViewExample10/mydata.cpp

 

#include "mydata.h"

MyData::MyData()
  : m_data(CreateData())
{
}

const std::vector<std::tuple<bool,bool,std::string> > MyData::CreateData()
{
  return
  {
    std::make_tuple(false,false,"Question A"),
    std::make_tuple(false,true ,"Question B"),
    std::make_tuple(true ,false,"Question C"),
    std::make_tuple(true ,true ,"Question D"),
  };
}

 

 

 

 

 

./CppQTableViewExample10/mymodel.h

 

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractTableModel>
#include <boost/shared_ptr.hpp>

struct MyData;

struct MyModel : public QAbstractTableModel
{
  MyModel(const boost::shared_ptr<MyData> data, QObject *parent = 0);

  int columnCount(const QModelIndex &parent) const;
  QVariant data(const QModelIndex &index, int role) const;
  Qt::ItemFlags flags(const QModelIndex &index) const;
  QVariant headerData(int section, Qt::Orientation orientation, int role) const;

  int rowCount(const QModelIndex &parent) const;
  bool setData(const QModelIndex &index, const QVariant &value, int role);

  private:
  const boost::shared_ptr<MyData> m_data;
};

#endif // MYMODEL_H

 

 

 

 

 

./CppQTableViewExample10/mymodel.cpp

 

#include "mymodel.h"

#include <cassert>
#include <QStandardItem>
#include "mydata.h"

MyModel::MyModel(const boost::shared_ptr<MyData> data, QObject *parent)
  : QAbstractTableModel(parent),
    m_data(data)
{

}

int MyModel::columnCount(const QModelIndex &) const
{
  return 3;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
  switch (index.column())
  {
    case 0:
      if (role == Qt::DisplayRole || role == Qt::CheckStateRole)
        return m_data->GetYes(index.row()) ? Qt::Checked : Qt::Unchecked;
    case 1:
      if (role == Qt::DisplayRole || role == Qt::CheckStateRole)
        return m_data->GetNo(index.row()) ? Qt::Checked : Qt::Unchecked;
    case 2:
      if (role == Qt::DisplayRole || role == Qt::EditRole)
        return m_data->GetQuestion(index.row()).c_str();
  }
  return QVariant();
}

Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
  switch (index.column())
  {
    case 0:
    case 1:
      return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
    case 2:
      return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
  }
  return Qt::NoItemFlags;
}

QVariant MyModel::headerData(int section, Qt::Orientation orientation, int /*role*/) const
{
  if (orientation == Qt::Horizontal)
  {
    switch (section)
    {
      case 0: return "Y";
      case 1: return "N";
      case 2: return "Question";
      default: assert(!"Should not get here");
    }
  }
  else
  {
    return section;
  }
}

int MyModel::rowCount(const QModelIndex &) const
{
  return m_data->GetRowCount();
}

bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
  switch (index.column())
  {
    case 0:
      if (role == Qt::CheckStateRole)
        m_data->SetYes(index.row(),value.toInt() == Qt::Checked);
    break;
    case 1:
      if (role == Qt::CheckStateRole)
        m_data->SetNo(index.row(),value.toInt() == Qt::Checked);
    break;
    case 2:
      if (role == Qt::EditRole)
        m_data->SetQuestion(index.row(),value.toString().toStdString());
    break;
  }
  emit dataChanged(index,index);
  return true;
}

 

 

 

 

 

./CppQTableViewExample10/qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#include <boost/shared_ptr.hpp>
#include <QDialog>

struct MyData;

namespace Ui {
  class QtDialog;
}

struct QStandardItemModel;

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  ~QtDialog();
  
private slots:

private:
  Ui::QtDialog *ui;
  boost::shared_ptr<MyData> m_data;
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQTableViewExample10/qtdialog.cpp

 

#include "qtdialog.h"

#include <cassert>

#include "mydata.h"
#include "mymodel.h"
#include "ui_qtdialog.h"

QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog),
  m_data(new MyData)
{
  ui->setupUi(this);

  MyModel * const model = new MyModel(m_data,this);
  assert(!ui->table_1->model());
  assert(!ui->table_2->model());
  ui->table_1->setModel(model);
  ui->table_2->setModel(model);
  assert(ui->table_1->model());
  assert(ui->table_2->model());


  ui->table_1->setColumnWidth(0, 24);
  ui->table_2->setColumnWidth(0, 24);
  ui->table_1->setColumnWidth(1, 24);
  ui->table_2->setColumnWidth(1, 24);
  ui->table_1->setColumnWidth(2,175);
  ui->table_2->setColumnWidth(2,175);
}

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

 

 

 

 

 

./CppQTableViewExample10/qtmain.cpp

 

#include <QApplication>
#include "qtdialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml