Set text direction for QLabel? - qt

Is there a way to set the text direction for a QLabel? I've got a situation in which I have QLabel objects whose text is only punctuation, and I want that to be displayed either in RTL or LTR format. (For instance, parentheses or quotation marks need to reverse depending on the text direction.) I've tried calling QLabel::setLayoutDirection, but to no effect.
#include <QApplication>
#include <QLabel>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QLabel label(" :  «");
label.setFont( QFont("Times New Roman", 72) );
label.setLayoutDirection( Qt::RightToLeft );
// label.setLayoutDirection( Qt::LeftToRight );
label.show();
return app.exec();
}
A workaround at this point is to prepend 0x202E ("Right-to-Left Override") to the string, but that's obviously a bit clunky.

label.setAlignment(Qt::AlignRight);

Related

How to insert hidden text in QLabel?

In a QLabel, I have some rich text with some superscript (e.g. kg m-3). If this text is copied and pasted in a plain text editor, the pasted text is kg m-3. I would like the pasted text to be kg m^-3. Is there any way to insert ^ as a hidden text in-between m and -3? I looked at some hidden text tags in rich text format but these don't seem to work in Qt.
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow *w = new QMainWindow;
QLabel *l = new QLabel(w);
l->setText("kg m<sup>-3</sup>");
l->setTextInteractionFlags(Qt::TextSelectableByMouse);
w->setCentralWidget(l);
w->show();
app.exec();
}
I solved this using the following line:
l->setText("kg m<html><font style="font-size:1px">^</font></html><sup>-3</sup>");
Using font-size:0px gives an error.
Without inserting the '^' character as 'hidden text' you could probably connect the signal QPlainTextEdit::textChanged() to a slot function that processes the text. To process the text i would suggest a simple RegExp.
All of this would look something like (Warning: code NOT tested):
Connect the signal to the slot function (the function to process the string has to be declared as a slot)
QObject::connect(plainTextEdit, SIGNAL(textChanged()), this, SLOT(processRichString());
Define the function for the processing as follows
void MyClass::processRichString()
{
QString strToProcess = label->toPlainText();
QRegExp rx("<sup>(.*)</sup>");
strToProcess.replace(rx, "^\\1");
label->setPlainText(strToProcess);
}

Qt - set a specific/fixed character width for QTextEdit [duplicate]

This question already has answers here:
How to align QTextEdit text
(2 answers)
Closed 3 years ago.
It's common that each character has a particular width. But I found that in Sublime Text 3, each character has the same width. Is that possible to do the same thing for QTextEdit?
Sublime Text 3:
My QTextEdit:
If you need to have all characters of the same width you should use a monospace fonts. Here is an example:
QTextEdit te;
te.setFont({ "Courier" });
te.setText("iiii\nwwww");
te.show();
Here is how you can do it.
#include <QApplication>
#include <QFrame>
#include <QHBoxLayout>
#include <QTextEdit>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto textedit = new QTextEdit;
QFont font("Monospace");
textedit->setFont(font);
textedit->setText("Hello\nWorld!");
textedit->show();
return a.exec();
}

QT : QGraphicsTextItem alignment char to top-left corner

I want to draw a character aligned to the top-left corner of parent.
QGraphicsTextItem * tItem = new QGraphicsTextItem(parent);
tItem->setPlainText("a");
tItem->setPos(QPointF(0,0));
Picture below presents output of my code (grey rectangle is parent of QGraphicsTextItem)
Result:
I want to get a result like this:
My dream result:
I tried to use Qt::AlignLeft and Qt::AlignTop but to no avail.
The solution is to use setDocumentMargin(0) to the QTextDocument, if you only put the letter "a" it seems that it is not the solution since there seems to be a vertical offset, but in reality it is not an offset but the capital letters have a higher height.
Example:
#include <QApplication>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
auto parent = scene.addRect(QRectF(0, 0, 100, 100), QPen(Qt::red), QBrush(Qt::blue));
auto * tItem = new QGraphicsTextItem(parent);
tItem->setPlainText("aAbB");
tItem->document()->setDocumentMargin(0);
view.show();
return a.exec();
}

How to continue text editing after formatting changes of QGraphicsTextItem?

