How to highlight a string of text within a QTextEdit - c++

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);

Related

How to translate variable text in Qt?

I have a difficult problem and tried too many things but in vain.
I have 4 translation files for 4 different languages, and whenever the user changes the language, I need to re-translate the UI.
Fixed text can be re-translated as follows:
ui->BusinessNameHelpText->setText(tr("Enter the business name."));
However, the variable text like the action on a button can't be done this way, because it's either “Install” or “Update” for example.
I tried this code block:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(trUtf8(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toStdString.c_str()));
This works for only the first time when I change language, but after that, it doesn't work.
It sounds like you have code like
ui->actionButton->setText(isInstall ? tr("Install") : tr("Update"));
in your program. You need to run that again, in response to the language change event.
Ok now I get it what is actually your problem.
Problem is how you treat change text of button.
It works only when you change English to other langue. It happens since English text is equivalent to text you have used in code as identifies of translation.
When your application calls this:
QString action = ui->actionButton->text();
if previously English was selected action will contain text which is same as identifier of translation.
if previously other langue was selected action will contain something else then identifier and translator is unable to find matching translation. In such case translator will return original value.
To fix just leave only ui->retranslateUi(this); and thrash other code and make sure your UI has all respective strings marked that they need translation.
I've found a post on Qt Forum which shows a problem like mine, and this answer says that it's impossible to translate dynamic text.

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.

New Symbolic Color in Pango Span Text

First time poster; long time admirer of the Stack Overflow angels.
I'm having an issue with colors in span text that are controlled by Pango.
Long Version:
I'm updating an old UI program which has C++ code guts with GTK, XML (written by Glade), and an RC stylesheet handling the graphics. Some of our colored markup text is hard-coded in the XML. Some of it is dynamically set in the C++ code.
The problem is, that when the program runs on our older systems, the color referenced by span text as 'green' shows up as #00FF00. On our newer systems, 'green' is showing up as #008000.
Example of code printing to a label widget:
gtk_label_set_markup((GtkLabel *) TitleBarLabel, "<span color='green'>Orbital Cannon Positioning</span>");
I'm fairly certain that Pango is in control of the span text markup. I found that the difference between the greens is exactly the difference between X11 and W3C color lists (https://en.wikipedia.org/wiki/X11_color_names#Clashes_between_web_and_X11_colors).
It seems that our old systems are using X11 and our new ones are using W3C, which makes sense.
I could just replace all instances of 'green' with '#00FF00' but if we wanted to change the colors in the future, we'd have to go through the whole thing again. I'd much rather have the colors changeable through a stylesheet instead of baked into the code.
C++ Code:
GtkWidget * TitleBarLabel;
TitleBarLabel = GTK_WIDGET (get_builder_object (builder, "TitleBarLabel"));
gtk_label_set_markup((GtkLabel *) TitleBarLabel, "<span color='#00FF00'>Death Ray Power Status</span>");
I can create a GdkColor at run-time and gdk_color_parse it with values from a config file, and then use gtk_widget_modify_text() to apply the color to the label widgets. But then that doesn't work for all of the hard-coded span text in the XML. Also, we have pleanty of labels with bits of text colored differently inside the same line.
C++ Code:
GdkColor pass_color;
gdk_color_parse("#00FF00", &pass_color);
gtk_widget_modify_text(TitleBarLabel, GTK_STATE_NORMAL, &pass_color);
I can make a style in my RC file for each color and link every single label that would use that color at run-time. But we'd have to remove all markup coloring and add lots of code for grabbing widgets that we never bothered with before and code for setting names of widgets instead of just printing to them with new span text. It gets the desired result of having the colors changeable in a stylesheet but it's a massive undertaking and it's not intuitive for our veteran engineers who are used to using the color attributes.
RC File:
style "pass_color"
{
fg[NORMAL] = #00FF00
}
widget "*TitleBarLabel_Pass" style "pass_color"
C++ Code:
gtk_widget_set_name(TitleBarLabel, "TitleBarLabel_Pass");
Short Version:
Ideally, I would like to be able to make a new color at run-time that we can link with span text in such faction:
<span color='MyNewColor'>Weather Manipulation Settings</span>
Or maybe even create a new tag that applies specific attributes, like:
<span><MyNewColor>Shark Tank pH Balance</MyNewColor></span>
But I doubt that's possible.
I tried playing around with pango_attr_type_register(), pango_attr_foreground_new(), and friends, but I couldn't figure out how attributes work of if they could even do what I thought they did. After much research, it looks like an 'attribute' is just a one-time setting on a single string of text. And not a new value that can be called in line with span text, as I hoped.
Is anything like this remotely possible without rebuilding all of Pango?
Is there a different work around that would get me a stylesheet like setup?
At this point, I'm open to suggestions.
Version Specs:
Computers showing green as #00FF00
OS: Linux Slackware 13.37 and below
GTK: 2.24.4
Pango: 1.28.4
Computers showing green as #008000
OS: Linux Slackware 14.1
GTK: 2.24.20
Pango: 1.34.1
If you are able to use GTK 3.x, I would suggest doing that, where this is much easier to do using CSS. There is even a way to use multiple CSS styles for different regions in the same label, though it is awkward.
In GTK 2, as you noted, you can reference widgets by their name property in your RC file:
widget "shark-tank-ph-label" style "green-text"
style "green-text" {
text[NORMAL] = #008000
}
I would recommend taking this approach even if it's not what you're used to. Refactoring once to remove the hardcoded colors from your labels will make it much easier the next time you have to change something like this, and will also make your code closer to how things would work in GTK 3.x should you decide to make a port in the future.

How to load qt linguist dynamically changed label text

In my project i'm trying to use qt linguist. When i change the language from English to Turkish, it is working all constant label.
But some labels i m loading them dynamically according to scenario of use cases.
Whatever i do with qt linguist, it doest workthe texts of these label.
How can i get rid of this problem?
Any help will be apprecialted
Qt has a guide to internationalization, which includes the basic information: to wrap your string in a tr function call.
label->setText( tr( "Hello, World!" ) );
In addition to this, if you want the language to change on the fly, you'll need to identify when the context has changed, and update your labels appropriately. Unfortunately, I can't easily put my hands on the signal that indicates when to do so.

Setting span html element text in C++

I am writing firefox extension using C++.
I want to set text of the span html element.
In Javascript, I used 'textcontent' javascript property to set span element text.
How can I do it in C++?
I found nsIDOMHTMLElement interface & its child interfaces.
They seem useful.
I am not getting the way in which I will use nsIDOMHTMLElement interface & its child interfaces to set span element text.
Please suggest me the way!
Thanks,
Vaibhav.
cross-posted from http://groups.google.com/group/mozilla.dev.extensions/browse_frm/thread/7d96ca2fe4c2bd5a#