一、布局
二、引入头文件
#include <QWidgetItem> #include <QMessageBox>
三、声明槽函数
private slots: //添加元素 void addEleSlot(); //删除元素 void deleteEleSlot(); //删除全部 void deleteAllEleSlot();
四、实现槽函数
//添加元素 void MainWindow::addEleSlot(){ QString eleName = ui->eleInputEdit->text(); if(eleName.isEmpty()){ QMessageBox::information(this,"错误提示","需要添加的元素不能为空"); return ; } QListWidgetItem *item = new QListWidgetItem; item->setText(eleName); ui->listWidgetShowView->addItem(item); ui->eleInputEdit->clear(); } //删除元素 void MainWindow::deleteEleSlot(){ QListWidgetItem *item = ui->listWidgetShowView->currentItem(); delete item; } //删除所有 void MainWindow::deleteAllEleSlot(){ //ui->listWidgetShowView->clear(); int countNum = ui->listWidgetShowView->count(); //QMessageBox::information(this,"msg",QString::number(countNum)); for(int i=countNum; i >= 0; i--){ QListWidgetItem *item = ui->listWidgetShowView->takeItem(i); delete item; } }
五、添加信号与槽的连接
QObject::connect(ui->addNewButton,SIGNAL(clicked()),this,SLOT(addEleSlot())); QObject::connect(ui->eleInputEdit,SIGNAL(returnPressed()),this,SLOT(addEleSlot())); QObject::connect(ui->deleteAllEleButton,SIGNAL(clicked()),this,SLOT(deleteAllEleSlot())); QObject::connect(ui->deleteSelectButton,SIGNAL(clicked()),this,SLOT(deleteEleSlot()));
版权声明:
此文为本站源创文章[或由本站编辑从网络整理改编],
转载请备注出处:
[狂码一生]
https://www.sindsun.com/articles/16/69
[若此文确切存在侵权,请联系本站管理员进行删除!]
--THE END--