我有類,它將資料從檔案讀取到二維陣列。
所以我需要在 qml TableView 中顯示該陣列。
我有 QVector<QVector> 表;將其顯示為我的 TableModel 中的資料。
OperatingFiles 物件在 main.cpp 中創建,它包含對密碼進行編碼/解碼并將其保存到檔案的函式。這個物件的函式也從 qml 代碼中呼叫
所以我想要在某處制作“table = passwordsDecoded”,但我不知道該怎么做。
操作檔案.h:
class OperatingFiles : public QObject
{
Q_OBJECT
public:
OperatingFiles();
public slots:
QVector<QVector<QString>> getVector(); // returns passwordsDecoded
private:
QVector<QVector<QString>> passwordsDecoded;
};
#endif // OPERATINGFILES_H
表模型.h:
class TableModel : public QAbstractTableModel
{
Q_OBJECT
QVector<QVector<QString>> table;
public:
explicit TableModel(QObject *parent = nullptr);
int rowCount(const QModelIndex & = QModelIndex()) const override;
int columnCount(const QModelIndex & = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
};
表模型.cpp:
#include "tablemodel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel{parent}
{
QVector<QString> mini; // this only for test that it really appears in TableView (it appears)
mini.append("rstrst");
mini.append("rstrst");
mini.append("rstrst");
table.append(mini);
table.append(mini);
table.append(mini);
}
int TableModel::rowCount(const QModelIndex &) const
{
return table.size();
}
int TableModel::columnCount(const QModelIndex &) const
{
return table.at(0).size();
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case Qt::DisplayRole:
return table.at(index.row()).at(index.column());
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> TableModel::roleNames() const
{
return { {Qt::DisplayRole, "display"} };
}
qml:
TableView {
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
model: TableModel{}
delegate: Rectangle {
implicitWidth: 100
implicitHeight: 50
border.width: 0
Text {
text: display
anchors.centerIn: parent
}
}
}
主.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "controlbuttons.h"
#include "operatingfiles.h"
#include "tablemodel.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
qmlRegisterType<TableModel>("TableModel", 0,1,"TableModel");
QQmlApplicationEngine engine;
ControlButtons *appManager = new ControlButtons(&app);
engine.rootContext()->setContextProperty("appManager", appManager);
OperatingFiles *fileOperator = new OperatingFiles();
engine.rootContext() -> setContextProperty("fileOperator", fileOperator);
const QUrl url(QStringLiteral("qrc:/main.qml"));
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();
}
我試圖在 TableModel 中創建可以用我的物件中的資料填充“表”的函式,該物件是直接從 qml 呼叫的(“OnClicked:”),嘗試了 make 建構式,它將獲取具有 2D 陣列的物件。我看過大量的視頻并閱讀了檔案,但實際上不知道該怎么做。
所以整個鏈是:選擇檔案? -> 讀取檔案? -> 解碼? -> 填充 2D 陣列? -> 發送到模型(不知何故) -> 出現在 UI 中?
如果我將我的二維陣列設為全域陣列,也許可以做到這一點,這樣我就可以從任何地方訪問它,但這不是一個解決方案。
謝謝!
uj5u.com熱心網友回復:
最簡單的修改是將 Q_INVOKABLE 添加到允許設定表的 TableModel
class TableModel : public QAbstractTableModel
{
...
Q_INVOKABLE void setTable(const QVariant& value)
{ //inline for briefity
beginResetModel();
table = value.value<QVector<QVector<QString>>>();
endResetModel();
}
}
然后從 QML 你可以做<id of TableModel>.setTable(fileOperator.getVector())
另一種選擇是具有類似 setter 方法的 Q_PROPERTY。然后你會使用<id of TableModel>.table = fileOperator.getVector()
我不確定你希望你的結構是什么樣子,但如果你不需要 TableModel 可以從 QML 實體化,你可以像使用fileOperator. 然后您將對 TableModel 進行更多控制,例如,您可以在完成后從 C 端呼叫填充函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/528361.html
標籤:C qtqmlqt6
