#include <QtGui> #include "listviewer.h" #include "ui_listviewer.h" ListViewer::ListViewer(const QStringList &itemList,QWidget *parent) : QDialog(parent), ui(new Ui::ListViewer) { ui->setupUi(this); model = new QStringListModel(this); model->setStringList(itemList); 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); } ListViewer::~ListViewer() { delete ui; } QStringList ListViewer::itemList() const { return model->stringList(); } void ListViewer::insert() { int row = listView->currentIndex().row(); model->insertRows(row, 1); QModelIndex index=model->index(row); listView->setCurrentIndex(index); listView->edit(index); } void ListViewer::del() { model->removeRows(listView->currentIndex().row(),1); }
|