Qt 使用QSqlQueryModel从mysql表输出数据

Qt 使用QSqlQueryModel从mysql表输出数据,qt,qml,qtableview,qsqlquerymodel,Qt,Qml,Qtableview,Qsqlquerymodel,我使用下面的例子。它使用QSqlQueryModel,并具有列名称的选择列表。 但是在我的例子中,我不知道输出中的表列名,因为我使用的是*query“Select*from users”。如何在Qml中打印表格视图中的数据 我的代码如下: QtTest2.cpp QtTest2::QtTest2(QObject *parent) : QSqlQueryModel(parent) { } void QtTest2::setQuery(const QString &query, c

我使用下面的例子。它使用QSqlQueryModel,并具有列名称的选择列表。 但是在我的例子中,我不知道输出中的表列名,因为我使用的是
*
query
“Select*from users”
。如何在Qml中打印表格视图中的数据

我的代码如下:

QtTest2.cpp

QtTest2::QtTest2(QObject *parent) :
    QSqlQueryModel(parent)
{
}

void QtTest2::setQuery(const QString &query, const QSqlDatabase &db)
{
    QSqlQueryModel::setQuery(query, db);
    generateRoleNames();
}

void QtTest2::setQuery(const QSqlQuery & query)
{
    QSqlQueryModel::setQuery(query);
    generateRoleNames();
}

void QtTest2::generateRoleNames()
{
    m_roleNames.clear();
    for( int i = 0; i < record().count(); i ++) {
        m_roleNames.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
    }
}

QVariant QtTest2::data(const QModelIndex &index, int role) const
{
    QVariant value;

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

void QtTest2::callSql()
{
    this->setQuery("SELECT * FROM users");
}
编辑
我已经调用了
engine.rootContext()->setContextProperty(“QtTest2”和&QtTest2)

由于您使用的是基于QAbstractTableModel的QSqlQueryModel,因此您可以使用角色“modelData”或“display”访问每个项目

文本{
文本:model.modelData//或model.display
锚定。填充:父级
2.5.2.利润率:10
颜色:“黑色”
font.pixelSize:15
垂直对齐:Text.AlignVCenter

}
显示导入的qml@eyllanesc我已经编辑了qml代码在代码块中添加了完整的qml代码。使用此代码,我得到
ReferenceError:display not defined
ReferenceError:modelData not defined
。你能指出必须做什么吗wrong@Chilarai在“//此处…”之前有一个不必要的“}”,请删除it@Chilarai我刚刚修正了你问题代码中的错误,你对代码也一样。谢谢你的解决方案。model.modelData/model.display解决了我的问题。
import QtQuick 2.12
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3

Page {

id : somepageid

Component.onCompleted: {
    QtTest2.callsql();
}

TableView {
    id: tableView
    model: QtTest2

    // This is for header

    Row {
        id: columnsHeader
        y: tableView.contentY
        z: 2
        Repeater {
            model: tableView.columns > 0 ? tableView.columns : 1
            Label {
                width: tableView.columnWidthProvider(modelData)
                height: 35
                text: QtTest2.headerData(modelData, Qt.Horizontal)
                color: '#aaaaaa'
                font.pixelSize: 15
                padding: 10
                verticalAlignment: Text.AlignVCenter

                background: Rectangle { color: "#333333" }
            }
        }
    }


    // Here I cannot print the values dynamically. 
    // Currently this is printing the email (e.g.), in all columns in a row
    // But yes, each row prints different email
    // I want the rest of the column data dynamically printed
      
    delegate:Rectangle {
                        Text {
                            text: email
                            anchors.fill: parent
                            anchors.margins: 10
                            color: 'black'
                            font.pixelSize: 15
                            verticalAlignment: Text.AlignVCenter
                        }
                    }
    }

   
Text {
    text: model.modelData // or model.display
    anchors.fill: parent
    anchors.margins: 10
    color: 'black'
    font.pixelSize: 15
    verticalAlignment: Text.AlignVCenter
}