Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QTableWidget

 

QTableWidget is a Qt class to view a table.

 

 

 

 

 

QTableWidget code snippets

 

  1. SetReadOnlyTableWidget

 

 

 

 

 

Full QTableWidget example project

 

 

Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: GUI application

Compiler: G++ 4.4.1

Libraries used:

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-09-04T16:12:54
#
#-------------------------------------------------
QT       += core gui
TARGET = CppQTableWidget
TEMPLATE = app
SOURCES += main.cpp\
        dialog.cpp
HEADERS  += dialog.h
FORMS    += dialog.ui
RESOURCES += \
    resources.qrc

 

 

 

 

 

dialog.cpp

 

//---------------------------------------------------------------------------
#include <string>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include "dialog.h"
#include "ui_dialog.h"
//---------------------------------------------------------------------------
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
  ui->setupUi(this);
  QTableWidget * t = ui->tableWidget;

  const int n_rows = t->rowCount();
  const int n_cols = t->columnCount();
  for (int x=0; x!=n_cols; ++x)
  {
    for (int y=0; y!=n_rows; ++y)
    {
      const std::string s = "("
        + boost::lexical_cast<std::string>(x)
        + ","
        + boost::lexical_cast<std::string>(y)
        + ")";
      QTableWidgetItem * i = new QTableWidgetItem(QString(s.c_str()));
      t->setItem(y, x, i);
    }
  }

}
//---------------------------------------------------------------------------
Dialog::~Dialog()
{
  delete ui;
}
//---------------------------------------------------------------------------
void Dialog::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------

 

 

 

 

 

dialog.h

 

#ifndef DIALOG_H
#define DIALOG_H
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
namespace Ui {
  class Dialog;
}
//---------------------------------------------------------------------------
class Dialog : public QDialog
{
  Q_OBJECT

public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::Dialog *ui;
};
//---------------------------------------------------------------------------
#endif // DIALOG_H

 

 

 

 

 

main.cpp

 

#include <QtGui/QApplication>
#include "dialog.h"
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog 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