Qt: QTableView Tutorial I

I am interesting with QTableView. I try to search QTableView tutorial, but difficult to get this tutorial. So, I try to create basic QTableView Tutorial. This is output from my first QTableView Tutorial:

First, copy this text and save as main.cpp.

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

int main( int argc, char **argv )
{
    QApplication app( argc, argv );

    tableView tbl;
    tbl.show();

    return app.exec();
}

Second, create tableview class to show QTableView in our program. Copy this text and save as tableview.cpp

#include "tableview.h"

tableView::tableView(QWidget *parent)
    : QWidget(parent)
{
    //create layout
    QVBoxLayout *mainLayout = new QVBoxLayout();
    QHBoxLayout *horLayout1 = new QHBoxLayout;
    QHBoxLayout *horLayout2 = new QHBoxLayout;

    //create input number of row
    label1 = new QLabel(tr("row : "));
    lineeRow = new QLineEdit();
    horLayout1->addWidget(label1);
    horLayout1->addWidget(lineeRow);

    //create input number of column
    label2 = new QLabel(tr("Column : "));
    lineeCol = new QLineEdit();
    horLayout2->addWidget(label2);
    horLayout2->addWidget(lineeCol);

    lineeRow->setText("4");
    lineeCol->setText("2");
    btnApply = new QPushButton(tr("Apply"));
    connect(btnApply, SIGNAL(clicked()), this, SLOT(btnApply_clicked()));

    //create QTableView
    tblv = new QTableView();
    tblv->setSelectionBehavior(QAbstractItemView::SelectItems );
    tblv->setSelectionMode( QAbstractItemView::ExtendedSelection );

    //setting layout
    mainLayout->addLayout(horLayout1);
    mainLayout->addLayout(horLayout2);
    mainLayout->addWidget(btnApply);
    mainLayout->addWidget(tblv);
    setLayout(mainLayout);

}

void tableView::btnApply_clicked()
{
    //get number of input row and column
    nrow = lineeRow->text().toInt();
    ncol = lineeCol->text().toInt();

    //create model
    QStandardItemModel *model = new QStandardItemModel( nrow, ncol, this );

    //create QTableview Horizontal Header
    for (int r=0; r<ncol; r++)
        model->setHorizontalHeaderItem( r, new QStandardItem( QString("Column_ %0" ).arg(r)) );

    //fill model value
    for( int r=0; r<nrow; r++ )
    {
        for( int c=0; c<ncol; c++)
        {
            QString sstr = "[ " + QString::number(r) + " , " + QString::number(c) + " ]";
            QStandardItem *item = new QStandardItem(QString("Idx ") + sstr);
            model->setItem(r, c, item);
        }
    }

    //set model
    tblv->setModel(model);
}

Third, create tableview header. Copy this text and save as tableview.h

#ifndef TABLEVIEW_H
#define TABLEVIEW_H

#include <QtGui>
#include <QWidget>
#include <QTableView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QString>

class tableView : public QWidget
{
    Q_OBJECT
public:
    tableView(QWidget *parent = 0);

private:
    QTableView *tblv;
    QLabel *label1, *label2;
    QLineEdit *lineeRow, *lineeCol;
    QPushButton *btnApply;
    int nrow, ncol;

private slots:
    void btnApply_clicked();
};

#endif // TABLEVIEW_H

Create pro file, copy this text as tblview1.pro

SOURCES += tableview.cpp \
    main.cpp
HEADERS += tableview.h

Compile program with command qmake and make. You can download  this QTableview tutorial source code at here.

8 Comments

Add a Comment

Your email address will not be published. Required fields are marked *