一、表
1、QTableWidget
QTableWidget是Qt中最常见的显示数据表格的控件,它是QTableView的子类。它必须使用标准的数据模型,每一个单元格必须QTableWidgetItem对象实现。
2、常用方法
1)、构造函数,创建指定行和列的表
QTableWidget ( int rows, int columns, QWidget * parent = 0 )
2)、普通函数
void setHorizontalHeaderItem ( int column, QTableWidgetItem * item )
void setHorizontalHeaderLabels ( const QStringList & labels )
void setVerticalHeaderItem ( int row, QTableWidgetItem * item )
void setVerticalHeaderLabels ( const QStringList & labels )
setItem ( int row, int column, QTableWidgetItem * item )
void setCellWidget ( int row, int column, QWidget * widget )
int rowCount ()
int columnCount ()
3)、槽函数
void insertColumn ( int column )
void insertRow ( int row )
void removeColumn ( int column )
void removeRow ( int row )
4)、信号
void cellChanged ( int row, int column )
void cellClicked ( int row, int column )
void cellDoubleClicked ( int row, int column )
void itemChanged ( QTableWidgetItem * item )
void itemClicked ( QTableWidgetItem * item )
void itemDoubleClicked ( QTableWidgetItem * item )
二、单元格
1、QTableWidgetItem
QTableWidget表格的建立必须依靠单元格,整个表格都是由单元格建立起来的
2、常用方法
1)、构造函数,创建指定行和列的表
QTableWidgetItem ( const QString & text, int type = Type )
QTableWidgetItem ( const QIcon & icon, const QString & text, int type = Type )
2)、普通函数
void setBackground ( const QBrush & brush )
void setText ( const QString & text )
int textAlignment () const
void setIcon ( const QIcon & icon )
三、实例
1、头文件
-
#ifndef TABLEWIDGET_H
-
#define TABLEWIDGET_H
-
#include <QtGui>
-
-
class tableWidget : public QDialog
-
{
-
Q_OBJECT
-
public:
-
tableWidget();
-
private:
-
QPushButton *addRowBtn, *addColBtn, *removeRowBtn, *removeColBtn;
-
QTableWidget *table;
-
-
public slots:
-
void addRow();
-
void addCol();
-
void removeRow();
-
void removeCol();
-
};
-
-
#endif
2、cpp
-
#include "tableWidget.h"
-
-
tableWidget::tableWidget()
-
{
-
//控件初始化
-
addRowBtn = new QPushButton("增加行");
-
addColBtn = new QPushButton("增加列");
-
removeRowBtn = new QPushButton("删除行");
-
removeColBtn = new QPushButton("删除列");
-
table = new QTableWidget(2,3);
-
//布局
-
QVBoxLayout *vLay = new QVBoxLayout();
-
vLay->addWidget(addRowBtn);
-
vLay->addWidget(addColBtn);
-
vLay->addWidget(removeRowBtn);
-
vLay->addWidget(removeColBtn);
-
QHBoxLayout *hLay = new QHBoxLayout();
-
hLay->addWidget(table);
-
hLay->addLayout(vLay);
-
setLayout(hLay);
-
//绑定信号和槽函数
-
connect(addRowBtn, SIGNAL(clicked()), this, SLOT(addRow()));
-
connect(addColBtn, SIGNAL(clicked()), this, SLOT(addCol()));
-
connect(removeRowBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
-
connect(removeColBtn, SIGNAL(clicked()), this, SLOT(removeCol()));
-
//设置表头
-
QStringList head;
-
head<<"姓名"<<"年龄"<<"性别";
-
table->setHorizontalHeaderLabels(head);
-
//设置单元格
-
table->setItem(0, 0, new QTableWidgetItem("魏杰"));
-
table->setItem(0, 1, new QTableWidgetItem("30"));
-
QComboBox *com1 = new QComboBox();
-
QComboBox *com2 = new QComboBox();
-
QStringList sex;
-
sex<<"男"<<"女";
-
com1->addItems(sex);
-
com2->addItems(sex);
-
table->setCellWidget(0, 2, com1);
-
table->setCellWidget(1, 2, com2);
-
-
resize(800, 800);
-
setWindowTitle("hehe");
-
-
}
-
-
void tableWidget::addRow()
-
{
-
int num = table->rowCount();
-
table->insertRow(num);
-
}
-
void tableWidget::addCol()
-
{
-
int num = table->columnCount();
-
table->insertColumn(num);
-
}
-
void tableWidget::removeRow()
-
{
-
int num = table->rowCount();
-
table->removeRow(num-1);
-
}
-
void tableWidget::removeCol()
-
{
-
int num = table->columnCount();
-
table->removeColumn(num-1);
-
-
}
阅读(1454) | 评论(0) | 转发(0) |