Saving and loading text files that aren't plain text - c++

So, I'm making a text editor in Qt. And It does mostly very basic functions. New file, save, open, cut, copy, paste, bold, underline, italic, undo, and redo. But, when I bold, italicize, or underline text and then open the text file again It opens as plain text. As in if you open a file, bold some text, save it and then open it again it will be in plain text not bolded or anything. I have tried reading about how to make it work the way I want, but with no luck. My program can even create/open rich text files, but it works the exact same way. I understand that the way I have written the code makes it work the way it does. I just don't know how to "fix" it.
Here are the blocks of code I have written that save and open files using the QFileDialog:
void WordWriteMain::on_actionOpen_File_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open a file","","Text (*.txt);;Rich Text (*.rtf)");
QFile file(fileName);
if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
ui->textBox->setText((file.readAll()));
}
}
void WordWriteMain::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,"Open a file","","Text (*.txt);;Rich Text (*.rtf)");
QFile file(fileName);
if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
file.write(ui->textBox->toPlainText().toUtf8());
}
}
Is what I'm trying to do even possible using QFileDialog? And like I said I understand the line file.write(ui->textBox->toPlainText().Utf8()); converts all of the text into plain text, but I don't know how to make the save slot work without doing it that way.
As always thank you so much for dedicating your time to help me and for reading my question. If there is any more information needed I would be more than happy to provide it. EDIT: Also, I would like in the future to be able to add paragraph aligning so should I make my text editor strictly rich text?

Use toHtml() instead of toPlainText().
When setting use setHtml() instead setText()
Also use toAscii() instead of toUtf8() to do not loose user's language code page.
See: http://doc.qt.io/archives/qt-4.7/qtextedit.html

Related

QT label setText() while preserving text formatting

I have designed a label in QT Designer and need to set the text from the cpp implementation. This works. But, the text formmatting (size and bold) is being removed.
How can I set the text while preserving the formatting done in the ui file?
Sample:
ui->label_version->setText(QString::fromStdString("1.0.0"));
You can open your ui file with text editor to check what test is set. In real Qt uses HTML to format text. Also, you can set stylesheet.
To save formatting you can try this:
1. Set text "%1" in ui
2. Save text before the change
3. Use saved text + .arg to set new text
Constructor()
{
this->m_savedText = m_ui->myLabel->text();
m_ui->myLabel->setText(this->m_savedText.arg("Default text"));
}
handler()
{
m_ui->myLabel->setText(this->m_savedText.arg("New text"));
}
I found the real issue. I had set the text formatting within the rich text editor. What I needed to do was to set the formatting for the label withing the QtWidget properties. When this was changed all was preserved when setting a new text.
The solution that worked for me is to set at the beginning (designer or with code) textFormat as PlainText, with that, whenever you setText, it keeps the format.

edit styles for normal.dotm template

My intention is to edit the Normal.dotm file from word.
All the links i read and the examples talk about open the file in word, which opens as a template, add the styles that i want and save to use it later to create new documents.
In these examples, when i open the file with word, i never see the characters inside the normal.dotm.
When i open the file with (for example) notepad or wordpad, it shows rare characters that i cant read.
It is possible to edit manually(or directly) or add the styles in the text inside the file? or the only way possible is to add the styles when i open the file with word as a template?.
I think once i found a link that said it was incorrect to directly add text to that file manually. But i am not sure of that.
Can you give me some advice?. Thank you.
This is the wrong forum for this type of question. SO is for questions about programming. In future please ask this sort of question on Super User.
To answer your question:
It is possible to edit Normal.dotm outside of Word but only if you are an expert in OOXML. If that is not the case open it inside Word using File | Open.
Documents in Word should be created from a template suited to the purpose, i.e. if you want to create letters first create a letter template with appropriate styles and content. You should not attempt to create all your documents based on the Normal template.

Creating users for game(reading and parsing CSV file)

