实现一个表格控件并向单元格中添加内容的功能
1.新建一个无ui界面的工程,其基类为widget,向工程中添加资源文件,并加入相应的ico或者png图片文件
2.代码实现
widget.h和main.cpp不做改变。为默认
widget.cpp
#include
"widget.h"
#include
<QTableWidget>
#include
<QIcon>
Widget
::Widget(QWidget
*parent
)
: QWidget(parent
)
{
QTableWidget
* tableWidget
= new QTableWidget(10,5,this);
tableWidget
->setWindowTitle("QTableWidget & Item");
tableWidget
->resize(450,350);
this->resize(450,350);
QStringList header
;
header
<< "编号" << "名字" << "介绍";
tableWidget
->setHorizontalHeaderLabels(header
);
tableWidget
->setItem(0,0,new QTableWidgetItem("第一把枪"));
tableWidget
->setItem(1,0,new QTableWidgetItem("第二把枪"));
tableWidget
->setItem(2,0,new QTableWidgetItem("第三把枪"));
tableWidget
->setItem(0,1,new QTableWidgetItem(QIcon(":/imgs/weapen/m4.ico"),"M4"));
tableWidget
->setItem(1,1,new QTableWidgetItem(QIcon(":/imgs/weapen/m16.ico"),"M16"));
tableWidget
->setItem(2,1,new QTableWidgetItem(QIcon(":/imgs/weapen/ak47.ico"),"AK47"));
tableWidget
->setItem(0,2,new QTableWidgetItem("m4是把好枪"));
tableWidget
->setItem(1,2,new QTableWidgetItem("m16是把垃圾枪"));
tableWidget
->setItem(2,2,new QTableWidgetItem("ak47压不住"));
tableWidget
->setEditTriggers(QAbstractItemView
::NoEditTriggers
);
tableWidget
->show();
this->setWindowTitle("表格控件");
}
Widget
::~Widget()
{
}
3.效果展示
4.不足和可改进之处
1.没有添加Qtxlsx包。故不能实现导出为Excel表格
2.后期可以将QTableWdiget改变成QTableview用以实现数据库的连接
转载请注明原文地址: https://win8.8miu.com/read-4830.html