Qt Miscellaneous -- add, delete (Backup) and qtqtablewidget in the QTableWidget list
1. Add a list
Requirement: Add a data entry to a Table with two columns.
Idea: Create an inputDialog, open the inputDialog that comes with Qt by clicking the button, pass it back to the input data, and add it to the list.
Interface:
Code:
InputDialog. h
#ifndef INPUTDIALOG_H#define INPUTDIALOG_H#include <QDialog>namespace Ui {class InputDialog;}class InputDialog : public QDialog{ Q_OBJECTpublic: explicit InputDialog(QWidget *parent = 0); ~InputDialog();signals: void sendDataList(QList<QString> *inputDataList);private slots: void on_buttonBox_accepted();private: Ui::InputDialog *ui;};#endif // INPUTDIALOG_H
InputDialog. cpp
#include "inputdialog.h"#include "ui_inputdialog.h"InputDialog::InputDialog(QWidget *parent) : QDialog(parent), ui(new Ui::InputDialog){ ui->setupUi(this);}InputDialog::~InputDialog(){ delete ui;}void InputDialog::on_buttonBox_accepted(){ QList<QString> *inputDataList=new QList<QString>(); inputDataList->append(ui->KeyEdit->text()); inputDataList->append(ui->ValueEdit->text()); emit sendDataList(inputDataList);}
Click the OK button on inputDialog to assemble the key and value values into a List. The sendDataList () function is used to transmit the data and wait for the data to be received.
The next step is to receive the message on the main interface. Click the add button on the interface to bring up the dialog box:
// Initialize inputDialog = new InputDialog (); inputDialog-> setModal (true); // always connect (inputDialog, SIGNAL (sendDataList (QList <QString> *), this, SLOT (ReceiveData (QList <QString> *)));
Connect the signal emitted by inputDialog and the slot function of the main form.
inputDialog->show(); inputDialog->exec(); if(inputDialog->Accepted==QDialog::Accepted){ DataUtil *dataUtil=new DataUtil(); dataUtil->AddEditedRow(ui->HardConTable,inputDataList->at(0),inputDataList->at(1)); }
If you click OK, call the function for adding rows.
bool DataUtil::AddEditedRow(QTableWidget *table,QString key,QString value){ if(table==NULL||key==""||value=="") return false; for(int i=0;i<table->rowCount();i++){ if(key==table->item(i,0)->text()) return true; } table->insertRow(table->rowCount()); QTableWidgetItem *newItem=new QTableWidgetItem(); newItem->setText(key); table->setItem(table->rowCount()-1,0,newItem); QTableWidgetItem *newItem1=new QTableWidgetItem(); newItem1->setText(value); table->setItem(table->rowCount()-1,1,newItem1); return true;}
The added row is always connected to the last row.
========================================================= ========================================================== ========================================
2. Delete the entire row of the List
Requirement: Click the remove button on the page (two columns in the table) to remove the selected row. Multiple rows can be deleted.
Thought process: first, return a List through the selectedItems () method of QTableWidget, then traverse the content in the whole List, and delete the List if the content is the same. However, it cannot be found because each cell is an item when being added. Select a row and selectItems () returns two items, while I only need one row of data, to be accurate, you only need the data in the first column of a row. You can determine whether to delete the data in the first column by comparing whether the data in the first column is the same. (We just reflected that, if we traverse selectedlist, each hop reading is the first column of the selected row) yesterday's brain was not very useful)
If (table-> columnCount () = 2) {// two-column QList <QString> * libsList = new QList <QString> *> (); QList <QString> * SelectedLibs = new QList <QString> *> (); for (int I = 0; I <table-> rowCount (); I ++) {QList <QString> * libL = new QList <QString> (); libL-> append (table-> item (I, 0) -> text (); libL-> append (table-> item (I, 1)-> text (); libsList-> append (libL );} for (int index = 0; index <table-> selectedItems (). count (); index + = 2) {// Add 2 QList <QString> * SelectedL = new QList <QString> (); selectedL-> append (table-> selectedItems (). at (index)-> text (); // SelectedL-> append (table-> selectedItems () in the first column of the row (). at (index + 1)-> text (); // SelectedLibs-> append (SelectedL);} this-> removeListItems (SelectedLibs, libsList ); // Delete the rows with the same value as SelectedLibs in libsList row by row (only compare the value of the first column) showListTable (table, libsList ); // display the modified libsList in the list again and return true;} else {return false ;}
The following two functions are called:
bool DataUtil::removeListItems(QList<QList<QString> *> *SelectedLibs,QList<QList<QString> *> *AllLibsList){ if(SelectedLibs==NULL||AllLibsList==NULL) return false; for(int i=0;i<SelectedLibs->count();i++){ for(int j=0;j<AllLibsList->count();j++){ if(SelectedLibs->at(i)->at(0)==AllLibsList->at(j)->at(0)){ AllLibsList->removeAt(j); } } } return true;}
Bool DataUtil: showListTable (QTableWidget * table, QList <QString> * LibsList) {// clear table-> setRowCount (0); for (int I = 0; I <LibsList-> count (); I ++) {this-> AddEditedRow (table, LibsList-> at (I)-> at (0 ), libsList-> at (I)-> at (1);} return true ;}
OK.