Freezing text controls when I try to modify text from c++
Hi,
QML Text controls is freezing for several second when I try to modify text from c++.
My code:
MyXML.h:
Code:
#ifndef MYXMLCLASS_H
#define MYXMLCLASS_H
#include <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();
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:
.
.
.
private:
.
.
.
};
#endif // MYXMLCLASS_H
MyXML.cpp:
Code:
#include <mymxl.h>
#include <QFileDialog>
#include <QXmlStreamWriter>
MyXML::MyXML()
{
newFileName = "";
}
{
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:
Code:
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")
}
}
Re: Freezing text controls when I try to modify text from c++
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,
_
Re: Freezing text controls when I try to modify text from c++
Quote:
Originally Posted by
anda_skoa
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.
Quote:
Originally Posted by
anda_skoa
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
Re: Freezing text controls when I try to modify text from c++
Quote:
Originally Posted by
neda
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?
Quote:
Originally Posted by
neda
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,
_
Re: Freezing text controls when I try to modify text from c++
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:
Code:
<?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:
Code:
MyXML::MyXML()
{
newFileName = "";
}
{
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()
{
{
//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()) {
setpROPERTY1(attr.value().toString());
}
setpROPERTY2(attr.value().toString());
}
setpROPERTY3(attr.value().toString());
}
setpROPERTY4(attr.value().toString());
}
setpROPERTY5(attr.value().toString());
}
}
}
else if (reader.name() == "recorde") {
?????????
}
}
}
}
Re: Freezing text controls when I try to modify text from c++
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-modelv...tractitemmodel
Cheers,
_
Re: Freezing text controls when I try to modify text from c++
Quote:
Originally Posted by
anda_skoa
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.
Re: Freezing text controls when I try to modify text from c++
Ok, very strange.
Can you provide/attach a minimal program that shows the problem?
Cheers,
_
Re: Freezing text controls when I try to modify text from c++
Quote:
Originally Posted by
anda_skoa
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?
Re: Freezing text controls when I try to modify text from c++
What is a "non-English name" ???
Re: Freezing text controls when I try to modify text from c++
I mean the file name is not English.
Re: Freezing text controls when I try to modify text from c++
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
Re: Freezing text controls when I try to modify text from c++
Quote:
Originally Posted by
ChriD
You may try the "decodeName" method on the QFile class
Yes,Thank you. my problem is solved.