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的示例代码 ...
随机推荐
- mysql_用户_操作
一. 创建用户 登录MySQL mysql -u root -p 添加新用户 create user 'username'@'host' identified by 'password'; usern ...
- php自定义函数: 时间转换成智能形式
function time_trans($paratime,$suffix=false){ $now_time = time(); $dur = $now_time - $paratime; $suf ...
- 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?
QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...
- 关于在python manage.py createsuperuser时报django.db.utils.OperationalError: no such table: auth_user的解决办法
在stackflow上看到解决的办法是需要进行数据路的migrate:https://stackoverflow.com/questions/39071093/django-db-utils-oper ...
- 前端基础-html(3)
一.列表标签 1.ul(无序列表)标签 ul(unordered list)无序列表,ul下的子元素只能是li(list item),如下示例: <ul> <li>第一项< ...
- Hadoop权威指南读书笔记
本书中提到的Hadoop项目简述 Common:一组分布式文件系统和通用I/O的组件与接口(序列化.javaRPC和持久化数据结构). Avro:一种支持高效.跨语言的RPC以及永久存储数据的序列化系 ...
- Nginx配置参数说明
一.主配置段1.正常运行必备的配置#运行用户和组,组身份可以省略user nginx nginx; #指定nginx守护进程的pid文件pid path/to/nginx.pid; #指定所有work ...
- 构建Ruby开发环境(Windows+Eclipse+Aptana Plugin)
1.安装Ruby ①.从http://rubyinstaller.org/downloads/下载安装包:rubyinstaller-2.2.5-x64.exe,直接安装.(so easy) 2.安装 ...
- Mac 远程连接 Windows 系统无法全屏
远程连接之后,Mac 工具栏中 配置 RDC 下 “首选项”. “显示” ----远程桌面大小:全屏 ----打开远程桌面窗口:第二显示器(我用的是双显示器,根据实际情况设定显示器) 配置完成后,点击 ...
- DD DT DL标签
我们平时常用的是< ul>< li>标签,不过dd.dt标签也蛮不错,特别是发布程序的时候功能模块列表什么的可以使用它来排版. < dl>< /dl>& ...