博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(二)使用预定义模型 QStringListModel例子
阅读量:4218 次
发布时间:2019-05-26

本文共 3766 字,大约阅读时间需要 12 分钟。

 

使用预定义模型 QStringListModel例子

 

源代码如下

Main.cpp

 

Cpp代码  
  1. #include <QApplication>   
  2.   
  3. #include "teamleadersdialog.h"   
  4.   
  5. int main(int argc, char *argv[])   
  6. {   
  7.     QApplication app(argc, argv);   
  8.   
  9.     //字符串数组   
  10.     QStringList leaders;   
  11.     leaders << "Stooge Viller" << "Littleface" << "B-B Eyes"  
  12.             << "Pruneface" << "Mrs. Pruneface" << "The Brow"  
  13.             << "Vitamin Flintheart" << "Flattop Sr." << "Shakey"  
  14.             << "Breathless Mahoney" << "Mumbles" << "Shoulders"  
  15.             << "Sketch Paree";   
  16.   
  17.     //对话框   
  18.     TeamLeadersDialog dialog(leaders);   
  19.     dialog.show();   
  20.   
  21.     return app.exec();   
  22. }  
#include 
#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代码  
  1. #ifndef TEAMLEADERSDIALOG_H   
  2. #define TEAMLEADERSDIALOG_H   
  3.   
  4. #include <QDialog>   
  5.   
  6. class QDialogButtonBox;   
  7. class QListView;   
  8. class QStringListModel;   
  9.   
  10. class TeamLeadersDialog : public QDialog   
  11. {   
  12.     Q_OBJECT   
  13.   
  14. public:   
  15.     //构造函数   
  16.     TeamLeadersDialog(const QStringList &leaders, QWidget *parent = 0);   
  17.   
  18.     QStringList leaders() const;   
  19.   
  20. private slots:   
  21.     void insert();   
  22.     void del();   
  23.   
  24. private:   
  25.     QListView *listView;   
  26.     QDialogButtonBox *buttonBox;   
  27.     QStringListModel *model;   
  28. };   
  29.   
  30. #endif  
#ifndef TEAMLEADERSDIALOG_H#define TEAMLEADERSDIALOG_H#include 
class QDialogButtonBox;class QListView;class QStringListModel;class TeamLeadersDialog : public QDialog{ Q_OBJECTpublic: //构造函数 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代码  
  1. #include <QtGui>   
  2.   
  3. #include "teamleadersdialog.h"   
  4.   
  5. TeamLeadersDialog::TeamLeadersDialog(const QStringList &leaders,   
  6.                                      QWidget *parent)   
  7.     : QDialog(parent)   
  8. {   
  9.     //创建并组装一个QStringListModel   
  10.     model = new QStringListModel(this);   
  11.     model->setStringList(leaders);   
  12.   
  13.     //创建一个QListView   
  14.     listView = new QListView;   
  15.     //设置模型   
  16.     listView->setModel(model);   
  17.     //设置QListView编辑触发器:通过开始输入或者双击进入编辑字符串的状态   
  18.     listView->setEditTriggers(QAbstractItemView::AnyKeyPressed   
  19.                               | QAbstractItemView::DoubleClicked);   
  20.     //   
  21.     buttonBox = new QDialogButtonBox();   
  22.     QPushButton *insertButton = buttonBox->addButton(tr("&Insert"),   
  23.             QDialogButtonBox::ActionRole);   
  24.     QPushButton *deleteButton = buttonBox->addButton(tr("&Delete"),   
  25.             QDialogButtonBox::ActionRole);   
  26.     buttonBox->addButton(QDialogButtonBox::Ok);   
  27.     buttonBox->addButton(QDialogButtonBox::Cancel);   
  28.     //信号槽绑定插入、删除按钮   
  29.     connect(insertButton, SIGNAL(clicked()), this, SLOT(insert()));   
  30.     connect(deleteButton, SIGNAL(clicked()), this, SLOT(del()));   
  31.     //按钮盒的ok和Cancel事件   
  32.     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));   
  33.     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));   
  34.   
  35.     //界面竖直布局listView和buttonBox   
  36.     QVBoxLayout *mainLayout = new QVBoxLayout;   
  37.     mainLayout->addWidget(listView);   
  38.     mainLayout->addWidget(buttonBox);   
  39.     //设置当前窗口的布局为mainLayout   
  40.     setLayout(mainLayout);   
  41.   
  42.     setWindowTitle(tr("Team Leaders"));   
  43. }   
  44. //获取当前模型中的内容   
  45. QStringList TeamLeadersDialog::leaders() const  
  46. {   
  47.     return model->stringList();   
  48. }   
  49.   
  50. void TeamLeadersDialog::insert()   
  51. {   
  52.     //从列表视图得到当前项的行数   
  53.     int row = listView->currentIndex().row();   
  54.     //在模型中插入一个新行,并且模型会自动更新列表视图   
  55.     model->insertRows(row, 1);   
  56.     //获取当前行在模型中的"模型索引"   
  57.     QModelIndex index = model->index(row);   
  58.     //设置刚刚插入的空白行为列表视图的当前索引   
  59.     listView->setCurrentIndex(index);   
  60.     //设置列表视图在当前行进入编辑状态   
  61.     listView->edit(index);   
  62. }   
  63.   
  64. void TeamLeadersDialog::del()   
  65. {   
  66.     //从目前行开始,共删除1行model数据,并自动更新列表视图   
  67.     model->removeRows(listView->currentIndex().row(), 1);   
  68. }  

转载地址:http://hqlmi.baihongyu.com/

你可能感兴趣的文章
Oracle smon_scn_time 表 说明
查看>>
VBox fdisk 不显示 添加的硬盘 解决方法
查看>>
Secure CRT 自动记录日志 配置 小记
查看>>
RMAN RAC 到 单实例 duplicate 自动分配通道 触发 ORA-19505 错误
查看>>
mysql 随机分页的优化
查看>>
DB2快速创建测试库
查看>>
利用db2look查看ddl
查看>>
SD卡驱动分析--基于高通平台
查看>>
[图文] Seata AT 模式分布式事务源码分析
查看>>
pm 源码分析
查看>>
Sending the User to Another App
查看>>
kmsg_dump
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>
test-definitions/blob/master/auto-test/bigdata/bigdata.sh
查看>>
/test-definitions/blob/master/auto-test/blktrace/blktrace.sh
查看>>
test-definitions/blob/master/auto-test/blogbench/blogbench.sh
查看>>