PDA

View Full Version : Freezing text controls when I try to modify text from c++



neda
22nd February 2016, 09:20
Hi,
QML Text controls is freezing for several second when I try to modify text from c++.

My code:

MyXML.h:

#ifndef MYXMLCLASS_H
#define MYXMLCLASS_H

#include <QObject>

class MyXML: public QObject
{
Q_OBJECT
Q_PROPERTY(QString newPROPERTY1 READ getPROPERTY1 WRITE setPROPERTY1 NOTIFY pROPERTY1Changed)
.
.
.
Q_PROPERTY(QString newPROPERTY18 READ getPROPERTY18 WRITE setPROPERTY18 NOTIFY pROPERTY18Changed)

public:
MyXML();
MyXML(QString);
Q_INVOKABLE QString getPROPERTY1() const;
.
.
.
Q_INVOKABLE QString getPROPERTY18() const;

public slots:
void setPROPERTY1(QString pROPERTY1);
.
.
.
void setPROPERTY18(QString pROPERTY18);

void saveXMLFile();
void readXMLFile();

signals:
void pROPERTY1Changed(QString);
.
.
.
void pROPERTY18Changed(QString);

private:
QString newPROPERTY1;
.
.
.
QString newPROPERTY18;
};

#endif // MYXMLCLASS_H

MyXML.cpp:


#include <mymxl.h>
#include <QFileDialog>
#include <QXmlStreamWriter>

MyXML::MyXML()
{
newFileName = "";
}
MyXML::MyXML(QString fileName)
{
newFileName = fileName;
}

QString MyXML::getPROPERTY1() const
{
return newPROPERTY1;
}
void MyXML::setPROPERTY1(QString pROPERTY1)
{
if (pROPERTY1 != newPROPERTY1)
{
newPROPERTY1 = pROPERTY1;
emit pROPERTY1Changed(pROPERTY1);
}
}
..
..
..
..
..

QString MyXML::getPROPERTY18() const
{
return newPROPERTY18;
}
void MyXML::setPROPERTY18(QString pROPERTY18)
{
if (pROPERTY18 != newPROPERTY18)
{
newPROPERTY18 = pROPERTY18;
emit pROPERTY18Changed(pROPERTY18);
}
}

void MyXML::saveXMLFile()
{
...
...
...
}

void MyXML::readXMLFile()
{
setPROPERTY2("neda2");
...
...
...
setPROPERTY18("neda18");

...

...
...
...


}


main.qml:


Input {
id:inputPROPERTY2
text: myXML.newPROPERTY2

}

...
...
...
...
...
...
Input {
id:inputPROPERTY18
text: myXML.newPROPERTY18

}

MyToolButton{

MouseArea
{

onClicked: {
openFileDialog.open()
}

}

}
FileDialog {
id: openFileDialog
title: "please select xml file"
folder: shortcuts.home
nameFilters: [ "XML files (*.XML *.xml)" ]
selectedNameFilter: "XML files (*.XML *.xml)"

onAccepted: {
console.log("You chose: " + openFileDialog.fileUrl)
var path = openFileDialog.fileUrl.toString();
// remove prefixed "file:///"
path= path.replace(/^(file:\/{3})|(qrc:\/{2})|(http:\/{2})/,"");

myXML.setPROPERTY1(path)
myXML.readXMLFile()
}
onRejected: {
console.log("Canceled")
}
}

anda_skoa
22nd February 2016, 10:01
Are you sure it freezes when you update the properties, not when parsing the XML file?

Also with so many properties of the same type it makes me wonder if you are not looking for a model.

Cheers,
_

neda
22nd February 2016, 11:41
Are you sure it freezes when you update the properties, not when parsing the XML file?

_

Yes, I remove the code that is related to reading XML file, and just put "setPROPERTY 2("neda 2");" for test.




Also with so many properties of the same type it makes me wonder if you are not looking for a model.

_

