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.
Related
I am trying to elide rich text (with html type links) using Qt and Pyside. The text strings will be something like this:
u"<a href='FRIEND' style=\"text-decoration: none\">" \
u"<font color=#1abc9c>{}</font></a> agregó <a href='LINK' " \
u"style=\"text-decoration: none\"><font color=#1abc9c>{}</font></a>" \
u" a su biblioteca".format(EntityMocks.friendMock.display_name,
EntityMocks.assetMock1.title)
The documentation of Qt explicitly states that this cannot be done using rich text so I am stripping my text of all the html tags before passing it to Qt´s elider. I am also trying to accomplish this for texts that will elide on multiple lines. This is my test code for just two lines:
class DoubleElidedText(QLabel):
def __init__(self, *args):
super(DoubleElidedText, self).__init__(*args)
def setText(self, text):
self.setToolTip(text)
self.update()
metrics = QFontMetrics(self.font())
elide = metrics.elidedText(strip_tags(text), Qt.ElideRight, self.width()*2 - self.width()/5)
if metrics.width(elide) > self.width():
self.setMinimumHeight(metrics.height()*2)
else:
self.setMinimumHeight(metrics.height())
texto = u"{}".format(elide)
super(DoubleElidedText, self).setText(texto)
class MLStripper(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
This behavior (stripping the html tags) has become unacceptable for my software.
I am trying to develop a way to elide the rich text that will be displayed in a QLabel with multiple lines. Doing this manually raises one particular issue that I am not being able to solve:
What is the length of the displayed lines? (It will depend on the white space added at the end of every line).
Am I addressing this issue correctly or is there some QtMagic that is missing in my research?
Not a Python solution but I assume that my approach from https://stackoverflow.com/a/66412942/3907364 can be ported to Python very easily.
In short: Use a QTextDocument to parse and represent the rich text and then use a QTextCursor to keep removing characters from it until it has the desired width.
Note this does not specifically deal with multiline Strings (in fact my answer focuses on single-line Strings), but I would assume extending it to multiline input should be doable.
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;
}
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.
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 −
I want to show large numbers separated by comma e.g. 123,000 not as 123000 but doing this i have to make it into a string on which the sorting is not working as desired.
Yes you have to convert it to a string. Use locale in order to display numbers based on user settings.
this->locale()->toString(value);
It's a bit easier if you move to a QTableView and QStandardItemModel, rather than a QTableWidget - don't know if this is suitable for you.
Then you can specify a SortRole which is distinct from the DisplayRole. Like this (hope the Python is readable - I use PySide)
tableView = QtGui.QTableView(self)
tableView.setSortingEnabled(True)
model = QtGui.QStandardItemModel(3,2,self)
model.setSortRole(QtCore.Qt.UserRole)
for r in range(3):
for c in range(2):
item = QtGui.QStandardItem(["1,000","200","100"][r])
item.setData(QtCore.Qt.UserRole,[1000,200,100][r])
model.setItem(r,c,item)
tableView.setModel(model)
Try commenting out the setSortRole line to see the two behaviours.