本文共 3766 字,大约阅读时间需要 12 分钟。
使用预定义模型 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();
- }
#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
- #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 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
- #include <QtGui>
-
- #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 *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(listView);
- mainLayout->addWidget(buttonBox);
-
- 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()
- {
-
- model->removeRows(listView->currentIndex().row(), 1);
- }
转载地址:http://hqlmi.baihongyu.com/