I am beginner in Qt and have not experience. :(
I want to show data of XML file in text controls, please guide me to write better code.
Thank you

anda_skoa
22nd February 2016, 11:58
Yes, I remove the code that is related to reading XML file, and just put "setPROPERTY 2("neda 2");" for test.

Then there is likely something wrong.
Have you tried reducing the code to just have two properties?



I am beginner in Qt and have not experience. :(
I want to show data of XML file in text controls, please guide me to write better code.
Thank you

For showing data from an XML file you could have a look at XmlListModel.

Cheers,
_

neda
23rd February 2016, 08:11
I think I'd better complete code here.

I have 5 text field control and one tableview.

User can add row to tableview (contain 12 column (prop6 - 16 and image)).
I must save data (all row of tableview and text of 5 controls and one image) in XML file,
and when I open XML file, it shows data in tableview and text controls.

QML Text controls (propery 1 -5) is freezing for 6 second when I try to modify text from c++.

I have no idea how to write code for this process.

My XML file:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<header prop1="..." prop2="..." prop3="..." prop4="..." prop5="..." />

<record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
<record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
.......
.......
.......
<record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
</NewDataSet>


myxml.cpp:

MyXML::MyXML()
{
newFileName = "";
}
MyXML::MyXML(QString pROPERTY18)
{
newPROPERTY18 = pROPERTY18;
}

QString MyXML::getPROPERTY1() const
{
return newPROPERTY1;
}
void MyXML::setPROPERTY1(QString pROPERTY1)
{
if (pROPERTY1 != newPROPERTY1)
{
newPROPERTY1 = pROPERTY1;
emit pROPERTY1Changed(pROPERTY1);
}
}
..
..
..
..
..

QString MyXML::getPROPERTY18() const
{
return newPROPERTY18;
}
void MyXML::setPROPERTY18(QString pROPERTY18)
{
if (pROPERTY18 != newPROPERTY18)
{
newPROPERTY18 = pROPERTY18;
emit pROPERTY18Changed(pROPERTY18);
}
}

void MyXML::saveXMLFile()
{
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();

xmlWriter.writeStartElement("NewDataSet");

xmlWriter.writeStartElement("header");
xmlWriter.writeAttribute("pROPERTY1",newpROPERTY1);
xmlWriter.writeAttribute("pROPERTY2",newpROPERTY2);
xmlWriter.writeAttribute("pROPERTY3",newpROPERTY3);
xmlWriter.writeAttribute("pROPERTY4",newpROPERTY4);
xmlWriter.writeAttribute("pROPERTY5",newpROPERTY5);
xmlWriter.writeEndElement();

????? save data of tableview

xmlWriter.writeEndElement();

file.close();
}
}


void MyXML::readXMLFile()
{
QFile file(pROPERTY18);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
//qDebug() << "Error: Cannot read file ";
return;
}
QXmlStreamReader reader(file.readAll());
file.close();
while(!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
if (reader.name() == "header") {//After about 6 seconds the text is displayed in controls
foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
if(attr.name().toString() == QLatin1String("pROPERTY1")){
setpROPERTY1(attr.value().toString());
}
else if(attr.name().toString() == QLatin1String("pROPERTY2")){
setpROPERTY2(attr.value().toString());
}
else if(attr.name().toString() == QLatin1String("pROPERTY3")){
setpROPERTY3(attr.value().toString());
}
else if(attr.name().toString() == QLatin1String("pROPERTY4")){
setpROPERTY4(attr.value().toString());
}
else if(attr.name().toString() == QLatin1String("pROPERTY5")){
setpROPERTY5(attr.value().toString());
}
}
}
else if (reader.name() == "recorde") {

?????????

}
}
}
}

anda_skoa
23rd February 2016, 09:07
Your comment suggests that you get the freezing when updating the values during XML parsing, but earlier you said it would happen even when not loading from XML?

In any case you will need a model to handle the table, see http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel

Cheers,
_

neda
23rd February 2016, 09:48
Your comment suggests that you get the freezing when updating the values during XML parsing, but earlier you said it would happen even when not loading from XML?

_

Yes, it would happen even when not loading from XML.

anda_skoa
23rd February 2016, 10:08
Ok, very strange.

Can you provide/attach a minimal program that shows the problem?

Cheers,
_

neda
24th February 2016, 04:52
Ok, very strange.

Can you provide/attach a minimal program that shows the problem?

_

Thank you.
I figure out my problem.

This problem will happen when I open th file name is not English.. :(
Is there a way to solve this problem?

ChriD
24th February 2016, 09:01
What is a "non-English name" ???

neda
24th February 2016, 10:07
I mean the file name is not English.

ChriD
24th February 2016, 11:16
Thats no serious answer.... A german or italian or french filename will work too.
Do you mean a filename with another charset?

You may try the "decodeName" method on the QFile class

neda
24th February 2016, 11:35
You may try the "decodeName" method on the QFile class


Yes,Thank you. my problem is solved.