Results 1 to 2 of 2

Thread: QTextStream/QIODevice issue.

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextStream/QIODevice issue.

    Hello there, I'm asking for help here once again.

    I have such function:
    Qt Code:
    1. void QuestCreator::onSaveQuestClicked()
    2. {
    3. if(ui->FileNameEdit->text() == "" || ui->SavePathEdit->text() == "")
    4. {
    5. QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, file name or save path is empty.");
    6. return;
    7. }
    8.  
    9. QTextStream quest("", QIODevice::ReadWrite | QIODevice::Text);
    10. quest << "local items = {";
    11.  
    12. for(int i = 0; i < ui->ItemsList->columnCount(); i++)
    13. {
    14. QTreeWidgetItem *item = ui->ItemsList->takeTopLevelItem(i);
    15. if(item != NULL)
    16. {
    17. quest << "\t\t[" << QString::number(i) << "] = {" << item->text(0) << ", " << item->text(1) << "},";
    18. ui->ItemsList->addTopLevelItem(item); // I don't know why, but when I call takeTopLevelItem(int) function then element is being removed from list, that's why I add it back to list here.
    19. }
    20. }
    21.  
    22. quest << "}";
    23. quest << "\nfunction onUse(cid, item, fromPosition, itemEx, toPosition)";
    24. quest << "\n\tif getCreatureStorage(cid, " << ui->StorageKeyEdit->text() << ") > -1) then";
    25. quest << "\n\t\tdoPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, \"" << ui->FailureMessageEdit->text() << "\")";
    26. quest << "\n\t\treturn true";
    27. quest << "\n\tend";
    28. quest << "\n";
    29.  
    30. if(ui->ItemsInContainer->isChecked())
    31. quest << "\n\tlocal container = doPlayerAddItem(cid, " << ui->ContainerIDEdit << ", 1)";
    32.  
    33. quest << "\n\tfor _, item in ipairs(items) do";
    34. if(ui->ItemsInContainer->isChecked())
    35. quest << "doAddContainerItem(container, item[1], item[2])";
    36. else
    37. quest << "doPlayerAddItem(cid, item[1], item[2])";
    38.  
    39. quest << "\n\tend";
    40. quest << "\n\tdoPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, \"\")";
    41. quest << "\n\tdoCreatureSetStorage(cid, " << ui->StorageKeyEdit->text() << ", 1)";
    42. quest << "\n\treturn true";
    43. quest << "\nend";
    44.  
    45. if(!ui->FileNameEdit->text().contains(".lua"))
    46. ui->FileNameEdit->setText(ui->FileNameEdit->text() + ".lua");
    47.  
    48. QFile questFile(ui->SavePathEdit->text() + "\\" + ui->FileNameEdit->text());
    49. if(!questFile.exists())
    50. {
    51. if(questFile.open(QIODevice::WriteOnly | QIODevice::Text))
    52. {
    53. questFile.write(quest.string()->toUtf8(), quest.string()->length());
    54. questFile.close();
    55. QMessageBox::information(this, "Quest Creator", "Quest has been saved.");
    56. }
    57. else
    58. QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, failed to open file.");
    59. }
    60. else
    61. QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, file already exists.");
    62. }
    To copy to clipboard, switch view to plain text mode 

    When I initialize QTextStream without paremeters, I'm getting errors about not set QIODevice, when I tried to add device (as you can see in constructor) I've started to get Segmentation Fault. I've debuged code line by line and the problem is here:
    Qt Code:
    1. questFile.write(quest.string()->toUtf8(), quest.string()->length());
    To copy to clipboard, switch view to plain text mode 

    Could anyone show me some steps how do I fix this problem?

    @Edit:
    Well, nevermind. Solved it on my own, just added QString result; and changed QTextStream quest constructor to QTextStream quest(&result) then used result variable while writing to file.
    Last edited by Diath; 25th December 2010 at 02:41.

  2. #2
    Join Date
    Mar 2008
    Location
    Marslev, Denmark
    Posts
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QTextStream/QIODevice issue.

    You have to do it the other way around. First, you create the QFile and open it. Then you create the QTextStream object and give it the QFile object as the first argument.
    Bo Thorsen, Viking Software
    Qt applications on Linux and Windows

Similar Threads

  1. QIODevice and QTextCodec?
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2010, 18:50
  2. QIODevice
    By sabeesh in forum Newbie
    Replies: 1
    Last Post: 26th September 2007, 10:01
  3. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  4. QIODEvice
    By mickey in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 19:00
  5. QIODevice::bytesAvailable() always returning 0
    By quickNitin in forum Newbie
    Replies: 6
    Last Post: 14th June 2006, 13:04

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.