我设置了要建模的标头,但是我无法在表视图中显示它,为什么?也许除了modelData之外,还有其他参数可以用来将标题的名称返回给tableview qml吗?
由于您还没有我的sql数据库,因此我无法提供更多重现示例。此代码仅将数据返回到tableview。也许tableview(qt quick 2)无法返回标头。
在TableModel类中,我不会覆盖QSqlTableModel中的标准方法。
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlTableModel>
#include <QFontDatabase>
#include <QDebug>
#include "TableModel.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("172.17.110.69");
db.setDatabaseName("usb_DB");
db.setUserName("usb_secure");
db.setPassword("QWer!234");
db.setPort(3306);
db.open();
qDebug()<<db.isOpen();
TableModel* model = new TableModel(&app,db);
model->setTable("usb_devices");
model->sort(0,Qt::SortOrder::AscendingOrder);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0,Qt::Orientation::Horizontal,"Column0");
model->setHeaderData(1,Qt::Orientation::Horizontal,"Column1");
model->setHeaderData(2,Qt::Orientation::Horizontal,"Column2");
model->setHeaderData(3,Qt::Orientation::Horizontal,"Column3");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QQmlContext *context = engine.rootContext();
context->setContextProperty("tableModel", model);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
main.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Window 2.11
ApplicationWindow {
id:appWindow
visible: true
TableView {
id: tableView
boundsBehavior:Flickable.StopAtBounds
reuseItems: true
clip: true
columnWidthProvider: function (column) { return 300; }
rowHeightProvider: function () { return 48; }
anchors.fill: parent
topMargin: columnsHeader.implicitHeight
model: tableModel
delegate:ItemDelegate{
id: delegateItem
TextField{
text: modelData
horizontalAlignment: Text.AlignHCenter
}
}
Row {
id: columnsHeader
y: tableView.contentY
z:2
Repeater {
model: tableView.columns > 0 ? tableView.columns : 1
Label {
id: labelRow
width: tableView.columnWidthProvider(index)
height: tableView.rowHeightProvider()
text: modelData
}
}
}
}
}
TableModel.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QtSql/QSqlRelationalTableModel>
#include <QSqlRecord>
class TableModel : public QSqlRelationalTableModel
{
Q_OBJECT
using QSqlRelationalTableModel::QSqlRelationalTableModel;
public slots:
Q_INVOKABLE bool set(int, int, QString);
private:
QSqlRecord rec;
};
#endif // TABLEMODEL_H
结果
答案 0 :(得分:0)
问题在于,如果Repeater模型是数字“ n”,则modelData是从“ 0”到“ n-1”的数字。解决方案是能够从QML访问headerData,为此必须使用Q_INVOKABLE:
class TableModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
using QSqlRelationalTableModel::QSqlRelationalTableModel;
Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const;
};
// ...
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QSqlRelationalTableModel::headerData(section, orientation, role);
}
// ...
*。qml
// ...
Repeater {
model: tableView.columns > 0 ? tableView.columns : 1
Label {
id: labelRow
width: tableView.columnWidthProvider(index)
height: tableView.rowHeightProvider()
text: tableModel.headerData(modelData, Qt.Horizontal) // <---
}
}
// ...