目录:
-
- 1. QT: QTableWidget 一些问题相关链接:
- 2. QT 绘制表格demo代码
1. QT: QTableWidget 一些问题相关链接:
QT: QTableWidget 去除序号列
QT: QTableWidget 去除表头
QT: QTableWidget 获取当前行内容,判断QTableWidget是否被点击
QT: QTableWidget可编辑设置,设置部分可编辑
QT: QTableWidget表格鼠标点击事件信号
QT: QTableWidget 获取指定行与列的单元格数据QTableWidgetItem
QT: QTableWidget遍历所有单元格
QT: QTableWidget 表头设计
2. QT 绘制表格demo代码
void PageProductInfo::create_info_table() // 绘制产品信息表格
{
// 先清空原表格ui->tableWidget_left->clear();//设置表头QTableWidgetItem *headerItem;QStringList headerText;headerText<<"选择"<<"序号"<<"产品型号"<<"图号"<<"名称"<<"备注"; //表头标题用QStringList来表示ui->tableWidget_left->setColumnCount(headerText.count());//列数设置为与 headerText的行数相等ui->tableWidget_left->setHorizontalHeaderLabels(headerText);ui->tableWidget_left->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);ui->tableWidget_left->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);//选择ui->tableWidget_left->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);//序号ui->tableWidget_left->horizontalHeader()->setMinimumHeight(64);//设置表头的样式ui->tableWidget_left->horizontalHeader()->setFixedHeight(36); //设置表头的高度ui->tableWidget_left->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:#9594A5;color: white;font-family:黑体;}");ui->tableWidget_left->horizontalHeader()->setHighlightSections(false); //表头不会因为鼠标点击选中而变色ui->tableWidget_left->setEditTriggers(QAbstractItemView::NoEditTriggers);//取消单元格的点击编辑ui->tableWidget_left->setSelectionMode(QAbstractItemView::SingleSelection);//单选ui->tableWidget_left->setSelectionBehavior(QAbstractItemView::SelectRows);//只能选中行int rowsize = m_vec_info.size();// ****************************************************// 非常非常重要,不设定总行数,会导致单元格无法插入// ****************************************************ui->tableWidget_left->setRowCount(rowsize);//总行数int rowIndex =0;// 设置表格内每一行的数据for(int i=0; i<m_vec_info.size(); i++){
ui->tableWidget_left->setRowHeight(rowIndex, 48);//设置行的高度QTableWidgetItem *item_index = new QTableWidgetItem (QString::number(rowIndex+1));// 具体内容大家可以自行定义QTableWidgetItem *item_CPXH = new QTableWidgetItem (m_vec_info[i].CPXH);QTableWidgetItem *item_TH = new QTableWidgetItem (m_vec_info[i].TH);QTableWidgetItem *item_MC = new QTableWidgetItem (m_vec_info[i].MC);QTableWidgetItem *item_BZ = new QTableWidgetItem (m_vec_info[i].BZ);//字体颜色item_index->setTextColor(QColor(255,255,255));item_CPXH->setTextColor(QColor(255,255,255));item_TH->setTextColor(QColor(255,255,255));item_MC->setTextColor(QColor(255,255,255));item_BZ->setTextColor(QColor(255,255,255));// 居中item_index->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);item_CPXH->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);item_TH->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);item_MC->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);item_BZ->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);//创建复选框控件QCheckBox *box = new QCheckBox();// 设置控件大小位置box->setGeometry(30,10,20,20);// 创建窗口和布局,将复选框控件加入布局中,再将布局添加到窗口中。QHBoxLayout *vLayout = new QHBoxLayout();QWidget *Widget_btn = new QWidget;vLayout->addWidget(box);Widget_btn->setLayout(vLayout);ui->tableWidget_left->setCellWidget(rowIndex,0,Widget_btn);ui->tableWidget_left->setItem(rowIndex,1,item_index);//添加序号ui->tableWidget_left->setItem(rowIndex,2,item_CPXH);//添加产品型号ui->tableWidget_left->setItem(rowIndex,3,item_TH);//添加图号ui->tableWidget_left->setItem(rowIndex,4,item_MC);//添加名称ui->tableWidget_left->setItem(rowIndex,5,item_BZ);//添加备注rowIndex++;}// 设置表格序号列隐藏ui->tableWidget_left->verticalHeader()->setHidden(true);// 创建表格单元格点击的信号槽,能够返回单元格所点击的行和列connect(ui->tableWidget_left,SIGNAL(cellClicked(int,int)),this,SLOT(show_prodcut_sets(int,int)));}