Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active August 19, 2017 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/11ae5d8b07c6c4b9bd0c0ef52ed4335e to your computer and use it in GitHub Desktop.
Save eyllanesc/11ae5d8b07c6c4b9bd0c0ef52ed4335e to your computer and use it in GitHub Desktop.
Test
#include "boot.h"
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QTimer>
Boot::Boot(QObject *parent) : QObject(parent)
{
}
void Boot::load()
{
loadXMLContent();
}
QString Boot::errorMsg()
{
return m_errorMsg;
}
void Boot::setErrorMsg(const QString &errorMsg)
{
if(m_errorMsg == errorMsg)
return;
m_errorMsg = errorMsg;
emit errorMsgChanged();
}
QString Boot::xmlContent()
{
return m_xmlContent;
}
void Boot::setXmlContent(const QString &xmlContent)
{
if(m_xmlContent == xmlContent)
return;
m_xmlContent = xmlContent;
emit xmlContentChanged();
}
bool Boot::loadXMLContent()
{
bool res;
timer = new QTimer(this);
setErrorMsg("Parsing channels.xml");
setErrorMsg("Trying to open channels.xml");
channelsList.open("config/channels.xml");
if(!channelsList.is_open())
{
setErrorMsg("There was an error opening channels.xml");
res = false;
}
else
{
setErrorMsg("channels.xml was opened successfully");
setErrorMsg("now reading from channels.xml");
connect(timer, &QTimer::timeout, [this](){
if(!channelsList.eof())
{
std::string temp;
getline(channelsList, temp);
QString qTemp = QString::fromStdString(temp);
setErrorMsg("Parsing " +qTemp);
tempXMLContent += qTemp+"\n";
}
else{
setXmlContent(tempXMLContent);
setErrorMsg("Parsing is complete");
}
});
timer->start(10);
res = true;
}
return res;
}
#ifndef BOOT_H
#define BOOT_H
#include <QObject>
#include <fstream>
class QTimer;
class Boot : public QObject
{
Q_OBJECT
Q_PROPERTY(QString errorMsg READ errorMsg NOTIFY errorMsgChanged)
Q_PROPERTY(QString xmlContent READ xmlContent NOTIFY xmlContentChanged)
public:
explicit Boot(QObject *parent = nullptr);
Q_INVOKABLE void load();
bool loadXMLContent();
QString errorMsg();
void setErrorMsg(const QString &errorMsg);
QString xmlContent();
void setXmlContent(const QString &xmlContent);
signals:
void errorMsgChanged();
void xmlContentChanged();
private:
QString m_errorMsg;
QString m_xmlContent;
QTimer *timer;
QString tempXMLContent;
std::ifstream channelsList;
};
#endif // BOOT_H
#include "boot.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QCoreApplication::setApplicationName("xyz");
QCoreApplication::setAttribute(Qt::AA_X11InitThreads);
QQuickView quickView;
qmlRegisterType<Boot>("xyz", 1, 0, "Boot");
quickView.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
quickView.setResizeMode(QQuickView::SizeRootObjectToView);
quickView.show();
return app.exec();
}
import QtQuick 2.0
import "."
import QtQuick.XmlListModel 2.0
import xyz 1.0
Item{
Timer {
id: timer
}
function delay(delayTime, cb) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.start();
}
Loader{
id: bootPageLoader
anchors.fill:parent
sourceComponent: bootSystem
focus:true
}
Component{
id:bootSystem
Rectangle{
width: 640
height: 480
color:"black"
focus:true
Component.onCompleted: {
booting.load();
}
Boot{
id:booting
onErrorMsgChanged: {
console.log("New Boot Log Message: " + errorMsg);
//This is where I am adding to the listview every time I receive a signal
logModel.append({msg:errorMsg});
log.positionViewAtEnd();
}
}
ListView {
id:log
anchors.left: parent.left
anchors.leftMargin: 10
anchors.topMargin: 10
anchors.bottomMargin:10
anchors.top: parent.top
width:(parent.width*40)/100
height:parent.height-20
model: ListModel{
id:logModel
}
delegate: Text {
text: msg
color: "green"
font.pixelSize: 15
}
}
Text{
id: loading
anchors{
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
}
text: "LOADING..."
color: "white"
font.pixelSize: Math.round(parent.height/20)
font.bold: true
}
}
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
TEMPLATE = app
QT += qml quick
CONFIG += c++11
SOURCES += main.cpp \
boot.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
boot.h
@Muhand
Copy link

Muhand commented Aug 19, 2017

What if I want to read a file? here is my code

`
bool Boot::loadXMLContent()
{
bool res;
ifstream channelsList;
QString tempXMLContent;

setErrorMsg("Parsing channels.xml");
setErrorMsg("Trying to open channels.xml");

channelsList.open("config/channels.xml");


if(!channelsList.is_open())
{
    setErrorMsg("There was an error opening channels.xml");
    res = false;
}
else
{
    setErrorMsg("channels.xml was opened successfully");
    setErrorMsg("now reading from channels.xml");

    while(!channelsList.eof())
    {
        string temp;
        getline(channelsList, temp);
        QString qTemp = QString::fromStdString(temp);

        setErrorMsg("Parsing " +qTemp);
        tempXMLContent += qTemp+"\n";
    }

    setXmlContent(tempXMLContent);

    setErrorMsg("Parsing is complete");

    res = true;
}


return res;

}`
This is the code of XmlContent

in boot.h add
Q_PROPERTY(QString xmlContent READ xmlContent NOTIFY xmlContentChanged)
QString xmlContent(); void setXmlContent(const QString &xmlContent);
void xmlContentChanged();
QString m_xmlContent;

@Muhand
Copy link

Muhand commented Aug 19, 2017

here is channels.xml
channels.xml

@eyllanesc
Copy link
Author

@Muhand For a few lines the effect is not visible as you have noticed, add more lines and you will see, if you want to be slower you can change the timer to a longer time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment