当前位置:网站首页>QT qtablewidget table column top requirements ideas and codes
QT qtablewidget table column top requirements ideas and codes
2022-07-04 05:56:00 【Program yuan ZCC】
QT Project use QTableWidget when Requirements encountered :
The top of the table column 、 Cancel topping
Click a column to top , If there is no top column, set it to the first column , If there is a top row in front, row behind the top row .
When canceling the top column , Restore to the last position of the top column , If it is the last column at the top , Don't move the position
( In the example , Because there is also a header column , So the program 1 In the first column )
General idea :
There are custom classes in each column of table items , If you delete and then create a new one, it will be more troublesome .
So I used , First create a new column , Then copy the contents of each table item in the clicked column to the new column , Then delete the click column
Project example operation process :
1、 First select the program 1 Top to the first column

2、 Select show 3 Roof placement Will be scheduled to the program 1 Back


2、 And so on Select show 4 Roof placement Will be scheduled to the program 3 Back ( At this time, if you cancel the top program 4, Position the same )

3、 Click on the program 3 Cancel topping , Then it will be ranked to the last item in the top column , And so on

Main code :
// Roof placement
void MyManagement::setColTop() {
int curCol = clickedTableCol;// Currently click on the column
int toCol = 0;
for(int s = 0;s<programList.size();s++){//programList All column arrays
if(programList[s]->isTop == 1) {//isTop Is it the top
toCol++;
}
}
toCol++;// The number of columns to top , If there is no top, go to the first column , If there is a set rule, set the top number +1
QWidget *item = qTableWidget->cellWidget(0,curCol);
// TableWidgetHeadItem *item = dynamic_cast<TableWidgetHeadItem*>(ui->qTableWidget->cellWidget(0,curCol));
qDebug() << " curCol11== " << curCol;
ui->qTableWidget->insertColumn(toCol); // Insert column first , Copy the contents of the column into the new column , Delete the front row ( It's complicated not to delete first and then create a new one )
// int colnum = ui->qTableWidget->columnCount();
// ui->qTableWidget->setColumnCount(colnum);// Set number of columns
item->setStyleSheet("QLabel{color:rgb(173, 53, 0);}");
qTableWidget->setCellWidget(0, toCol, item);// Header
int nRowCount = qTableWidget->rowCount(); // Copy the contents of the table
for(int row=1; row<nRowCount; row++){
qDebug() << "==row== " << row << " curCol== "<< curCol;
QWidget *wdg = qTableWidget->cellWidget(row, curCol + 1);// The column has been inserted before To get the contents of cells, add 1
qTableWidget->setCellWidget(row, toCol, wdg);
}
qTableWidget->removeColumn(curCol + 1);// Delete the click column , Top to the front column , Delete by +1
// Modify the attributes in the array
for(int s = 0;s<programList.size();s++){
if(programList[s]->col == curCol) {
programList[s]->isTop = 1;
programList[s]->col = toCol;
}else if(programList[s]->col < curCol && programList[s]->isTop == 0){// Click behind the column to be placed at the top , The front is not the top +1
programList[s]->col += 1;
}
}
clickedTableCol = toCol;
qDebug() << " After topping col Location " << clickedTableCol;
qTableWidget->setCurrentCell(0, clickedTableCol);
// ui->qTableWidget->resizeColumnsToContents();
// ui->qTableWidget->resizeRowsToContents();
}// Cancel topping
void MyManagement::cancelColTop() {
int curCol = clickedTableCol;
int toCol = 0;
for(int s = 0;s<programList.size();s++){
if(programList[s]->isTop == 1) {
toCol++;
}
}
qDebug() << " Cancel topping curCol11== " << curCol << " toCol== " << toCol;
QWidget *item = qTableWidget->cellWidget(0,curCol);
item->setStyleSheet("QLabel{color:rgb(255, 255, 255);}");
if(toCol == curCol) {// At the end of the top Position the same
for(int s = 0;s<programList.size();s++){
if(programList[s]->col == curCol) {
programList[s]->isTop = 0;
}
}
} else {
qTableWidget->insertColumn(toCol + 1);// Top the last column +1 Insert
qTableWidget->setCellWidget(0, toCol + 1, item);
int nRowCount = qTableWidget->rowCount(); // Copy the contents of the table
for(int row=1; row<nRowCount; row++){
qDebug() << "==row== " << row << " curCol== "<< curCol;
QWidget *wdg = qTableWidget->cellWidget(row, curCol);// The column has been inserted before To get the contents of cells, add 1
qTableWidget->setCellWidget(row, toCol + 1, wdg);
}
qTableWidget->removeColumn(curCol);
for(int s = 0;s<programList.size();s++){
if(programList[s]->col == curCol) {// First modify the current column
programList[s]->isTop = 0;
programList[s]->col = toCol;
} else if(programList[s]->col > curCol){// Larger than the current top column
if(programList[s]->isTop == 1) {// For the top column
programList[s]->col -= 1;
}
}
}
}
clickedTableCol = toCol;
qDebug() << " After canceling the top col Location " << clickedTableCol << " toCol== " << toCol;
qTableWidget->setCurrentCell(0, clickedTableCol);
}边栏推荐
- QT 获取随机颜色值设置label背景色 代码
- JS how to convert seconds into hours, minutes and seconds display
- 总线的基本概念
- BUU-Real-[PHP]XXE
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的 锁机制 分析
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- JS arguments parameter usage and explanation
- 注释与注解
- fastjson
- (4) Canal multi instance use
猜你喜欢
![Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai](/img/a5/be967170a18cb2de097f01a6d4e039.jpg)
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai

buuctf-pwn write-ups (8)

Leakage detection relay jy82-2p

我的NVIDIA开发者之旅——优化显卡性能

Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)

Compound nonlinear feedback control (2)

HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)

【微服务】Nacos集群搭建以及加载文件配置

19. Framebuffer application programming

buuctf-pwn write-ups (8)
随机推荐
我的NVIDIA开发者之旅——优化显卡性能
Use of hutool Pinyin tool
fastjson
一键过滤选择百度网盘文件
Sword finger offer II 038 Daily temperature
Descriptive analysis of data distribution characteristics (data exploration)
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
(4) Canal multi instance use
px em rem的区别
【无标题】
Uninstall Google drive hard drive - you must exit the program to uninstall
Install pytoch geometric
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
left_and_right_net可解释性设计
Win10 clear quick access - leave no trace
ansys命令
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
[excel] PivotChart
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述
Gridview出现滚动条,组件冲突,如何解决