我正在尝试显示同一数据库中两个表中的数据。数据库中的表plan_db.sqlite
是使用这些查询创建的:
CREATE TABLE PlanOne (ID INTEGER NOT NULL PRIMARY KEY, Time VARCHAR(5), Name VARCHAR(50));
CREATE TABLE PlanTwo (ID INTEGER NOT NULL PRIMARY KEY, Time VARCHAR(5), Name VARCHAR(50));
插入一些数据后,它的结构如下所示:
PlanOne:
1|10:00|Event 1
2|14:00|Event 2
PlanTwo:
1|12:00|Event 3
2|16:00|Event 4
连接在类中建立DatabaseHandler
:
QSqlDatabase plan_db = QSqlDatabase::addDatabase("QSQLITE", "plan_connection");
然后我有两个QSqlQueryModel
s 应该代表两个表中的数据:PlanOne
和PlanTwo
:
MySqlModelOne
:
class MySqlModelOne : public QSqlQueryModel
{
Q_OBJECT
public:
explicit MySqlModelOne(QObject* parent = Q_NULLPTR);
Q_INVOKABLE void refresh();
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
private:
QSqlDatabase plan_db = QSqlDatabase::database("plan_connection");
QSqlQuery plan_qry;
const static char* COLUMN_NAMES[];
const static char* SQL_SELECT;
};
MySqlModelOne::MySqlModelOne(QObject* parent) : QSqlQueryModel(parent)
{
if (plan_db.open()) qDebug("Plan database opened");
else qDebug("Plan database not opened");
plan_qry = QSqlQuery(plan_db);
refresh();
}
void MySqlModelOne::refresh()
{
plan_qry.exec(SQL_SELECT);
this->setQuery(plan_qry);
}
QHash<int, QByteArray> MySqlModelOne::roleNames() const
{
int idx = 0;
QHash<int, QByteArray> roleNames;
while (COLUMN_NAMES[idx]) {
roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
idx++;
}
return roleNames;
}
QVariant MySqlModelOne::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}
const char* MySqlModelOne::COLUMN_NAMES[] = { "ID", "Time", "Name", NULL};
const char* MySqlModelOne::SQL_SELECT = "SELECT * FROM PlanOne";
MySqlModelTwo
是MySqlModelOne
具有适当更改的副本,例如将每个One
替换为Two
:
const char* MySqlModelTwo::SQL_SELECT = "SELECT * FROM PlanTwo";
这些类在以下位置正确暴露给 QML main.cpp
:
qmlRegisterType<MySqlModelOne>("com.sweak.mysqlmodelone", 1, 0, "MySqlModelOne");
qmlRegisterType<MySqlModelTwo>("com.sweak.mysqlmodeltwo", 1, 0, "MySqlModelTwo");
qmlRegisterType<DatabaseHandler>("com.sweak.databasehandler", 1, 0, "DatabaseHandler");
最后是应该QSqlQueryModel
使用ListView
s 显示来自 s 的数据的 QML 代码:
Window {
width: 640
height: 480
visible: true
DatabaseHandler {
id: dataBaseHandler
}
MySqlModelOne {
id: modelOne
}
MySqlModelTwo {
id: modelTwo
}
ListView {
id: listViewOne
model: modelOne
width: 100
height: width
anchors.left: parent.left
delegate: RowLayout {
Text {
text: modelOne.Time
}
Text {
text: modelOne.Name
}
}
}
ListView {
id: listViewTwo
model: modelTwo
width: 100
height: width
anchors.left: listViewOne.right
delegate: RowLayout {
Text {
text: modelTwo.Time
}
Text {
text: modelTwo.Name
}
}
}
执行程序后,程序窗口是空白的(显然),我收到以下错误消息:
Plan database opened
Plan database opened
qrc:/main.qml:42:17: Unable to assign [undefined] to QString
qrc:/main.qml:45:17: Unable to assign [undefined] to QString
qrc:/main.qml:42:17: Unable to assign [undefined] to QString
qrc:/main.qml:45:17: Unable to assign [undefined] to QString
qrc:/main.qml:66:17: Unable to assign [undefined] to QString
qrc:/main.qml:69:17: Unable to assign [undefined] to QString
qrc:/main.qml:66:17: Unable to assign [undefined] to QString
qrc:/main.qml:69:17: Unable to assign [undefined] to QString
前两行向我保证,数据库在两个QSqlQueryModel
s 中都已正确打开,但接下来的 8 行向我表明,数据可能尚未从两个表中检索到,或者已检索到,但格式undefined
显然不能分配给QString
因此无法通过ListView
s 显示。问题是否在于我尝试通过两个不同的模型同时从同一个数据库中检索数据?如果是这样,我怎样才能做到这一点而不会导致此类错误?或者您可能有其他想法如何使用QSqlQueryModel
.