Qt学习:项视图类之QStringListModel和QListView

 QListView是不显示表头和表框的,如果要显示,可以使用QTreeView来

    view的显示属性分为列表list显示和icon图标显示,使用
    QListView::setViewMode()来设置,默认为list型的

    QListView::setModel ()用来设置view所关联的model

    获取view中当前的位置,QListView::currentIndex(),返回的是QModelIndex类型的
    设置当前行使用QListView::setCurrentIndex(QModelIndex),
    设置某行可以编辑,使用QListView::edit(QModelIndex)

    当某个QModelIndex被移动时候,信号indexesMoved(QModelIndexList)被发射
    当双击某项时候,信号doubleClicked(QModelIndex)被发射
    当大几某项时候,信号clicked(QModelIndex)被发射

    QStringListModel就是封装了QStringList的model。QStringList是一种很常用的数据类型,它实际上是一个字符串列表。我们可以想象,对于一个list来说,如果提供一个字符串列表形式的数据,就应该能够把这个数据展示出来。因为二者是一致的:QStringList是线性的,而list也是线性的。所以,QStringListModel很多时候都会作为QListView的model。
    model使用insertRows,insertRow来添加多行或一行,使用setData()来设置该行的数据
    rowCount()用来获改model的行数
    removeRows()和removeRow()用来删除model中的一行或多行,详见API手册。 

    下边给出例子,照例,我们先给出效果:

 

    整个例子用了一个QListView,一个QStirngListModel和一个QDialogButtonBox,构造函数接收一个存储着QString的QStringList用来初始化QStringListModel,然后用QListView的setModel()函数把数据和model联系起来。这里我们可以很清楚的看到MVC架构的各个部分是如何关联起来的:QStringListModel是一组数据集,存储的是一系列字符串数据;QListView是视图类,是一个窗口部件,用来观察这组数据;一些和它关联的操作,我们统称为控制(insert,delete等) 。下面给出代码:

[cpp]  view plain  copy
  1. //teamleadersdialog.h  
  2. #ifndef TEAMLEADERSDIALOG_H  
  3. #define TEAMLEADERSDIALOG_H  
  4.   
  5. #include <QDialog>  
  6. #include <QStringListModel>  
  7. #include <QListView>  
  8. #include <QDialogButtonBox>  
  9.   
  10. class TeamLeadersDialog : public QDialog  
  11. {  
  12.     Q_OBJECT  
  13.       
  14. public:  
  15.     TeamLeadersDialog(const QStringList &leaders, QWidget *parent = 0);  
  16.     ~TeamLeadersDialog();  
  17.   
  18. private slots:  
  19.     void insert();  
  20.     void del();  
  21.   
  22. private:  
  23.     QStringListModel *model;  
  24.     QListView *listView;  
  25.     QDialogButtonBox *buttonBox;  
  26. };  
  27.   
  28. #endif // TEAMLEADERSDIALOG_H  
  29.   
  30. //teamleadersdialog.cpp  
  31. #include <QPushButton>  
  32. #include <QVBoxLayout>  
  33.   
  34. #include "teamleadersdialog.h"  
  35.   
  36. TeamLeadersDialog::TeamLeadersDialog(const QStringList &leaders, QWidget *parent)  
  37.     : QDialog(parent)  
  38. {  
  39.     model = new QStringListModel(this);  
  40.     model->setStringList(leaders);  
  41.   
  42.     listView = new QListView;  
  43.     listView->setModel(model);  
  44.     listView->setEditTriggers(QAbstractItemView::AnyKeyPressed  
  45.                               | QAbstractItemView::DoubleClicked);  
  46.   
  47.     buttonBox = new QDialogButtonBox;  
  48.     QPushButton *insertButton = buttonBox->addButton(  
  49.                 tr("&Insert"),QDialogButtonBox::ActionRole);  
  50.     QPushButton *deleteButton = buttonBox->addButton(  
  51.                 tr("&Delete"), QDialogButtonBox::ActionRole);  
  52.     buttonBox->addButton(QDialogButtonBox::Ok);  
  53.     buttonBox->addButton(QDialogButtonBox::Cancel);  
  54.   
  55.     connect(insertButton, SIGNAL(clicked()), this, SLOT(insert()));  
  56.     connect(deleteButton, SIGNAL(clicked()), this, SLOT(del()));  
  57.     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));  
  58.     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));  
  59.   
  60.     QVBoxLayout *layout = new QVBoxLayout;  
  61.     layout->addWidget(listView);  
  62.     layout->addWidget(buttonBox);  
  63.     setLayout(layout);  
  64.   
  65.     setWindowTitle(tr("QStringListModel"));  
  66. }  
  67.   
  68. void TeamLeadersDialog::insert()  
  69. {  
  70.     int row = listView->currentIndex().row();  
  71.     model->insertRows(row, 1);  
  72.   
  73.     QModelIndex index = model->index(row);  
  74.     listView->setCurrentIndex(index);  
  75.     listView->edit(index);  
  76. }  
  77.   
  78. void TeamLeadersDialog::del()  
  79. {  
  80.     model->removeRows(listView->currentIndex().row(), 1);  
  81. }  
  82.   
  83. TeamLeadersDialog::~TeamLeadersDialog()  
  84. {     
  85. }  
  86.   
  87. //main.cpp  
  88. #include "teamleadersdialog.h"  
  89. #include <QApplication>  
  90.   
  91. int main(int argc, char *argv[])  
  92. {  
  93.     QApplication a(argc, argv);  
  94.     QStringList list;  
  95.     list << QObject::tr("xianziyu")  
  96.          << QObject::tr("yangwangming")  
  97.          << QObject::tr("yinshixin")  
  98.          << QObject::tr("baichengshuang")  
  99.          << QObject::tr("zhangzurui");  
  100.     TeamLeadersDialog w(list);  
  101.     w.show();  
  102.       
  103.     return a.exec();  
  104. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值