1.列表的添加

    需求:向一个有两列的Table中添加一条数据

    思路:新建一个inputDialog,通过按钮打开Qt自带的inputDialog,传递回输入的数据,再添加到列表中

    界面:

        

    代码:

    inputDialog.h  

#ifndef INPUTDIALOG_H
#define INPUTDIALOG_H #include <QDialog> namespace Ui {
class InputDialog;
} class InputDialog : public QDialog
{
Q_OBJECT public:
explicit InputDialog(QWidget *parent = );
~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);
}

  通过点击inputDialog上的确定按钮,将key和value的值组装成一个List,通过信号函数sendDataList()发射,等待被接收。

  接下来是主界面上接收,通过点击界面上的add按钮,弹出对话框:

 //初始化inputDialog
inputDialog=new InputDialog();
inputDialog->setModal(true); //总在最前
connect(inputDialog,SIGNAL(sendDataList(QList<QString>*)),this,SLOT(ReceiveData(QList<QString>*)));

  连接inputDialog发射的信号和主窗体的槽函数。

 inputDialog->show();
inputDialog->exec();
if(inputDialog->Accepted==QDialog::Accepted){
DataUtil *dataUtil=new DataUtil();
dataUtil->AddEditedRow(ui->HardConTable,inputDataList->at(),inputDataList->at());
}

  如果点击ok,调用添加行的函数。

bool DataUtil::AddEditedRow(QTableWidget *table,QString key,QString value){
if(table==NULL||key==""||value=="") return false; for(int i=;i<table->rowCount();i++){
if(key==table->item(i,)->text()) return true;
} table->insertRow(table->rowCount());
QTableWidgetItem *newItem=new QTableWidgetItem();
newItem->setText(key);
table->setItem(table->rowCount()-,,newItem); QTableWidgetItem *newItem1=new QTableWidgetItem();
newItem1->setText(value);
table->setItem(table->rowCount()-,,newItem1);
return true;
}

被添加行总是接在最后一行。

=========================分割线========================================================================================

2.列表的整行删除操作

  需求:通过点击界面上的remove按钮,(表有两列)则移除选中行,支持多行选择删除。

  思路历程:一开始想的是,通过QTableWidget的 selectedItems()方法返回一个List,再遍历整个列表中的内容,相同则删除。但是发现不行,因为在添加的时候每个单元格是一个item,那选择一行,selectItems()返回的是两个,而我只需要一行的数据,准确的说只需要一行的第一列的数据,通过对比第一列的数据是否相同就可以决定是否删除。(刚才反应过来了,如果遍历selectedlist的时候,每次跳一个读取就是选中行的第一列)昨天脑子不太好用)

if(table->columnCount()==){ //两列
QList<QList<QString>*> *libsList=new QList<QList<QString>*>();
QList<QList<QString>*> *SelectedLibs=new QList<QList<QString>*>(); for(int i=;i<table->rowCount();i++){
QList<QString> *libL=new QList<QString>(); libL->append(table->item(i,)->text());
libL->append(table->item(i,)->text());
libsList->append(libL);
}
for(int index=;index<table->selectedItems().count();index+=){ //加2
QList<QString> *SelectedL=new QList<QString>();
SelectedL->append(table->selectedItems().at(index)->text()); //该行第一列
SelectedL->append(table->selectedItems().at(index+)->text());//该行第二列
SelectedLibs->append(SelectedL);
}
this->removeListItems(SelectedLibs,libsList); //逐行删除libsList中与SelectedLibs相同的行(只比较第一列的值)
showListTable(table,libsList); //将修改过的libsList重新显示在列表中
return true;
}else{
return false;
}

下面是调用的两个函数:

bool DataUtil::removeListItems(QList<QList<QString> *> *SelectedLibs,QList<QList<QString> *> *AllLibsList){
if(SelectedLibs==NULL||AllLibsList==NULL) return false;
for(int i=;i<SelectedLibs->count();i++){
for(int j=;j<AllLibsList->count();j++){
if(SelectedLibs->at(i)->at()==AllLibsList->at(j)->at()){
AllLibsList->removeAt(j);
}
}
}
return true;
}
bool DataUtil::showListTable(QTableWidget *table,QList<QList<QString>* >  *LibsList){
//清空
table->setRowCount(); for( int i=;i<LibsList->count();i++){
this->AddEditedRow(table,LibsList->at(i)->at(),LibsList->at(i)->at());
}
return true;
}

