(二)使用预定义模型 QStringListModel例子
2012-08-03 14:07
363 查看
使用预定义模型 QStringListModel例子
源代码如下
Main.cpp
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
Cpp代码
#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 = 0); QStringList leaders() const; private slots: void insert(); void del(); private: QListView *listView; QDialogButtonBox *buttonBox; QStringListModel *model; }; #endif
teamleadersdialog.cpp
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, 1);
//获取当前行在模型中的"模型索引"
QModelIndex index = model->index(row);
//设置刚刚插入的空白行为列表视图的当前索引
listView->setCurrentIndex(index);
//设置列表视图在当前行进入编辑状态
listView->edit(index);
}
void TeamLeadersDialog::del()
{
//从目前行开始,共删除1行model数据,并自动更新列表视图
model->removeRows(listView->currentIndex().row(), 1);
}
源代码如下
Main.cpp
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();
}
#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
Cpp代码
#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 = 0); QStringList leaders() const; private slots: void insert(); void del(); private: QListView *listView; QDialogButtonBox *buttonBox; QStringListModel *model; }; #endif
#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 = 0); QStringList leaders() const; private slots: void insert(); void del(); private: QListView *listView; QDialogButtonBox *buttonBox; QStringListModel *model; }; #endif
teamleadersdialog.cpp
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, 1);
//获取当前行在模型中的"模型索引"
QModelIndex index = model->index(row);
//设置刚刚插入的空白行为列表视图的当前索引
listView->setCurrentIndex(index);
//设置列表视图在当前行进入编辑状态
listView->edit(index);
}
void TeamLeadersDialog::del()
{
//从目前行开始,共删除1行model数据,并自动更新列表视图
model->removeRows(listView->currentIndex().row(), 1);
}
相关文章推荐
- (二)使用预定义模型 QStringListModel例子
- 使用预定义模型 QStringListModel+QStringlist例子
- 使用预定义模型 QStringListModel例子
- (三)使用预定义模型QDirModel的例子
- 使用预定义模型QDirModel+Treeview的例子
- model-view模型之QListView和QStringListModel .
- model-view模型之QListView和QStringListModel
- QStringListModel的简单使用
- model-view模型之QListView和QStringListModel
- Qt编程26:QStringListModel与QListView结合使用
- QT数据库使用案列【联系人】-- 使用sqlite和QStringListModel
- QListView使用学习--QStringListModel的使用
- 模型视图简介、QListWidget、QTreeWidget、QTableWidget、QStringListModel、QFileSystemModel
- ThinkPHP--数据库连接及model模型定义使用
- QStringListModel的简单使用
- Xcode8.x使用CoreData模型出现类被非法重定义的解决办法
- 使用SQL Server Analysis Services Tabular Model建立分析模型
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- 使用 Jackson 树模型(tree model) API 处理 JSON
- Qt QStringList的简单使用方法