Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QTableViewExample1

 

QtQt CreatorLubuntu

 

QTableView example 1: table with three columns, one with an X is a QTableView example.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQTableViewExample1/CppQTableViewExample1.pro

 

include(../../DesktopApplication.pri)
include(../../Libraries/Boost.pri)

SOURCES += \
    qtdialog.cpp \
    qtmain.cpp

HEADERS  += qtdialog.h

FORMS    += qtdialog.ui

 

 

 

 

 

./CppQTableViewExample1/qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QDialog>
#pragma GCC diagnostic pop

namespace Ui {
  class QtDialog;
}

struct QStandardItemModel;

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  QtDialog(const QtDialog&) = delete;
  QtDialog& operator=(const QtDialog&) = delete;
  ~QtDialog();
  
private slots:
  void on_button_clicked();

private:
  Ui::QtDialog *ui;
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQTableViewExample1/qtdialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qtdialog.h"

#include <cassert>

#include <QStandardItemModel>
#include "ui_qtdialog.h"
#pragma GCC diagnostic pop

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

  assert(!ui->table->model());
  ui->table->setModel(new QStandardItemModel(this));
  assert( ui->table->model());

  assert(dynamic_cast<QStandardItemModel*>(ui->table->model()));
  dynamic_cast<QStandardItemModel*>(ui->table->model())->setColumnCount(3);

  for (int i=0; i!=10; ++i) on_button_clicked();
}

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

void QtDialog::on_button_clicked()
{
  const int n_cols = ui->table->model()->columnCount();
  QList<QStandardItem*> items;
  for (int i=0; i!=n_cols; ++i)
  {
    QStandardItem * const item = new QStandardItem;
    item->setText("");
    items.push_back(item);
  }
  const int j = (std::rand() >> 4) % n_cols;
  items[j]->setText("X");

  assert(dynamic_cast<QStandardItemModel*>(ui->table->model()));
  dynamic_cast<QStandardItemModel*>(ui->table->model())->appendRow(items);

  ui->table->scrollToBottom();
  ui->table->setCurrentIndex(items[j]->index());
  ui->table->edit(items[j]->index());
}

 

 

 

 

 

./CppQTableViewExample1/qtmain.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QApplication>
#include "qtdialog.h"
#pragma GCC diagnostic pop

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