QT QStringListModel 示例代码
1. QStringListModel , 实现 插入 删除 编辑 list,支持鼠标双击编辑。
2. dialog.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QtGui> class Dialog : public QDialog
{
Q_OBJECT public:
Dialog(const QStringList &leaders, QWidget *parent = 0); public slots:
void insertName();
void deleteName();
void editName(); private:
QListView *listView;
QStringListModel *model; }; #endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include <QtGui> Dialog::Dialog(const QStringList &leaders,QWidget *parent)
: QDialog(parent)
{
model = new QStringListModel;
model->setStringList(leaders); listView = new QListView;
listView->setModel(model); QPushButton *insertButton = new QPushButton(tr("insert"));
QPushButton *deleteButton = new QPushButton(tr("delete"));
QPushButton *editButton = new QPushButton(tr("edit"));
connect(insertButton, SIGNAL(clicked()), this, SLOT(insertName()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteName()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editName())); QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(insertButton);
hLayout->addWidget(deleteButton);
hLayout->addWidget(editButton);
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(listView);
vLayout->addLayout(hLayout); setLayout(vLayout);
} void Dialog::insertName()
{
bool ok;
QString name = QInputDialog::getText(this, tr("New Name"), tr(""),
QLineEdit::Normal, tr(""), &ok );
if( ok && !name.isEmpty() )
{
int row = listView->currentIndex().row();
model->insertRows(row,1);
QModelIndex index = model->index(row);
model->setData(index, name);
listView->setCurrentIndex(index);
}
} void Dialog::deleteName()
{
model->removeRows(listView->currentIndex().row(), 1);
} void Dialog::editName()
{
int row = listView->currentIndex().row();
QModelIndex index = model->index(row);
QVariant variant = model->data(index, Qt::DisplayRole);
QString name = variant.toString();
bool ok;
name = QInputDialog::getText(this, tr("Edit name"), tr(""), QLineEdit::Normal, tr(""), &ok);
if( ok && !name.isEmpty() )
{
row = listView->currentIndex().row();
index = model->index(row);
model->setData(index, name);
listView->setCurrentIndex(index);
}
}
main.cpp
#include "dialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv); QStringList leaders;
leaders << "test1" << "test2" << "test3" ;
Dialog w(leaders);
w.show(); return a.exec();
}
QT QStringListModel 示例代码的更多相关文章
- Qt QSortFilterProxyModel示例代码, 使用方法
1. QSortFilterProxyModel不能单独使用,它只是一个"代理",真正的数据需要另外的一个model提供,而且它是用来排序和过滤的. 2. 实现代码 #ifndef ...
- PyQt(Python+Qt)学习随笔:工具箱(QToolBox)编程使用的步骤及示例代码
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 使用toolBox开发应用时,通过Designer设计ui界面时,只能在Designer中设计too ...
- python开源项目及示例代码
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- python开源项目及示例代码(转)
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- 基于DotNetOpenAuth的OAuth实现示例代码: 获取access token
1. 场景 根据OAuth 2.0规范,该场景发生于下面的流程图中的(D)(E)节点,根据已经得到的authorization code获取access token. 2. 实现环境 DotNetOp ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- C#微信公众平台接入示例代码
http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...
随机推荐
- idea 右下角不显示git分支
开发工程中遇到idea右下角不显示git分支问题: 解决方案:查找资料说是需要打开VCS->Enable version control. 但是Enable version control 已经 ...
- CRM客户关系管理系统-需求概设和详设
大概设计 大概设计就是对需求进行一个整体性分析,把需要实现的功能都列出来,对于客户关系管理系统,我们需要从角色出发,从而确定有哪些需求,最好是画个思维导图 首先我们是为培训学校这么一个场景来开发的,所 ...
- MySQL版本与工具
MySQL各个版本区别 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Serve ...
- 关于在python manage.py createsuperuser时报django.db.utils.OperationalError: no such table: auth_user的解决办法
在stackflow上看到解决的办法是需要进行数据路的migrate:https://stackoverflow.com/questions/39071093/django-db-utils-oper ...
- JS+PHP瀑布流效果(二)
<!-- 加载商品 --><script> //用户拖动滚动条,达到底部时ajax加载一次数据 var loading = $("#loading&quo ...
- 转:使用awk命令获取文本的某一行,某一列
1.打印文件的第一列(域) : awk '{print $1}' filename2.打印文件的前两列(域) : awk '{print ...
- JavaScript你所不知道的困惑(3)
版权声明:本文出自水寒的原创文章.未经博主同意不得转载. https://blog.csdn.net/lxq_xsyu/article/details/25600011 困惑一: window.col ...
- win10笔记本触摸板手势大全
- 20170413 F110学习
F110 学习: Tcode: F110 自动付款业务, FBZP 维护收付程序设置 FBL1N 供应商行项目 XK03 显示供应商(银行信息维护) F110 ...
- linux wdcp安装
wdCP是WDlinux Control Panel的简称,是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统,旨在易于使用Linux系统做为我们的网站服务器系统,以及平时 ...