Results 1 to 13 of 13

Thread: Freezing text controls when I try to modify text from c++

  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. #ifndef MYXMLCLASS_H
    2. #define MYXMLCLASS_H
    3.  
    4. #include <QObject>
    5.  
    6. class MyXML: public QObject
    7. {
    8. Q_OBJECT
    9. Q_PROPERTY(QString newPROPERTY1 READ getPROPERTY1 WRITE setPROPERTY1 NOTIFY pROPERTY1Changed)
    10. .
    11. .
    12. .
    13. Q_PROPERTY(QString newPROPERTY18 READ getPROPERTY18 WRITE setPROPERTY18 NOTIFY pROPERTY18Changed)
    14.  
    15. public:
    16. MyXML();
    17. MyXML(QString);
    18. Q_INVOKABLE QString getPROPERTY1() const;
    19. .
    20. .
    21. .
    22. Q_INVOKABLE QString getPROPERTY18() const;
    23.  
    24. public slots:
    25. void setPROPERTY1(QString pROPERTY1);
    26. .
    27. .
    28. .
    29. void setPROPERTY18(QString pROPERTY18);
    30.  
    31. void saveXMLFile();
    32. void readXMLFile();
    33.  
    34. signals:
    35. void pROPERTY1Changed(QString);
    36. .
    37. .
    38. .
    39. void pROPERTY18Changed(QString);
    40.  
    41. private:
    42. QString newPROPERTY1;
    43. .
    44. .
    45. .
    46. QString newPROPERTY18;
    47. };
    48.  
    49. #endif // MYXMLCLASS_H
    To copy to clipboard, switch view to plain text mode 

    MyXML.cpp:

    Qt Code:
    1. #include <mymxl.h>
    2. #include <QFileDialog>
    3. #include <QXmlStreamWriter>
    4.  
    5. MyXML::MyXML()
    6. {
    7. newFileName = "";
    8. }
    9. MyXML::MyXML(QString fileName)
    10. {
    11. newFileName = fileName;
    12. }
    13.  
    14. QString MyXML::getPROPERTY1() const
    15. {
    16. return newPROPERTY1;
    17. }
    18. void MyXML::setPROPERTY1(QString pROPERTY1)
    19. {
    20. if (pROPERTY1 != newPROPERTY1)
    21. {
    22. newPROPERTY1 = pROPERTY1;
    23. emit pROPERTY1Changed(pROPERTY1);
    24. }
    25. }
    26. ..
    27. ..
    28. ..
    29. ..
    30. ..
    31.  
    32. QString MyXML::getPROPERTY18() const
    33. {
    34. return newPROPERTY18;
    35. }
    36. void MyXML::setPROPERTY18(QString pROPERTY18)
    37. {
    38. if (pROPERTY18 != newPROPERTY18)
    39. {
    40. newPROPERTY18 = pROPERTY18;
    41. emit pROPERTY18Changed(pROPERTY18);
    42. }
    43. }
    44.  
    45. void MyXML::saveXMLFile()
    46. {
    47. ...
    48. ...
    49. ...
    50. }
    51.  
    52. void MyXML::readXMLFile()
    53. {
    54. setPROPERTY2("neda2");
    55. ...
    56. ...
    57. ...
    58. setPROPERTY18("neda18");
    59.  
    60. ...
    61.  
    62. ...
    63. ...
    64. ...
    65.  
    66.  
    67. }
    To copy to clipboard, switch view to plain text mode 


    main.qml:

    Qt Code:
    1. Input {
    2. id:inputPROPERTY2
    3. text: myXML.newPROPERTY2
    4.  
    5. }
    6.  
    7. ...
    8. ...
    9. ...
    10. ...
    11. ...
    12. ...
    13. Input {
    14. id:inputPROPERTY18
    15. text: myXML.newPROPERTY18
    16.  
    17. }
    18.  
    19. MyToolButton{
    20.  
    21. MouseArea
    22. {
    23.  
    24. onClicked: {
    25. openFileDialog.open()
    26. }
    27.  
    28. }
    29.  
    30. }
    31. FileDialog {
    32. id: openFileDialog
    33. title: "please select xml file"
    34. folder: shortcuts.home
    35. nameFilters: [ "XML files (*.XML *.xml)" ]
    36. selectedNameFilter: "XML files (*.XML *.xml)"
    37.  
    38. onAccepted: {
    39. console.log("You chose: " + openFileDialog.fileUrl)
    40. var path = openFileDialog.fileUrl.toString();
    41. // remove prefixed "file:///"
    42. path= path.replace(/^(file:\/{3})|(qrc:\/{2})|(http:\/{2})/,"");
    43.  
    44. myXML.setPROPERTY1(path)
    45. myXML.readXMLFile()
    46. }
    47. onRejected: {
    48. console.log("Canceled")
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 22nd February 2016 at 09:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Freezing text controls when I try to modify text from c++

    Quote Originally Posted by anda_skoa View Post
    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 View Post

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Freezing text controls when I try to modify text from c++

    Quote Originally Posted by neda View Post
    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 View Post
    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    neda (23rd February 2016)

  6. #5
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. <?xml version="1.0" standalone="yes"?>
    2. <NewDataSet>
    3. <header prop1="..." prop2="..." prop3="..." prop4="..." prop5="..." />
    4.  
    5. <record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
    6. <record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
    7. .......
    8. .......
    9. .......
    10. <record prop6=".." prop7=".." prop8="..." prop9=".." prop10=".." prop11=".." prop12="..." prop13="..." prop14=..." prop15="..." prop16="..." image="iVBORw0KGgoAAtKdOq1aPGLd/Y8QAQEprZ1a7P3oLIBAIgBRIRtcI8w/ ...." />
    11. </NewDataSet>
    To copy to clipboard, switch view to plain text mode 


    myxml.cpp:
    Qt Code:
    1. MyXML::MyXML()
    2. {
    3. newFileName = "";
    4. }
    5. MyXML::MyXML(QString pROPERTY18)
    6. {
    7. newPROPERTY18 = pROPERTY18;
    8. }
    9.  
    10. QString MyXML::getPROPERTY1() const
    11. {
    12. return newPROPERTY1;
    13. }
    14. void MyXML::setPROPERTY1(QString pROPERTY1)
    15. {
    16. if (pROPERTY1 != newPROPERTY1)
    17. {
    18. newPROPERTY1 = pROPERTY1;
    19. emit pROPERTY1Changed(pROPERTY1);
    20. }
    21. }
    22. ..
    23. ..
    24. ..
    25. ..
    26. ..
    27.  
    28. QString MyXML::getPROPERTY18() const
    29. {
    30. return newPROPERTY18;
    31. }
    32. void MyXML::setPROPERTY18(QString pROPERTY18)
    33. {
    34. if (pROPERTY18 != newPROPERTY18)
    35. {
    36. newPROPERTY18 = pROPERTY18;
    37. emit pROPERTY18Changed(pROPERTY18);
    38. }
    39. }
    40.  
    41. void MyXML::saveXMLFile()
    42. {
    43. QXmlStreamWriter xmlWriter(&file);
    44. xmlWriter.setAutoFormatting(true);
    45. xmlWriter.writeStartDocument();
    46.  
    47. xmlWriter.writeStartElement("NewDataSet");
    48.  
    49. xmlWriter.writeStartElement("header");
    50. xmlWriter.writeAttribute("pROPERTY1",newpROPERTY1);
    51. xmlWriter.writeAttribute("pROPERTY2",newpROPERTY2);
    52. xmlWriter.writeAttribute("pROPERTY3",newpROPERTY3);
    53. xmlWriter.writeAttribute("pROPERTY4",newpROPERTY4);
    54. xmlWriter.writeAttribute("pROPERTY5",newpROPERTY5);
    55. xmlWriter.writeEndElement();
    56.  
    57. ????? save data of tableview
    58.  
    59. xmlWriter.writeEndElement();
    60.  
    61. file.close();
    62. }
    63. }
    64.  
    65.  
    66. void MyXML::readXMLFile()
    67. {
    68. QFile file(pROPERTY18);
    69. if (!file.open(QFile::ReadOnly | QFile::Text))
    70. {
    71. //qDebug() << "Error: Cannot read file ";
    72. return;
    73. }
    74. QXmlStreamReader reader(file.readAll());
    75. file.close();
    76. while(!reader.atEnd()) {
    77. reader.readNext();
    78. if (reader.isStartElement()) {
    79. if (reader.name() == "header") {//After about 6 seconds the text is displayed in controls
    80. foreach(const QXmlStreamAttribute &attr, reader.attributes()) {
    81. if(attr.name().toString() == QLatin1String("pROPERTY1")){
    82. setpROPERTY1(attr.value().toString());
    83. }
    84. else if(attr.name().toString() == QLatin1String("pROPERTY2")){
    85. setpROPERTY2(attr.value().toString());
    86. }
    87. else if(attr.name().toString() == QLatin1String("pROPERTY3")){
    88. setpROPERTY3(attr.value().toString());
    89. }
    90. else if(attr.name().toString() == QLatin1String("pROPERTY4")){
    91. setpROPERTY4(attr.value().toString());
    92. }
    93. else if(attr.name().toString() == QLatin1String("pROPERTY5")){
    94. setpROPERTY5(attr.value().toString());
    95. }
    96. }
    97. }
    98. else if (reader.name() == "recorde") {
    99.  
    100. ?????????
    101.  
    102. }
    103. }
    104. }
    105. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 23rd February 2016 at 08:16.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  8. #7
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Freezing text controls when I try to modify text from c++

    Quote Originally Posted by anda_skoa View Post
    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  10. #9
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Freezing text controls when I try to modify text from c++

    Quote Originally Posted by anda_skoa View Post
    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?
    Last edited by neda; 24th February 2016 at 10:08.

  11. #10
    Join Date
    Dec 2015
    Location
    Austria
    Posts
    23
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: Freezing text controls when I try to modify text from c++

    What is a "non-English name" ???

  12. #11
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Freezing text controls when I try to modify text from c++

    I mean the file name is not English.

  13. #12
    Join Date
    Dec 2015
    Location
    Austria
    Posts
    23
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default 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
    Last edited by ChriD; 24th February 2016 at 11:34.

  14. The following user says thank you to ChriD for this useful post:

    neda (24th February 2016)

  15. #13
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Freezing text controls when I try to modify text from c++

    Quote Originally Posted by ChriD View Post

    You may try the "decodeName" method on the QFile class
    Yes,Thank you. my problem is solved.
    Last edited by neda; 24th February 2016 at 11:53.

Similar Threads

  1. modify a QML Text from C++
    By neda in forum Qt Quick
    Replies: 7
    Last Post: 16th February 2016, 09:04
  2. Replies: 1
    Last Post: 11th May 2014, 08:29
  3. Replies: 1
    Last Post: 24th April 2010, 15:31
  4. How to modify the content of text file
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2009, 10:23
  5. how to modify the text of node in xml docment?
    By GChen in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2009, 10:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.