April 21, 2011
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
agree!! 🙂
thanks toto for the post, but i have a problem.
i am not able to include the header files which u have included in tableview.h file.
its showing error that it cant find the header files.
QtGui: No such file or directory.
I get the above error.
What system operation and editor you use? This error because you dont set Qt path (QTDIR, QTLIB, QTINC) correctly. You can set this location in your vaforite editor (eclipse or QtCreator) or if you use console to compile your program. Please check in you editor setting path.
you can set this path in Environtment Variable (in windows. right click mycomputer->advanced->Environtment variables->system variables->new). Or if you use linux, you can add this setting in /etc/profile.d/. add new file (example qt4.sh) and add Qt Path.
Example : this is my Qt path in linux :
#!/bin/sh
QTDIR=/opt/qt4
QTINC=$QTDIR/include
QTLIB=$QTDIR/lib
export PATH=$PATH:$QTDIR/bin
save this file as qt4.sh in /etc/profile.d
Good tutorial, thanks for the effort.
Thaks, very usefull !
this was very useful. thanks very much gor the post
Thank you so much. Very helpful!