kobe
1
#ifndef TEAMLEADERDIALOG_H
#define TEAMLEADERDIALOG_H
#include <QtGui/QDialog>
#include <QListView>
#include <QStringListModel>
class TeamLeaderDialog : public QDialog
{
Q_OBJECT
public:
TeamLeaderDialog(const QStringList &leaders, QWidget *parent = 0);
~TeamLeaderDialog();
private slots:
void insertName();
void deleteName();
void editName();
private:
QListView *listView;
QStringListModel *model;
};
#endif // TEAMLEADERDIALOG_H
#include "teamleaderdialog.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QInputDialog>
TeamLeaderDialog::TeamLeaderDialog(const QStringList &leaders, QWidget *parent)
: QDialog(parent)
{
model = new QStringListModel;
model->setStringList(leaders);
listView = new QListView;
listView->setModel(model);
QPushButton *insertButton = new QPushButton("Insert");
QPushButton *deleteButton = new QPushButton("Delete");
QPushButton *editButton = new QPushButton("Edit");
QHBoxLayout *hlayout = new QHBoxLayout;
hlayout->addWidget(insertButton);
hlayout->addWidget(deleteButton);
hlayout->addWidget(editButton);
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->addWidget(listView);
vlayout->addLayout(hlayout);
setLayout(vlayout);
connect(insertButton, SIGNAL(clicked()), this, SLOT(insertName()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteName()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editName()));
}
TeamLeaderDialog::~TeamLeaderDialog()
{
}
void TeamLeaderDialog::insertName()
{
bool ok;
QString name = QInputDialog::getText(this, tr("New Name"), tr(""), QLineEdit::Normal, tr(""), &ok);
if (ok && !name.isEmpty())
{
int row = listView->currentIndex().row();
model->insertRows(row, 1);
QModelIndex index = model->index(row);
model->setData(index, name);
listView->setCurrentIndex(index);
}
}
void TeamLeaderDialog::deleteName()
{
model->removeRows(listView->currentIndex().row(), 1);
}
void TeamLeaderDialog::editName() //直接按F2就可以编辑,不用自己实再实现编辑功能
{
int row = listView->currentIndex().row();
QModelIndex index = model->index(row);
QVariant variant = model->data(index, Qt::DisplayRole); //获取当前选择的项的文本
QString name = variant.toString();
bool ok;
name = QInputDialog::getText(this, tr("Edit Name"), tr(""), QLineEdit::Normal, name, &ok);
if (ok && !name.isEmpty())
{
row = listView->currentIndex().row();
index = model->index(row);
model->setData(index, name);
listView->setCurrentIndex(index);
}
}
原文链接