(二)使用预定义模型 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 ...
随机推荐
- 安卓解析 json 4种格式 全解析
总共4种json , 还提供了 httclient 和 httpurlconnection 2种方式获取json的方法(请把baseurl 改为可以地址比如http://www.sohu.c ...
- Linux内核中锁机制之内存屏障、读写自旋锁及顺序锁
在上一篇博文中笔者讨论了关于原子操作和自旋锁的相关内容,本篇博文将继续锁机制的讨论,包括内存屏障.读写自旋锁以及顺序锁的相关内容.下面首先讨论内存屏障的相关内容. 三.内存屏障 不知读者是是否记得在笔 ...
- 关于 Chrome Console 查看DOM详情细节的奇思淫巧
我们期待打印出的dom效果如下: 但某些时候,打印出来,或者通过$0.$1.document.getElementById('####') 等方式打印出来的效果如下: 根据第一幅图我们不难看出,当打印 ...
- php model与json_encode/json_decode
常用于model的操作,看看就知道了 <?php class UserModel { var $user_id = 0; var $user_name = ''; var $user_email ...
- enumerate的简单使用
l = [11,22,33,55,"ss","zz"] for i,v in enumerate(l): print(i,v) #打印结果: # 0 11 # ...
- Hadoop本地库介绍及相关问题解决方法汇总
1.hadoop本地库的作用是什么?2.哪两个压缩编码器必须使用hadoop本地库才能运行?3.hadoop的使用方法?4.hadoop本地库与系统版本不一致会引起什么错误?5.$ export HA ...
- 基于Unity3d 引擎的Android游戏优化
原文地址:http://blog.csdn.net/jixuguo/article/details/9018669 近期项目进入收尾阶段,之前对项目做了非常多优化,mesh合并 .降低DrawCall ...
- 微信开发,对象转换为xml时候引用XStream这个类报错处理方案
报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /** * 扩展XStream 支持CDATA */ privat ...
- CSS中overflow:scroll怎么设置只上下滚动而不左右滚动
CSS中"overflow:scroll"默认是左右,上下都滚动.怎么设置只上下滚动而不左右滚动,下面有个不错的解决方法 CSS中"overflow:scroll&quo ...
- 在IntentService中使用Toast与在Service中使用Toast的异同
1. 表象 Service中能够正常显示Toast,IntentService中不能正常显示Toast.在2.3系统上,不显示toast,在4.3系统上,toast显示.可是不会消失. 2. 问题分析 ...