I'm Giving value to vector<QString> from xlsx file dynamically ,how can I display that vactor value to UI (QML)?
-
Myclass.cpp
#include "myclass.h"
#include "QDebug"
#include "QVariant"
#include <vector>
using namespace QXlsx;
using namespace std;
vector<QString> data;
MyClass::MyClass(QObject *parent) :QObject(parent)
{}
void MyClass::callme(QString address)
{
s++;
QVariant var;
QXlsx::Document xlsxR(address);
if(xlsxR.load())
{
Cell* cell;
int col=1,row=1;
for(;row<=xlsxR.dimension().lastRow();row++)
{
for(col=1;col<=xlsxR.dimension().lastColumn();col++)
{
cell=xlsxR.cellAt(row,col);
var=cell->readValue();
qDebug()<<var.toString();
data.push_back(var.toString());} }
}
else{
qDebug()<<"Not Loaded";
}}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myclass.h"
#include <QQmlContext>
#include <QVector>
#include <thread>
int main(int argc, char *argv[])
{#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv); MyClass obj;//object of Myclass QQmlApplicationEngine engine; 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); QQmlContext *rootContext=engine.rootContext(); //rootContext is like global namespace in QML rootContext->setContextProperty("A",&obj); return app.exec();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.15
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.1
import QtQuick.Controls 1.4
ApplicationWindow{
visible:true
height: 500
width: 800
Button {
x:720
y:0
text: "Add_Tab"
}
//----------------------------File Open function---------------------------------------
function openFile(fileUrl) {
var request = new XMLHttpRequest();
request.open("GET", fileUrl, false);
request.send(null);
return request.responseText;
}//-----------------TAB-1-------------------------------------------------------------- TabView { anchors.fill: parent Tab { title: "HOME" Rectangle { color: "skyblue" } } //=================TAB-2================================================================ Tab { title: "QGC" Rectangle { color: "skyblue" anchors.fill: parent } } //=================TAB-3==================================================================== Tab { title: "Config" Rectangle { color: "skyblue" anchors.fill: parent Label { id: label x:10 y:13 text: "Location :-" } TextArea { id: txt x:100 y:10 width: 400 height: 30 } TextArea { id: txt1 x:50 y:50 width: 600 height:400 text: _aString Button { text: "Get" onClicked: A.getData(); } } FileDialog { id: openFileDialog folder: shortcuts.home nameFilters: ["(*.xlsx)",] onAccepted: { var path = openFileDialog.fileUrl.toString() path = path.replace(/^(file:\/{2})/,""); //removing file part txt.text=path } } Button { x:530 y:12 text: "browse" onClicked: openFileDialog.visible = true } Button { x:620 y:12 text: "Open" onClicked: A.callme(txt.text); } } }//-------end tab3--------------------------------------------------------------- }
}
-
The easiest way to pass such data to QML is using
QStringListModel
.