QT: QTableView Example – Short and Quick

QTableView is an interesting concept in QT. I was exploring it today and decided to post what I learnt from that.. I hope you will find it interesting/useful.

Download Source –>Download QTableView Source - Downloaded 4136 times

Create a new project and lets get started.. My folder structure is as below..

How my QT Creator Folder Strucutre Looks...

Open the forms->mainwindow.ui and insert a TableView into the form as below,

Insert TableView



 Now, open mainwindow.cpp, and drop the following code in the constructor,

QStandardItemModel *model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column1 Header")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column2 Header")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("Column3 Header")));

    ui->tableView->setModel(model);

The code should go within your constructor as in the below image,

Code View

Run the program now, and you should get the below effect…

TableView with headers

Now you have a QTableview with 3 columns and 2 rows with headers created.. We can add data to that section easily as below…

Add these code before the last line, and the table will fill the first col, first row with value..


QStandardItem *firstRow = new QStandardItem(QString("ColumnValue"));
model->setItem(0,0,firstRow);

I have attached the source code for playing around.. Have fun.

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

7 comments

  1. Sajith, I use your explanation to build my Table, but I have a problem. When I try to add a value on the last col. the value not appers.
    In your case I make this:
    model->setItem(0,2, lastRow);

    The value of lastRow it’s not showed, but if I add one more col. the value of example above appers on the row (0,2).

    Why I got this Problem? You have the same problem?
    Thanks

    1. Dante, (0,2) => 1st row & 2nd column.. What problem are u facing? when defining how do u define it.. Can you show me the code pls? I will be able to help u quicker…

      1. Hi Sajith

        Can you please me by telling me that how can i insert data into column of table widget from a text file.

  2. Some code:


    #include "tablemodelhistoricocomando.h"

    TableModelHistoricoComando::TableModelHistoricoComando(QObject *parent) :
    QObject(parent)
    {
    this->headerCreated = false;
    this->currentRow = 0;
    this->model = new QStandardItemModel(0);
    }
    QStandardItemModel* TableModelHistoricoComando::getModelo()
    {
    return this->model;
    }
    void TableModelHistoricoComando::createHeader()
    {
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("Indexador")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("Data")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("Hora")));
    model->setHorizontalHeaderItem(3, new QStandardItem(QString("Comando")));
    model->setHorizontalHeaderItem(3, new QStandardItem(QString("")));
    this->headerCreated = true;
    }
    void TableModelHistoricoComando::addHistorico(RespostaHistoricoComando *historico)
    {
    if(! this->headerCreated)
    {
    createHeader();
    }
    QStandardItem *row = new QStandardItem( QString::number(historico->getIndex()) );
    model->setItem(currentRow, 0, row);
    row = new QStandardItem( historico->getData() );
    model->setItem(currentRow, 1, row);
    row = new QStandardItem( historico->getHora() );
    model->setItem(currentRow, 2, row);
    row = new QStandardItem( historico->getDescricaoComando() );
    model->setItem(currentRow, 3, row);
    row = new QStandardItem( "" );
    model->setItem(currentRow, 4, row);
    currentRow++;
    }

    If you see my code, I add one column over then I need… This column have no values.
    If I remove this last column, the column called “Comando” will not apper on UI.
    Did you undestand me?

    Always the last column is not showed in runtime application.

Leave a Reply

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

CommentLuv badge