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. BUILD FAILED D:\build.xml:2: 前言中不同意有内容。

    1.错误描写叙述 Microsoft Windows [版本号 6.1.7601] 版权全部 (c) 2009 Microsoft Corporation. 保留全部权利. C:\Users\Admi ...

  2. 【Android】自己定义View

    翻译自:http://developer.android.com/training/custom-views/index.html 一)创建view类 一个设计良好的自己定义view与其它的类一样.它 ...

  3. 如何抓取基于https协议的webservice数据包

    方法一:基于Fiddler2等第三方工具(需要在Java端禁用SSL安全检查) 原文拷贝自http://blog.csdn.net/zmxj/article/details/6327775,向原作者表 ...

  4. 【USACO 2017FEB】 Why Did the Cow Cross the Road III

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...

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

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

  6. 28. extjs中Ext.BLANK_IMAGE_URL的作用

    转自:https://blog.csdn.net/yiyuhanmeng/article/details/6960132 在使用ExtJS时,我们往往需要在使用之前加入这么一句:Ext.BLANK_I ...

  7. 06_锅炉压力案例_progressbar实现

    相关的native方法可以用javah来生成一个头文件.拿着这个的MainActivity,用它来生成一个头文件. Signature是当前这个方法的方法签名.() V全空参数返回的是void. /* ...

  8. CodeForces 382C Arithmetic Progression (排序+分类讨论)

    题意:给出一个长度为n的序列,表示有n张卡片,上面的数字,现在还有一张卡片,上面没有数字,问说可以写几种数字在这张卡片上面, 使得n+1张卡片上的数字可以排列成一个等差数列,有无限多种时输出-1. 析 ...

  9. E20171212-hm

    odd   adj. 古怪的; 奇数的; 剩余的; 临时的; odd number 奇数 even adj. 偶数的 even number 偶数

  10. MARK ZUCKERBERG, A letter to our daughter(转)

    A letter to our daughter   MARK ZUCKERBERG·WEDNESDAY, DECEMBER 2, 2015   Dear Max, Your mother and I ...