您好我使用qtreeview的问题。所以我想从所选项中获取值并将所选值从选定行保存到数组。当用户单击编辑按钮时,我不知道应该写什么代码。请帮助我在行void Teachers::editTeacher ()?
中写下哪些代码。这是我的代码:
#include <iostream>
#include <exception>
#include "mainwindow/teacherspage/teachers.h"
#include "mainwindow/teacherspage/crud_teacher.h"
using namespace std;
Teachers::Teachers(QWidget* parent) : QWidget(parent)
{
proxyModel_ = new QSortFilterProxyModel;
proxyView_ = new QTreeView;
proxyView_->setRootIsDecorated(false);
proxyView_->setAlternatingRowColors(true);
proxyView_->setModel(proxyModel_);
proxyView_->setSortingEnabled(true);
proxyView_->sortByColumn(1, Qt::AscendingOrder);
filterPatternLineEdit_ = new QLineEdit;
filterPatternLabel_ = new QLabel("Cari data:");
filterColumnComboBox_ = new QComboBox;
filterColumnComboBox_->addItem("No Induk");
filterColumnComboBox_->addItem("Nama");
filterColumnComboBox_->setFixedWidth(200);
addButton_ = new QPushButton("Tambah");
addButton_->setFixedWidth(100);
editButton_ = new QPushButton("Edit");
editButton_->setFixedWidth(100);
deleteButton_ = new QPushButton("Hapus");
deleteButton_->setFixedWidth(80);
reloadButton_ = new QPushButton();
reloadButton_->setIcon(QIcon(":/images/reload.png"));
reloadButton_->setFixedWidth(20);
connect(filterPatternLineEdit_, SIGNAL(textChanged(QString)),
this, SLOT(filterRegExpChanged()));
connect(filterColumnComboBox_, SIGNAL(currentIndexChanged(int)),
this, SLOT(filterColumnChanged()));
connect(addButton_, SIGNAL(clicked()), this, SLOT(addTeacher()));
connect(editButton_, SIGNAL(clicked()), this, SLOT(editTeacher()));
connect(deleteButton_, SIGNAL(clicked()), this, SLOT(deleteTeacher()));
connect(reloadButton_, SIGNAL(clicked()), this, SLOT(reloadTeacher()));
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *filterLayout = new QHBoxLayout;
QHBoxLayout *sourceLayout = new QHBoxLayout;
filterLayout->addWidget(filterPatternLabel_, 1);
filterLayout->addWidget(filterPatternLineEdit_, 2);
filterLayout->addWidget(filterColumnComboBox_, 3);
filterLayout->addWidget(addButton_, 4);
filterLayout->addWidget(editButton_, 5);
filterLayout->addWidget(deleteButton_, 6);
filterLayout->addWidget(reloadButton_, 7);
sourceLayout->addWidget(proxyView_);
mainLayout->addLayout(filterLayout);
mainLayout->addLayout(sourceLayout);
setLayout(mainLayout);
teachersModel = new TeachersModel(this);
try {
SetSourceModel(teachersModel->CreateTeacherModel());
} catch (exception& e) {
qDebug() << "Caught " << e.what();
QMessageBox::critical(this, "Error", e.what());
}
}
void Teachers::filterRegExpChanged()
{
QRegExp regExp(filterPatternLineEdit_->text(), Qt::CaseInsensitive, QRegExp::RegExp);
proxyModel_->setFilterRegExp(regExp);
}
void Teachers::filterColumnChanged()
{
proxyModel_->setFilterKeyColumn(filterColumnComboBox_->currentIndex());
}
void Teachers::SetSourceModel(QAbstractItemModel* model)
{
proxyModel_->setSourceModel(model);
proxyView_->setColumnHidden(9, true);
}
void Teachers::addTeacher()
{
CrudTeacher *crudTeacher = new CrudTeacher(this);
crudTeacher->show();
}
void Teachers::editTeacher()
{
}
void Teachers::deleteTeacher()
{
}
void Teachers::reloadTeacher()
{
delete teachersModel;
TeachersModel *teachersModel = new TeachersModel(this);
SetSourceModel(teachersModel->CreateTeacherModel());
}
Teachers::~Teachers() {}
这是结果计划:
答案 0 :(得分:2)
我猜你可以将QAbstractItemModel::data与视图当前索引一起使用。
由于QModelIndex
也包含列,因此您必须为每个列调用一次:
QList<QVariant> teacherData;
QModelIndex tIndex = proxyModel_->mapToSource(proxyView_->currentIndex());
for(int i = 0; i < teachersModel->rowCount(); i++)
{
QModelIndex columnIndex = teachersModel->sibling(tIndex.row(), i, tIndex);
teacherData.append(teachersModel->data(columnIndex);
}
我不知道你正在使用的模型,但是使用当前索引添加一个只返回完整行的函数会不会更容易?例如,如果您使用列表来存储数据,则可能如下所示:
//In the header file
private:
QList<Teacher> teachers; //or something similar
//in the .cpp file
QList<QVariant> getTeacherData(const QModelIndex &index)
{
QList<QVariant> data;
Teacher t = this->teachers[index.row()];
data.append(t.name); //as an example, use your own fields
//...
return data;
}