So I am working on a planets vs zombies type mock up game for class, and am using Qt Creator GUI with C++. One of the things that we are required to do is, on start-up, the game window will attempt to read two files: "pvz_levels.csv" and "pvz_players.csv" from a pre-specified home directory.
The levels file is of the form "level:sequence:rows:start:interval:decrement" and "sequence" itself is a comma separated list of the form (1,1,1,2,3,1,3,1,3,3) which is the sequence in which zombies appear. If this file does not exist in the directory, the program exits with an error.
The players file is of the form "timestamp:player:level"; the time of last play, name of player, and last attempted level, respectively. If this file does not exist, the program silently skips this operation and starts as a new player. If it does exist, the file must be read and parsed, and then used in the program for calculations and such.
So, I am having much trouble with reading and parsing these files. Furthermore, we are required to save the user data in these files, and on the next start-up the user should have the option to continue their game by selecting their respective user from a drop-down list. They should also be able to delete any users.
I am proficient enough with c++ basics but this is my first GUI experience and my prof did not go over it in much detail, so I require quite a bit of help with this project.
Thank you to anyone who is able to help!
Look up the code to an available "csv parser" which stands for "comma separated variable". You need is almost identical, except you use a semi-colon instead of a comma. It seems that by changing one character you parsing is done for you.
You may be able to find a csv parser that accepts the character to be used as the parsing character (I've seen them before).
If you wish I can find a suitable csv parser for your use, but now that you know what you're looking for "csv parser c++ code" it should be a quick Google away.
Also, most csv parsers expect strings to be enclosed to double quotes (") but that is easily modifable.
Some hints:
Just open the File using QFile. Set up a QTextStream and use QTextStream::readLine() to read all Lines into QStringList. Now use QString::split() on the QString saved in the list to get the single values stored in this line. From there you can easily use QString::to* functions to cast the values into your desired type.
For saving just reverse the procedure.
Set up a line using: QString("%1,%2,%3").arg(timestamp).arg(player).arg(level) and put it into your QTextStream. If the stream is connected to a file, this will be written into the file.
I've implemented successfully the CSV reading like this:
std::unique_ptr<QFile> csv_worker(new QFile(resource_path));
QTextStream input_csv(csv_worker.get());
QList<QStringList> data
while(!input_csv.atEnd())
{
//removes the carriage return symbol and splits the elements
QStringList line = input_csv.readLine().remove(QRegExp("\r")).split(","); //replace here with :
data << line;
}
csv_worker->close();
It reads the complete file, each line generates a QStringList.
For the second element in the QStringList, let's say (1,1,1,2,3,1,3,1,3,3}, you have to additionally split that in a sub-QStringList, removing then brackets with something like this:
QStringList sequence = data[i][1].remove(QRegExp("{")).remove(QRegExp("}")).split(",");

fastest way to copy from text file and paste into QListWidget?

i have QListWidget that load from file strings , each string in the file converted to item in the QListWidget.
now i need to be able copy from text numbers of line to clipbord and paste them into the QListWidget
and it will add items into the QListWidget .
how can i do the copy / paste function is there any tutorial or example that show this kind of action ?
thanks
If I rightly understood your actions, you want to copy from file, to paste in global clipboard and to insert data from clipboard to QListWidget.
Then, you should read about QClipBoard http://doc.crossplatform.ru/qt/4.7.x/qclipboard.html
The fastest way (fastest for writing, not performance-wise) to read lines from a file and load them to a QListWidget is:
QFile f;
QListWidget w;
.........
w.addItems(QString(f.readAll()).split('\n'));
You can use a QRegExp instead of \n if you want catch stuff like \r\n as well.

RichEditCtrl and its text content

I copy all formatted rich edit text into clipboard as
m_edit.setsel(0,-1);
m_edit.copy();
But when I call GetClipboardData,
I obtain text without format.
Someone could explain to me something about this ?
I think you are mistaken. The copy method sends a WM_COPY message to the control. This will place formatted text as well as plain text on the clipboard. Look through all the formats on the clipboard and you will find RTF is present.