PDA

View Full Version : QTextStream/QIODevice issue.



Diath
25th December 2010, 03:27
Hello there, I'm asking for help here once again.

I have such function:

void QuestCreator::onSaveQuestClicked()
{
if(ui->FileNameEdit->text() == "" || ui->SavePathEdit->text() == "")
{
QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, file name or save path is empty.");
return;
}

QTextStream quest("", QIODevice::ReadWrite | QIODevice::Text);
quest << "local items = {";

for(int i = 0; i < ui->ItemsList->columnCount(); i++)
{
QTreeWidgetItem *item = ui->ItemsList->takeTopLevelItem(i);
if(item != NULL)
{
quest << "\t\t[" << QString::number(i) << "] = {" << item->text(0) << ", " << item->text(1) << "},";
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.
}
}

quest << "}";
quest << "\nfunction onUse(cid, item, fromPosition, itemEx, toPosition)";
quest << "\n\tif getCreatureStorage(cid, " << ui->StorageKeyEdit->text() << ") > -1) then";
quest << "\n\t\tdoPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, \"" << ui->FailureMessageEdit->text() << "\")";
quest << "\n\t\treturn true";
quest << "\n\tend";
quest << "\n";

if(ui->ItemsInContainer->isChecked())
quest << "\n\tlocal container = doPlayerAddItem(cid, " << ui->ContainerIDEdit << ", 1)";

quest << "\n\tfor _, item in ipairs(items) do";
if(ui->ItemsInContainer->isChecked())
quest << "doAddContainerItem(container, item[1], item[2])";
else
quest << "doPlayerAddItem(cid, item[1], item[2])";

quest << "\n\tend";
quest << "\n\tdoPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, \"\")";
quest << "\n\tdoCreatureSetStorage(cid, " << ui->StorageKeyEdit->text() << ", 1)";
quest << "\n\treturn true";
quest << "\nend";

if(!ui->FileNameEdit->text().contains(".lua"))
ui->FileNameEdit->setText(ui->FileNameEdit->text() + ".lua");

QFile questFile(ui->SavePathEdit->text() + "\\" + ui->FileNameEdit->text());
if(!questFile.exists())
{
if(questFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
questFile.write(quest.string()->toUtf8(), quest.string()->length());
questFile.close();
QMessageBox::information(this, "Quest Creator", "Quest has been saved.");
}
else
QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, failed to open file.");
}
else
QMessageBox::critical(this, "Quest Creator", "Couldn't save quest, file already exists.");
}

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:

questFile.write(quest.string()->toUtf8(), quest.string()->length());

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.

bothorsen
28th December 2010, 22:44
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.