How can I display unicode in QGraphicsTextItem? [duplicate] - qt

This question already has answers here:
How to specify a unicode character using QString?
(4 answers)
Closed 7 years ago.
I would like to be able to display Unicode in QGraphicsTextItem (or a subclass of it).
The only way to set text in QGraphicsTextItem seems to be
setPlainText(text);
Trying
setPlainText(QString::fromUtf8("Caf\x00e9 Frap\x00e9"));
or
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Café Frapé");
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Caf\x00e9 Frap\x00e9");
I get:
Caf? Frap?
It seems that no matter what I do (which I am not sure is correct) I do not get the output right...
Do QGraphicsTextItem support unicode ? Is maybe the setPlainText function at fault - but then what are the alternatives ? (I looked into setDocument but it also sets plain text...)
Edit - copying the special characters inside the QGraphicsTextItem works, once on screen, but still unable to place any unicode from code.

In a class inheriting QGraphicScene, I used:
QString text(QString::fromUtf8(xt.text));
...
QGraphicsTextItem *t = addText(text = text.replace("\\n", "\n"), font);
The dot source is utf8:
digraph so {
Café -> Frapé
}
And the rendering:
You can find here the C++ code.

I think you should use the
QGraphicsTextItem item.
item.setHtml( "Café Frapé" );
function instead of the mentioned. Read this QGraphicsTextItem::setHtml.

Related

How to get a superscript in QTableWidget header?

I found out from this question that If I want to have superscript in labels I can use this solution:
µm<sup>2</sup>
which means that I can use HTML tags and it works well.
but when I tried this way in QTableWidget, it didn't work.
This is its result:
I try other HTML tags which work in QLabel but none of them work for QTableWidgetItem
This is the solution for having superscript in the QTableWidget header :
We should use only Unicode characters for example:
QTableWidgetItem *___qtablewidgetitem = tableWidget->horizontalHeaderItem(0);
___qtablewidgetitem->setText(QCoreApplication::translate("MainWindow", "x\302\262", nullptr));
QTableWidgetItem *___qtablewidgetitem1 = tableWidget->horizontalHeaderItem(1);
___qtablewidgetitem1->setText(QCoreApplication::translate("MainWindow", "x\302\263", nullptr));
QTableWidgetItem *___qtablewidgetitem2 = tableWidget->horizontalHeaderItem(2);
___qtablewidgetitem2->setText(QCoreApplication::translate("MainWindow", "x\342\202\211", nullptr));
In the above example, I use "x\302\262" to see x².
This will be the result
Because it may be difficult to know these numbers, I used an online keyboard.
I wrote the content I wanted to see in the header here and after that I use Ctrl+C in Table's header UI.
then Qt will generate Unicode forms in "ui_mainwindow.h" file.
As I mentioned characters should be Unicode so for example I test typing my text in LibreOffice and Ctrl+C x² from there but it didn't understand and show x2 in the header.

How to use TextLayout without deprecated impl_getNativeFont method [duplicate]

This question already has answers here:
How to calculate the pixel width of a String in JavaFX?
(4 answers)
Closed 5 years ago.
I need to know the size of a text using a textlayout object.
I found the following bunch of codes
final TextLayout LAYOUT = Toolkit.getToolkit().getTextLayoutFactory().createLayout();
LAYOUT.setContent(text != null ? text : "", font.impl_getNativeFont());
LAYOUT.setLineSpacing(1.0f);
LAYOUT.setWrapWidth(100.0f);
LAYOUT.setBoundsType(TextLayout.BOUNDS_CENTER);
return LAYOUT.getBounds().getHeight();
The code is working properly except I have a warning message concerning getNativeFont which seems to be deprecated, knowing that what I need is the height of the text
So my question : What is the appropriate method ?
Thanks in advance !
Thank you,
It is working for me:
Text theText = new Text(theLabel.getText());
theText.setFont(theLabel.getFont());
double width = theText.getBoundsInLocal().getWidth();

Multiline title on a JavaFx Chart

Is there some means - short of extending the JavaFX charting base class(es) - to specify a multi-line title string?
As seen in the screenshot the title is the only element 'wanting' more space.
I had seen some references to using a newline '\n' and even a pipe character '|' in the string but they apparently do not work for the title.
I just threw this in a sample I had and it worked.
chart.setTitle("really long title...........\n.............and some more ");
Label l = (Label)chart.lookup(".chart-title");
l.setWrapText(true);
The \n sets the break point if I don't want it at the limit.
As you can see it's just a Label, the hard part is getting it.
You can also use a css file with the selector. I think it's already centered.
.chart-title{
-fx-wrap-text : true;
-fx-alignment : center;
}

qt qstring toStdString loss of minus sign

I am using a qstring and using the function toStdString(). When I do this I lose a minus sign:
'332-_09I_W'
this text becomes:
'332_09I_W'
What can I do to prevent this?
EDIT: Actually, the problem is not when i use toStdString(), it is when I set the text in my qTextEdit. The change occurs here:
myTextEdit->setHtml(myString);
I've tried:
QString qs("332-_091_W");
std::string st = qs.toStdString();
ui->textEdit->setHtml(st.c_str());
It gives no problem to me. Which version of Qt are you using?
However, from the documentation:
setHtml() changes the text of the text edit. Any previous text is removed
and the undo/redo history is cleared. The input text is
interpreted as rich text in html format.
Note: It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created
and passed to setHtml().
The minus/hyphen symbol is ambiguous in HTML, try to change it in the QString (before passing it to setHtml()) with −

Does QString::fromUtf8 automatically reverse a Hebrew string?

I am having a problem where a Hebrew string is being displayed in reverse. I use QTableWidget to display some info, and here the string appears correctly using:
CString hebrewStr; hebrewStr.ToUTF8();
QString s = QString::fromUtf8( hebrewStr );
In another part of my program this same string is displayed on the screen, but not using QT, and this is what is being shown in reverse:
CString hebrewStr;
hebrewStr.ToUTF8();
I have debugged and hebrewStr.ToUTF8() in both cases produces the exact same unicode string, but the string is only displayed correctly in the QTableWidget. So I am wondering if Qt automatically reverses a given Hebrew string (since it is a rigth-to-left language). Thanks!
Yes, in this case QString generate the full unicode wchar_t from the UTF-8 encoded string. If you would like to do similar thing in MFC, you should use CStringW and decode the string.
Use MultiByteToWideChar for UTF8 to CStringW conversion.
Connected question in StackOverflow.

Resources