0

我正在尝试显示同一数据库中两个表中的数据。数据库中的表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");

然后我有两个QSqlQueryModels 应该代表两个表中的数据:PlanOnePlanTwo

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";

MySqlModelTwoMySqlModelOne具有适当更改的副本,例如将每个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使用ListViews 显示来自 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

前两行向我保证,数据库在两个QSqlQueryModels 中都已正确打开,但接下来的 8 行向我表明,数据可能尚未从两个表中检索到,或者已检索到,但格式undefined显然不能分配给QString因此无法通过ListViews 显示。问题是否在于我尝试通过两个不同的模型同时从同一个数据库中检索数据?如果是这样,我怎样才能做到这一点而不会导致此类错误?或者您可能有其他想法如何使用QSqlQueryModel.

4

1 回答 1

0

问题出在程序的 QML 部分。ModelOne在尝试访问表中的字段时将and绑定到每个sModelTwo的属性后,无需这样做:modelListView

Text {
    text: modelOne.Time
}

相反,它应该是:

Text {
    text: Time
}
于 2021-02-17T16:02:18.307 回答