How to use QTableWidget in Qt
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt
Article
Keywords: QTableWidget
Created: mind_freak
(28 Mar 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This article show how to use QTableWidget The QTableWidget class provides an item-based table view with a default model.
Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.
Various function
- Sets the horizontal header labels using labels.
list<<"No."<<"Name"<<"Adder."<<"City"<<"Phone No.";
widget->setHorizontalHeaderLabels(list);
- This property holds the number of rows in the table.
widget->setRowCount(5);
- This property holds the number of columns in the table.
widget->setColumnCount(5);
- This is used to set the widget in the table.
widget->setCellWidget(0,1,lbl);
Source code
Header File
#ifndef TABLEWIDGET_H
#define TABLEWIDGET_H
#include <QtGui/QWidget>
#include "ui_tableWidget.h"
#include<QStringList>
#include<QTableWidget>
#include<QHBoxLayout>
#include<QPushButton>
#include<QLabel>
class tableWidget : public QWidget
{
Q_OBJECT
public:
tableWidget(QWidget *parent = 0);
~tableWidget();
private:
QTableWidget *widget;
QHBoxLayout *layout;
QStringList list;
QPushButton *but1;
QLabel *lbl;
};
#endif // TABLEWIDGET_H
Source File
#include "tableWidget.h"
#include<QTableWidget>
#include<QStringList>
#include<QPushButton>
tableWidget::tableWidget(QWidget *parent)
: QWidget(parent)
{
layout=new QHBoxLayout(this);
list<<"No."<<"Name"<<"Adder";
widget=new QTableWidget(this);
but1=new QPushButton("Press",this);
widget->setRowCount(3);
widget->setColumnCount(15);
widget->setHorizontalHeaderLabels(list);
widget->setCellWidget(0,0,but1);
widget->setCellWidget(0,1,lbl);
layout->addWidget(widget);
setLayout(layout);
}
tableWidget::~tableWidget()
{
// No need to delete any object it has got a parent which is properly deleted.
}
Screenshot
More about QTableWidget



(no comments yet)