Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5708332
  • 博文数量: 409
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 8273
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-23 19:15
个人简介

qq:78080458 学习交流群:150633458

文章分类

全部博文(409)

文章存档

2019年(127)

2018年(130)

2016年(20)

2015年(60)

2014年(41)

2013年(31)

分类: 嵌入式

2016-02-02 11:33:49

一、表
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、头文件

点击(此处)折叠或打开

  1. #ifndef TABLEWIDGET_H
  2. #define TABLEWIDGET_H
  3. #include <QtGui>

  4. class tableWidget : public QDialog
  5. {
  6. Q_OBJECT
  7. public:
  8.     tableWidget();
  9. private:
  10.     QPushButton *addRowBtn, *addColBtn, *removeRowBtn, *removeColBtn;
  11.     QTableWidget *table;

  12. public slots:
  13.     void addRow();
  14.     void addCol();
  15.     void removeRow();
  16.     void removeCol();
  17. };

  18. #endif
2、cpp

点击(此处)折叠或打开

  1. #include "tableWidget.h"

  2. tableWidget::tableWidget()
  3. {
  4.     //控件初始化
  5.     addRowBtn = new QPushButton("增加行");
  6.     addColBtn = new QPushButton("增加列");
  7.     removeRowBtn = new QPushButton("删除行");
  8.     removeColBtn = new QPushButton("删除列");
  9.     table = new QTableWidget(2,3);
  10.     //布局
  11.     QVBoxLayout *vLay = new QVBoxLayout();
  12.     vLay->addWidget(addRowBtn);
  13.     vLay->addWidget(addColBtn);
  14.     vLay->addWidget(removeRowBtn);
  15.     vLay->addWidget(removeColBtn);
  16.     QHBoxLayout *hLay = new QHBoxLayout();
  17.     hLay->addWidget(table);
  18.     hLay->addLayout(vLay);
  19.     setLayout(hLay);
  20.     //绑定信号和槽函数
  21.     connect(addRowBtn, SIGNAL(clicked()), this, SLOT(addRow()));
  22.     connect(addColBtn, SIGNAL(clicked()), this, SLOT(addCol()));
  23.     connect(removeRowBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
  24.     connect(removeColBtn, SIGNAL(clicked()), this, SLOT(removeCol()));
  25.     //设置表头
  26.     QStringList head;
  27.     head<<"姓名"<<"年龄"<<"性别";
  28.     table->setHorizontalHeaderLabels(head);
  29.     //设置单元格
  30.     table->setItem(0, 0, new QTableWidgetItem("魏杰"));
  31.     table->setItem(0, 1, new QTableWidgetItem("30"));
  32.     QComboBox *com1 = new QComboBox();
  33.     QComboBox *com2 = new QComboBox();
  34.     QStringList sex;
  35.     sex<<"男"<<"女";
  36.     com1->addItems(sex);
  37.     com2->addItems(sex);
  38.     table->setCellWidget(0, 2, com1);
  39.     table->setCellWidget(1, 2, com2);
  40.     
  41.     resize(800, 800);
  42.     setWindowTitle("hehe");
  43.     
  44. }

  45. void tableWidget::addRow()
  46. {
  47.     int num = table->rowCount();
  48.     table->insertRow(num);
  49. }
  50. void tableWidget::addCol()
  51. {
  52.     int num = table->columnCount();
  53.     table->insertColumn(num);
  54. }
  55. void tableWidget::removeRow()
  56. {
  57.     int num = table->rowCount();
  58.     table->removeRow(num-1);
  59. }
  60. void tableWidget::removeCol()
  61. {
  62.     int num = table->columnCount();
  63.     table->removeColumn(num-1);

  64. }





阅读(1381) | 评论(0) | 转发(0) |
0

上一篇:lesson5-文件布局

下一篇:lesson7-容器类

给主人留下些什么吧!~~