我有一个自定义类ConnectionStandardItem,它是QStandardItem的子类。此项应包含服务器,用户名,密码等信息,以便稍后在QSqlDatabase中使用。
ConnectionStandardItem::ConnectionStandardItem(QString const& connectionId) : QStandardItem()
{
QMap<QString, QVariant> m_connectionDefinition;
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "goat", "connections");
if (!connectionId.isEmpty())
{
settings.beginGroup(connectionId.trimmed());
m_connectionDefinition[connectionId] = QVariant(connectionId.trimmed());
foreach(QString key, settings.childKeys())
{
m_connectionDefinition[key] = settings.value(key);
}
setText(m_connectionDefinition["name"].toString());
setData(m_connectionDefinition, Qt::UserRole);
settings.endGroup();
}
}
在对话框中使用扩展的QStandardItem:
NewConnectionDialog::NewConnectionDialog(QWidget *parent) : QDialog(parent), ui(new Ui::NewConnectionDialog) {
ui->setupUi(this);
m_model = new QStandardItemModel(this);
m_driversModel = new QStandardItemModel(this);
m_connectionListModel = new QStandardItemModel(this);
ui->listDropdownDBDriver->setModel(m_driversModel);
ui->listViewConnections->setModel(m_connectionListModel);
/* set combobox */
QStandardItem* item_psql = new QStandardItem();
item_psql->setText("PostgreSQL");
item_psql->setData("QPSQL", Qt::UserRole);
m_driversModel->appendRow(item_psql);
QStandardItem* item_mysql = new QStandardItem();
item_mysql->setText("MySQL/MariaDB");
item_mysql->setData("QMYSQL", Qt::UserRole);
m_driversModel->appendRow(item_mysql);
updateConnectionListModel();
connect(ui->listViewConnections->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(handleSelectionChanged(QItemSelection, QItemSelection)));
connect(m_connectionListModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateListViewItem(QStandardItem*)));
}
void NewConnectionDialog::handleSelectionChanged(QItemSelection current, QItemSelection previous) {
const QModelIndex index = ui->listViewConnections->currentIndex();
ConnectionStandardItem* connection = m_connectionListModel->itemFromIndex(index)->data().value<ConnectionStandardItem*>();
QMap<QString, QVariant> connectionDefinition = connection->data().value<QMap<QString, QVariant>>();
//ui->listDropdownDBDriver->setCurrentIndex(index);
ui->txtUser->setText(connectionDefinition["username"].toString());
ui->txtPass->setText(connectionDefinition["pass"].toString());
ui->txtServer->setText(connectionDefinition["server"].toString());
ui->txtPort->setText(connectionDefinition["port"].toString());
ui->txtDatabase->setText(connectionDefinition["database"].toString());
}
void NewConnectionDialog::updateConnectionListModel() {
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "goat", "connections");
foreach(QString key, settings.childGroups())
{
ConnectionStandardItem* item = new ConnectionStandardItem(key);
m_connectionListModel->appendRow(item);
}
}
不幸的是,在对话框中,我无法访问当前所选项目的数据。我正在检查的方法是handleSelectionChanged。
我如何正确地从ConnenctionStandardItem中提取Map对象,以便更新GUI,反之亦然,将gui中的更改发送回模型(并通过模型发送到.ini设置文件)? 有没有更好的方法来解决这个问题(不同的班级)?
答案 0 :(得分:1)
ConnectionStandardItem不是保存在QStandardItem中的数据,但是必须从QStandardItem强制转换ConnectionStandardItem。
另一方面,您以Qt::UserRole
角色保存数据:
setData(m_connectionDefinition, Qt::UserRole);
但是,使用data()
时,您将根据文档使用Qt::UserRole + 1
角色:
QVariant QStandardItem::data(int role = Qt::UserRole + 1) const
下一个解决方案是
void NewConnectionDialog::handleSelectionChanged(QItemSelection current, QItemSelection previous) {
const QModelIndex index = ui->listViewConnections->currentIndex();
QStandardItem *item = m_connectionListModel->itemFromIndex(index);
ConnectionStandardItem* connection = static_cast<ConnectionStandardItem*>(item); // <---
QMap<QString, QVariant> connectionDefinition = connection->data(Qt::UserRole).value<QMap<QString, QVariant>>(); // <---
//ui->listDropdownDBDriver->setCurrentIndex(index);
ui->txtUser->setText(connectionDefinition["username"].toString());
ui->txtPass->setText(connectionDefinition["pass"].toString());
ui->txtServer->setText(connectionDefinition["server"].toString());
ui->txtPort->setText(connectionDefinition["port"].toString());
ui->txtDatabase->setText(connectionDefinition["database"].toString());
}
尽管我也看到强制转换也是不必要的,因为我们不会使用在ConnectionStandardItem
中实现的方法,所以我们只能使用QStandardItem
:
void NewConnectionDialog::handleSelectionChanged(QItemSelection current, QItemSelection previous) {
const QModelIndex index = ui->listViewConnections->currentIndex();
QStandardItem *connection = m_connectionListModel->itemFromIndex(index);
QMap<QString, QVariant> connectionDefinition = connection->data(Qt::UserRole).value<QMap<QString, QVariant>>(); // <---
//ui->listDropdownDBDriver->setCurrentIndex(index);
ui->txtUser->setText(connectionDefinition["username"].toString());
ui->txtPass->setText(connectionDefinition["pass"].toString());
ui->txtServer->setText(connectionDefinition["server"].toString());
ui->txtPort->setText(connectionDefinition["port"].toString());
ui->txtDatabase->setText(connectionDefinition["database"].toString());
}