(二)使用预定义模型 QStringListModel例子
使用预定义模型 QStringListModel例子
源代码如下
Main.cpp
#include <QApplication> #include "teamleadersdialog.h" int main(int argc, char *argv[])
{
QApplication app(argc, argv); //字符串数组
QStringList leaders;
leaders << "Stooge Viller" << "Littleface" << "B-B Eyes"
<< "Pruneface" << "Mrs. Pruneface" << "The Brow"
<< "Vitamin Flintheart" << "Flattop Sr." << "Shakey"
<< "Breathless Mahoney" << "Mumbles" << "Shoulders"
<< "Sketch Paree"; //对话框
TeamLeadersDialog dialog(leaders);
dialog.show(); return app.exec();
}
teamleadersdialog.h
#ifndef TEAMLEADERSDIALOG_H
#define TEAMLEADERSDIALOG_H #include <QDialog> class QDialogButtonBox;
class QListView;
class QStringListModel; class TeamLeadersDialog : public QDialog
{
Q_OBJECT public:
//构造函数
TeamLeadersDialog(const QStringList &leaders, QWidget *parent = ); QStringList leaders() const; private slots:
void insert();
void del(); private:
QListView *listView;
QDialogButtonBox *buttonBox;
QStringListModel *model;
}; #endif
teamleadersdialog.cpp
#include <QtGui> #include "teamleadersdialog.h" TeamLeadersDialog::TeamLeadersDialog(const QStringList &leaders,
QWidget *parent)
: QDialog(parent)
{
//创建并组装一个QStringListModel
model = new QStringListModel(this);
model->setStringList(leaders); //创建一个QListView
listView = new QListView;
//设置模型
listView->setModel(model);
//设置QListView编辑触发器:通过开始输入或者双击进入编辑字符串的状态
listView->setEditTriggers(QAbstractItemView::AnyKeyPressed
| QAbstractItemView::DoubleClicked);
//
buttonBox = new QDialogButtonBox();
QPushButton *insertButton = buttonBox->addButton(tr("&Insert"),
QDialogButtonBox::ActionRole);
QPushButton *deleteButton = buttonBox->addButton(tr("&Delete"),
QDialogButtonBox::ActionRole);
buttonBox->addButton(QDialogButtonBox::Ok);
buttonBox->addButton(QDialogButtonBox::Cancel);
//信号槽绑定插入、删除按钮
connect(insertButton, SIGNAL(clicked()), this, SLOT(insert()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(del()));
//按钮盒的ok和Cancel事件
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); //界面竖直布局listView和buttonBox
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(listView);
mainLayout->addWidget(buttonBox);
//设置当前窗口的布局为mainLayout
setLayout(mainLayout); setWindowTitle(tr("Team Leaders"));
}
//获取当前模型中的内容
QStringList TeamLeadersDialog::leaders() const
{
return model->stringList();
} void TeamLeadersDialog::insert()
{
//从列表视图得到当前项的行数
int row = listView->currentIndex().row();
//在模型中插入一个新行,并且模型会自动更新列表视图
model->insertRows(row, );
//获取当前行在模型中的"模型索引"
QModelIndex index = model->index(row);
//设置刚刚插入的空白行为列表视图的当前索引
listView->setCurrentIndex(index);
//设置列表视图在当前行进入编辑状态
listView->edit(index);
} void TeamLeadersDialog::del()
{
//从目前行开始,共删除1行model数据,并自动更新列表视图
model->removeRows(listView->currentIndex().row(), );
}
转自:http://qimo601.iteye.com/blog/1534322
(二)使用预定义模型 QStringListModel例子的更多相关文章
- (三)使用预定义模型QDirModel的例子
使用预定义模型QDirModel的例子 Main.cpp #include <QApplication> #include "directoryviewer.h" in ...
- TVM部署预定义模型
TVM部署预定义模型 本文通过深度学习框架量化的模型加载到TVM中.预量化的模型导入是在TVM中提供的量化支持之一. 本文演示如何加载和运行由PyTorch,MXNet和TFLite量化的模型.加载后 ...
- Shell脚本_位置参数和预定义参数
一.位置参数变量 1.输出两个输入参数之和 l1.sh 1 2 3 4 5 6 7 8 9 #!/bin/bash num1=$1 num2=$2 sum=$((num1+num2)) # ...
- C#预定义类型、引用类型
一.预定义的值类型 一个字节(1Byte)=8位(8Bit) BitArarry类可以管理位Bit. 1.整型 所有的整形变量都能用十进制或十六进制表示:long a=0x12AB 对一个整形值如未指 ...
- javascript 函数初探 (二)--- 那些年的预定义函数
javascript的预定义函数: javascript引擎中有一组可以随时调用的内建函数. 这些内建函数包括: 1. parseInt() 2. parseFloat() 3. isNaN() 4. ...
- 【二十三】php之预定义超全局变量
php提供了九种预定义超全局变量: $_GET.$_POST.$_REQUEST.$_SERVER.$_ENV.$_FILE. $_COOKIE.$_SESSION. $GLOBALS 1.$_GET ...
- Java8学习笔记(二)--三个预定义函数接口
三个函数接口概述 JDK预定义了很多函数接口以避免用户重复定义.最典型的是Function: @FunctionalInterface public interface Function<T, ...
- .NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式
开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Func/Predicate)和超爱的Lambda表达式.为了方便码农们,. ...
- C++ 中常见预定义宏的使用
http://blog.csdn.net/hgl868/article/details/7058906 替代字符串: #define DOWNLOAD_IMAGE_LOG /var/log/png.l ...
随机推荐
- Error_GL_总账日记账过账失败
- DBA_实践指南系列11_Oracle Erp R12性能调优基础(案例)
http://www.itpub.net/thread-1412437-1-1.html
- 关于c语言内存分配,malloc,free,和段错误,内存泄露
1. C语言的函数malloc和free (1) 函数malloc和free在头文件<stdlib.h>中的原型及参数 void * malloc(size_t size ...
- Hive学习之函数DDL和Show、Describe语句
创建/删除函数 创建暂时函数 以下的语句创建由class_name实现的暂时函数,该函数被创建后仅仅能够在当前会话中使用.会话结束后函数失效. 实现函数的类能够是Hive类路径中的随意类.能够使用Ad ...
- LCA近期公共祖先
LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...
- iOS接收null的处理方法
常常server返回的数据,有null,还有nil,假设在模型层不处理的话,到时候数据展现时,一定会崩啊,近期决心要解决问题,所以查看了一些资料后,有答案了: - (id) setNoNull:(id ...
- C# - 关于过期Token的解决方案
使用static来定义变量和过期时间 //===================== 初始化静态数据 ========================== #region ); //Access_To ...
- laravel 如何引入自己的函数或类库
例如在app下建一个Common文件夹 在Common下建一个function.php 放入公共函数 例如: function test(){ echo 'this is a test'; } 在项目 ...
- node js 调试方法
1. node-debug tutorial 大家对nodejs调试应该都比较头疼,至少我这个不用IDE写js的人很头疼这个,其实node的生态圈非常好 有非常好的工具和非常潮的开发方式 这里总结了3 ...
- 利用 FormData 对象发送 Key/Value 对的异步请求
使用Key/Value对和FormData能够轻易地通过XMLHttpRequest指定要传递什么数据,它是一个非常强大的发送数据到服务器的方法. 基础 通常的方法是你创建一个 FormData 对象 ...