ok,到此完毕。

  

Qt 杂记——QTableWidget列表添加、删除(备份)的更多相关文章

  1. vue实现购物清单列表添加删除

    vue实现购物清单列表添加删除 一.总结 一句话总结: 基础的v-model操作,以及数组的添加(push)删除(splice)操作 1.checkbox可以绑定数组,也可以直接绑定值? 绑定数组就是 ...

  2. 向redis中添加删除list列表

    转: 向redis中添加删除list列表 2018年04月18日 15:44:54 luo_yu_1106 阅读数:4082   一.添加 向redis中添加队列有两种方式 1.lpush l是lef ...

  3. 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...

  4. 动态添加删除网卡 - 每天5分钟玩转 OpenStack(156)

    这是 OpenStack 实施经验分享系列的第 6 篇. 在项目实施过程中,经常会有添加删除网卡的需求.比如一个运行数据库的 instance,初始只有一个网卡,数据库服务和备份共用这块网卡,后来为提 ...

  5. XMPP框架下微信项目总结(6)刷新好友列表(删除,添加好友)

    原理:1 服务器(openfire)添加/删除 好友,会向客户端(app)发送消息, 2 代理(xmppStreamDelegate)监听到添加/删除消息后,花名册模块(RosterModule)会在 ...

  6. SQL server 2008 Express Edition实现自动备份和自动删除备份

    1.查看SQL 版本: select @@VERSION --可以看到 Express Edition 精简免费版 Microsoft SQL Server 2008 R2 (SP2) - 10.50 ...

  7. SQL语句添加删除修改字段及一些表与字段的基本操作

    用SQL语句添加删除修改字段 1.增加字段     alter table docdsp    add dspcode char(200)2.删除字段     ALTER TABLE table_NA ...

  8. Qt applendPlainText()/append() 多添加一个换行解决方法

    Qt applendPlainText()/append() 多添加一个换行解决方法 void ConsoleDialog::appendMessageToEditor(const QString & ...

  9. 添加/删除/修改Windows 7右键的“打开方式”

    右键菜单添加/删除"打开方式" 此"打开方式"非系统的"打开方式",二者可以并存. 右键菜单添加"打开方式" 在HKEY ...

随机推荐

  1. react 执行 yarn build 页面无法显示

    资源文件路径问题 如果你使用create-react-app创建项目,执行命令 yarn build 后,直接以静态方式打开build文件夹内的index.html,会看到页面显示出现问题,打开con ...

  2. hdu oj 1285 确定比赛名次

    hdu oj 1285 确定比赛名次 题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  3. Creo二次开发—内存处理

    #include <ProDisplist.h> ProError ProDisplistInvalidate(ProMdl model) Invalidates the two- or ...

  4. JavaScript变量提升演示样例

    直接先看两段代码 function getSum() { var sum = a + b; var a = 1; var b = 2; return sum; } getSum(); function ...

  5. ldd LD_TRACE_LOADED_OBJECTS

    1 该环境变量设置为1的话,只会打印所执行的程序的依赖,即所依赖的动态链接库

  6. luogu 3865 【模板】ST表

    我太菜了 今天才学会现场脑补ST表静态RMQ #include<iostream> #include<cstdio> #include<algorithm> #in ...

  7. python-----写入txt用法

    代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/14 11:23 # @Author : zxb file_p ...

  8. 逻辑频道号---DVB NIT LCN

    先介绍NIT,NIT描述如下: 有一点要注意,NIT是对大网的描述,即NIT并不是描述当前的流,而是描述大网的某些或者全部流.如下图,TS流描述1-6共对6个频点不同的TS流进行了描述,具体对哪一个流 ...

  9. cisco secure ACS服务器搭建

    网上下载Cisco Secure Access Control System 5.2.iso的镜像,总共有两部分 http://pan.baidu.com/disk/home#path=%252FCi ...

  10. linux centos7 安装mysql-5.7.17教程(图解)

    1系统约定安装文件下载目录:/data/softwareMysql目录安装位置:/usr/local/mysql数据库保存位置:/data/mysql日志保存位置:/data/log/mysql 2下 ...