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.
Related
I have several Word documents and I need the text out of them in my programm, to make my specific pdf file out of it. At the moment i have following code which allows me to show the word document, but i need the plain text.
QAxWidget* WordDocument=new QAxWidget ("Word.Document", this-> ui-> textEdit);
WordDocument-> setGeometry (QRect (10, 10, 621, 471));
WordDocument->setControl ("C:\\Users\\user6\\Desktop\\Optimierung des Verteilerbaus.docx");
WordDocument-> show ();
How can I get the text out of it?
I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.
What I think I need is a strategy that relies on Qt's library to set a background color between these two indices. I began looking a QSyntaxHighlighter; however I think that this is more for highlighting using a predefined set of laws and not for just grabbing up anything between a & b and setting the background color. If I can use syntax highlighter please provide me with and example or reference as I have already read through the documentation and didn't find anything.
Thanks for any help in advance!
P.S. Just to emphasize on the html compatibility issues; html becomes problematic due to multiple < and > signs used.
You can use QTextCursor and QTextCharFormat for it:
QTextEdit *edit = new QTextEdit;
...
int begin = ...
int end = ...
...
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(edit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);
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
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.
When i try save rave project in pdf\html file, have incorrect encoding.
When choose format and press SAVE, it ussually save in iso-8859-1 code.
But i need cp1251 (cyrillic).
For example "Ïëîùàäü" instead of "Площадь".
I would guess that the best solution to your problem would be to use Unicode, rather than a codepage such as CP1251. Is it possible to use Unicode with Rave Reports?
I have the same problem when I want o save reprt to pdf format. I have to create TRvRenderPDF and set it as RenderObject but pdf file was viewed not correctrly.
The TRvRenderPDF component not unicode-compatible(that is very bad) that is why all text in report coverted into Ansi with active codepage(for cyrillic it is CP1251). Now we have pdf file with text in CP1251 encoding.
As default TRvRenderPDF generate pdf with font TYPE1 Helvetica(by the standart of the format pdf it is build-in). But text is interpreted with encoding ISO 8859-1(or CP1252) but it encoding was CP1251 tha is why we have "Ïëîùàäü" or something analogous.
What we can to do:
Get font TYPE1(CP1252) where service symbols(numbers like in cyrillic letters in CP1251) replaced with cyrillic glyphs. For example a link and we need to install it.
Now we need tor replace old font name(Helvetica) from generated pdf document with new font name(AGHelvetica). You can dow it with text editor or i your's programm(read file -> find -> replace -> save file).
That all situation.
P.S. Sorry for my english.
P.P.S. If set property of pdf render EmbedBaseFonts = true, pdf document were saved with TrueType fonts, but problem stay. Neew to see to unicode render? but not this one.