TableWidget table structure component, which can be regarded as the advanced version of TreeWidget tree component. Compared with tree structure component, table component is more flexible. It not only provides the function of outputting and displaying two-dimensional tables, but also can directly edit and modify table elements. Table structure is divided into header and data in table, The table structure can be regarded as a two-dimensional array, and specific elements can be locked through the array rows and columns. The following code is the basic use method for the table structure, which realizes the initialization of header data, element insertion and other basic operations respectively.
Before studying the Widget component, first get familiar with the View component. Compared with the Widget component, the View component only does not have editing function, and other functions are consistent. The View component supports the establishment of mapping relationship with the database. If the table does not need to be updated, it is best to use the View component. The code for creating the table by the View component is as follows.
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <QStandardItemModel> QStandardItemModel *model = new QStandardItemModel(); MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // Initialize tableView header model->setColumnCount(3); model->setHeaderData(0,Qt::Horizontal,QString("account number")); model->setHeaderData(1,Qt::Horizontal,QString("user")); model->setHeaderData(2,Qt::Horizontal,QString("Age")); ui->tableView->setModel(model); ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); // The header is displayed on the left //Set column width ui->tableView->setColumnWidth(0,101); ui->tableView->setColumnWidth(1,102); } MainWindow::~MainWindow() { delete ui; } // Add data to table // https://www.cnblogs.com/lyshark void MainWindow::on_pushButton_clicked() { for(int i = 0; i < 5; i++) { model->setItem(i,0,new QStandardItem("20210506")); //Set character color model->item(i,0)->setForeground(QBrush(QColor(255, 0, 0))); //Set character position model->item(i,0)->setTextAlignment(Qt::AlignCenter); model->setItem(i,1,new QStandardItem(QString("lyshark"))); model->setItem(i,2,new QStandardItem(QString("24"))); } }
The code runs as follows:
The initialization of Widget component is basically consistent with that of View component. When the program runs, first execute the following code in the constructor to initialize the table.
// https://www.cnblogs.com/lyshark MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QStringList header; header << "full name" << "Gender" << "Age"; ui->tableWidget->setColumnCount(header.size()); // Sets the number of columns in the table ui->tableWidget->setHorizontalHeaderLabels(header); // Set horizontal head ui->tableWidget->setRowCount(5); // Set total rows ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); // Set the table structure to be non editable by default // Initialize the edit box and other properties on the right ui->radioButton->setChecked(true); ui->lineEdit_1->setText(""); ui->lineEdit_2->setText(""); // Fill data QStringList NameList; NameList << "lyshark A" << "lyshark B" << "lyshark C"; QStringList SexList; SexList << "male" << "male" << "female"; qint32 AgeList[3] = {22,23,43}; // Using NameList[x] for getting elements has the same effect as using NameList.at(x) for(int x=0;x< 3;x++) { int col =0; // Add name ui->tableWidget->setItem(x,col++,new QTableWidgetItem(NameList[x])); // Add gender ui->tableWidget->setItem(x,col++,new QTableWidgetItem(SexList.at(x))); // Add age ui->tableWidget->setItem(x,col++,new QTableWidgetItem( QString::number(AgeList[x]) ) ); } } MainWindow::~MainWindow() { delete ui; }
The code runs as follows:
Next, add some binding events to the buttons in Ui. Here, we bind the following through the connect binding signal:
- UI - > pushbutton bind add signal
- ui->pushButton_ 2 binding deletion signal
- ui->pushButton_ 3 bind to get cell signal
- ui->pushButton_ 4 binding modification signal
Add add button signal: bind a signal slot to the Add button, and click the button to add
connect(ui->pushButton,&QPushButton::clicked,[=](){ QString Uname = ui->lineEdit_1->text(); QString Usex = "male"; int Uage = 0; if(ui->radioButton->isChecked()) Usex = "male"; if(ui->radioButton_2->isChecked()) Usex = "female"; Uage =(ui->lineEdit_2->text()).toInt(); // Before adding, judge whether Uname exists in the TableWidget. If it exists, return 0 and if not, return 1 bool isEmpty = ui->tableWidget->findItems(Uname,Qt::MatchExactly).empty(); if(isEmpty) { ui->tableWidget->insertRow(0); // Add an empty list at the beginning of the line ui->tableWidget->setItem(0,0,new QTableWidgetItem(Uname)); ui->tableWidget->setItem(0,1,new QTableWidgetItem(Usex)); ui->tableWidget->setItem(0,2,new QTableWidgetItem( QString::number(Uage))); } });
Add delete button signal: click the button to delete the selected line
connect(ui->pushButton_2,&QPushButton::clicked,[=](){ bool isEmpty = ui->tableWidget->findItems(ui->lineEdit_1->text(),Qt::MatchExactly).empty(); if(!isEmpty) { // Navigate to the line number of the line int row = ui->tableWidget->findItems(ui->lineEdit_1->text(),Qt::MatchExactly).first()->row(); // Release resources ui->tableWidget->removeRow(row); } });
Add release cell button signal: obtain the currently selected cell and release the current single cell
connect(ui->pushButton_3,&QPushButton::clicked,[=](){ int row = ui->tableWidget->currentRow(); std::cout << row << std::endl; QTableWidgetItem *table = ui->tableWidget->currentItem(); delete(table); });
Add and modify cell button signal: add and modify the processing flow of the specified content
connect(ui->pushButton_4,&QPushButton::clicked,[=](){ QTableWidgetItem *cellItem; // Fetch the currently selected row int curr_row = ui->tableWidget->currentRow(); // Number of circular columns // https://www.cnblogs.com/lyshark for(int col=0; col<ui->tableWidget->columnCount(); col++) { // Find pointer to current column cellItem = ui->tableWidget->item(curr_row,col); // Circular output column name std::cout << cellItem->text().toStdString().data() << std::endl; // First deal with the first name, read it out and write it back to column 0 of the list if(col == 0) cellItem->setText(ui->lineEdit_1->text()); // Judge the gender and write back to column 1 respectively if(col == 1) { if(ui->radioButton->isChecked()) cellItem->setText("male"); if(ui->radioButton_2->isChecked()) cellItem->setText("female"); } // Judge the age and write back to column 3 if(col == 2) cellItem->setText(ui->lineEdit_2->text()); } });
After signal binding, the code runs as follows: