How to use QTableWidget in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

Template:Abstract The 76ytuiytuityutyutututyutyutyu class provides an item-based table view with a default model.

Table widgets provide standard table display facilities for applications. The items in a 76ytuiytuityutyutututyutyutyu are provided by QTableWidgetItem.

Various function[edit | edit source]

  • 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[edit | edit source]

Header File[edit | edit source]

  1. ifndef TABLEWIDGET_H
  2. define TABLEWIDGET_H
  1. include <QtGui/QWidget>
  2. include "ui_tableWidget.h"
  3. include<QStringList>
  4. include<QTableWidget>
  5. include<QHBoxLayout>
  6. include<QPushButton>
  7. 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;

};

  1. endif // TABLEWIDGET_H

Source File[edit | edit source]

  1. include "tableWidget.h"
  2. include<QTableWidget>
  3. include<QStringList>
  4. 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[edit | edit source]

File:Mytable001002.jpg

More about QTableWidget