Qt學習:項視圖類之QStringListModel和QListView

來源:互聯網
上載者:User

    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等) 。下面給出代碼:

//teamleadersdialog.h#ifndef TEAMLEADERSDIALOG_H#define TEAMLEADERSDIALOG_H#include <QDialog>#include <QStringListModel>#include <QListView>#include <QDialogButtonBox>class TeamLeadersDialog : public QDialog{    Q_OBJECT    public:    TeamLeadersDialog(const QStringList &leaders, QWidget *parent = 0);    ~TeamLeadersDialog();private slots:    void insert();    void del();private:    QStringListModel *model;    QListView *listView;    QDialogButtonBox *buttonBox;};#endif // TEAMLEADERSDIALOG_H//teamleadersdialog.cpp#include <QPushButton>#include <QVBoxLayout>#include "teamleadersdialog.h"TeamLeadersDialog::TeamLeadersDialog(const QStringList &leaders, QWidget *parent)    : QDialog(parent){    model = new QStringListModel(this);    model->setStringList(leaders);    listView = new QListView;    listView->setModel(model);    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()));    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));    QVBoxLayout *layout = new QVBoxLayout;    layout->addWidget(listView);    layout->addWidget(buttonBox);    setLayout(layout);    setWindowTitle(tr("QStringListModel"));}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(){    model->removeRows(listView->currentIndex().row(), 1);}TeamLeadersDialog::~TeamLeadersDialog(){   }//main.cpp#include "teamleadersdialog.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    QStringList list;    list << QObject::tr("xianziyu")         << QObject::tr("yangwangming")         << QObject::tr("yinshixin")         << QObject::tr("baichengshuang")         << QObject::tr("zhangzurui");    TeamLeadersDialog w(list);    w.show();        return a.exec();}

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.