Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QTableViewExample5

 

QtQt CreatorLubuntu

 

QTableView example 5: simple custom data 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: ./CppQTableViewExample5/CppQTableViewExample5.pro

 

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

HEADERS  += qtdialog.h

FORMS    += qtdialog.ui

 

 

 

 

 

./CppQTableViewExample5/qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#include <QDialog>

namespace Ui {
  class QtDialog;
}

struct MyModel;

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

private:
  Ui::QtDialog *ui;

  ///The downcasted model
  MyModel* m_model;
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQTableViewExample5/qtdialog.cpp

 

#include "qtdialog.h"

#include <cassert>
#include <iostream>
#include <stdexcept>

#include <boost/numeric/conversion/cast.hpp>

#include <QHeaderView>
#include <QStandardItemModel>
#include "ui_qtdialog.h"

///A simple custom data type
///For now, its just contains a std::vector<std::string>
struct MyDataTable
{
  MyDataTable() : data(CreateTable())  {}
  std::vector<std::string> data;
  static const std::vector<std::string> CreateTable()
  {
    return
    {
      "Black",
      "Blue",
      "Cyan",
      "Green",
      "Magenta"
      "Red",
      "White",
      "Yellow",
    };
  }
};

struct MyModel : public QStandardItemModel
{

  MyModel(QObject *parent = 0)
    : QStandardItemModel(
        0, //rows, will become 8
        1, //columns, only 1 for now
        parent
      ),
      m_data(MyDataTable())
  {
    setColumnCount(1); //Only 1 for now
    setHeaderData(0,Qt::Horizontal,"Color");
    const int n_rows = boost::numeric_cast<int>(m_data.data.size());
    for (int i=0; i!=n_rows; ++i)
    {
      QList<QStandardItem*> items;
      //Add text
      {
        QStandardItem * const item = new QStandardItem;
        item->setCheckable(false); //NO CHECKBOX!
        items.push_back(item);
      }
      //Only have one column for now
      assert(this->columnCount() == items.size());
      this->appendRow(items);
    }
    assert(this->rowCount() == boost::numeric_cast<int>(m_data.data.size()));
  }
  ///Implement virtual member function
  QVariant data(const QModelIndex &index, int role) const
  {
    //Removing this line will cause checkboxes to appear
    if (role != Qt::EditRole &&  role != Qt::DisplayRole) return QVariant();

    assert(index.isValid());
    assert(index.row() < boost::numeric_cast<int>(m_data.data.size()));
    assert(index.column() < 1);
    const std::string& row = m_data.data[ index.row() ];
    switch (index.column())
    {

      case 0:
        //Only 1 column for now
        return QString(row.c_str());
      default:
        assert(!"Should not get here");
        throw std::logic_error("MyModel::data");
    }
  }
  ///Implement virtual member function
  bool setData(const QModelIndex &index, const QVariant &value, int /*role = Qt::EditRole*/)
  {
    assert(index.isValid());
    assert(index.row() < boost::numeric_cast<int>(m_data.data.size()));
    assert(index.column() < 1);
    std::string& row = m_data.data[ index.row() ];
    switch (index.column())
    {

      case 0:
        //Only 1 column for now
        row = value.toString().toStdString(); break;
      default:
        assert(!"Should not get here");
        throw std::logic_error("MyModel::setData");
    }

    //Allows other views to synchronize on this model update
    emit dataChanged(index,index); //From C++ GUI programming with Qt 4, 2nd edition
    return false;
  }
  private:
  ///My custom data type
  MyDataTable m_data;
};

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

  ui->table->setModel(m_model);
  assert(ui->table->model());
  assert(m_model);
  assert(ui->table->model() == m_model);
  //Only one column for now
  ui->table->setColumnWidth(0,175);
}

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

 

 

 

 

 

./CppQTableViewExample5/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