I am trying to make changes (font changes) to a QGraphicsTextItem that is editable.
I am trying to change formatting of fragments of text, or the formatting applied at typing point (if I set text bold, the text I type after that action at cursor position would be bold).
Setting formatting for text fragments works - but I can't find a way to return the focus correctly to the item.
I can show the caret at the right position, but I can't type in the box unless I actually click in box (even though it seems hat I should be able to).
Simple sample (for some reason it crashes on closing program but I don't care about that since I am testing the text class, not the main program):
header: mytextitem.h
#include <QGraphicsTextItem>
class MyTextItem : public QGraphicsTextItem
{
Q_OBJECT
public:
MyTextItem();
~MyTextItem() {}
public slots:
void setItemBold(const int b);
};
mytextitem.cpp
#include "mytextitem.h"
#include <QTextCursor>
MyTextItem::MyTextItem()
{
setPlainText("ABCD");
setFont(QFont("Arial", 20));
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable);
setTextInteractionFlags(Qt::TextEditorInteraction);
}
void MyTextItem::setItemBold(const int b)
{
int _weight = (b != 0) ? QFont::Bold : QFont::Normal;
QTextCursor _cursor = textCursor();
//int p = _cursor.position(); // this won't help
QTextCharFormat _format = _cursor.charFormat();
_format.setFontWeight(_weight);
_cursor.setCharFormat(_format);
//_cursor.setPosition(p, QTextCursor::KeepAnchor); // makes no difference on allowing me to type, but I can make the cursor move
//_cursor.movePosition(QTextCursor::NoMove, QTextCursor::KeepAnchor, 0); // makes no difference but I just thought some action might
setTextCursor(_cursor);
setFocus(Qt::MouseFocusReason);
// grabKeyboard(); // does nothing
}
main.cpp
#include <QApplication>
#include <QGraphicsView>
#include <QGridLayout>
#include <QtWidgets>
#include <QCheckBox>
#include "mytextitem.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene(-20, -20, 150, 100);
QGraphicsView view(&scene);
QWidget widget;
QGridLayout layout(&widget);
layout.addWidget(&view, 0, 0);
QCheckBox bold("Bold");
layout.addWidget(&bold, 0, 1);
MyTextItem* item = new MyTextItem();
scene.addItem(item);
QObject::connect(&bold, SIGNAL(stateChanged(int)), item, SLOT(setItemBold(int)));
view.ensureVisible(scene.sceneRect());
widget.show();
return a.exec();
}
Editing the item is possible only if clicking in the box.
Assuming that I am already in the box (editing), and I push the "Bold" checkbox, I expect to be able to continue editing - type in the box - but even though I try to
set focus (which places the blinking text cursor in the box),
set position for cursor (I can move it, or select things... that works but I want to keep current position and selection)
grab keyboard - seems to do nothing
nothing seems to return me to the box so I continue typing (with the new font setting).
How can I get the QTextCursor or anything else to allow me to keep editing the text ?
You need to focus on QGraphicsView after format change. You can't focus on QGraphicsTextItem because it isn't QWidget.

Qt: QMenu with translucent background

I use Windows and I want to set a style sheet to a QMenu to give it a translucent background. In order for that to work, I first set the FramelessWindowHint, then I set the WA_TranslucentBackground attribute. Then I set my style sheet and display the menu with the popup method. It is drawn correctly, but it behaves strangely: As soon as it has the FramelessWindowHint, it is always visible (even before calling the popup() method). It does not hide itself anymore after one of its entries has been clicked.
Here is a minimalistic example:
#include <QApplication>
#include <QMenu>
#include <QPoint>
#include <QCursor>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMenu menu;
menu.addAction("about", &a, SLOT(aboutQt()));
menu.addAction("exit", &a, SLOT(quit()));
menu.setWindowFlags(Qt::FramelessWindowHint);
menu.setAttribute(Qt::WA_TranslucentBackground);
menu.setStyleSheet("QMenu{background:rgba(255, 0, 0, 50%);}");
menu.popup(QCursor::pos());
return a.exec();
}
menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);
should solve your problem. Now you are clearing all flags already set by Qt.

Resources