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();
}
Related
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);
}
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();
}
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);
I have a QMenu with a translucent background and rounded edges (border-radius). Unfortunately, Windows 7 draws a drop shadow for this menu, which does not fit to the rounded edges. Its the shadow that would be drawn for normal rectangular menues.
Is there either
- a way to completely disable drawing drop shadows for QMenu
or
- a way to make the shadow fit to the rounded edges
?
Here is a minimalistic example where it occurs:
#include <QApplication>
#include <QPushButton>
#include <QMenu>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QPushButton b("press me");
QMenu m;
m.addAction("hello"); m.addAction("world");
m.setWindowFlags(m.windowFlags() | Qt::FramelessWindowHint);
m.setAttribute(Qt::WA_TranslucentBackground);
m.setStyleSheet("background:rgba(255,0,0,50%); border-radius:5px;");
b.setMenu(&m);
b.show();
return a.exec();
}
This should do it:
w.setWindowFlags(w.windowFlags() | Qt::NoDropShadowWindowHint);
We get our application full screen alright, but the OSX Menu bar is still shown above it...
Is there any way around this problem?
Yes, might be a bug as already suggested. Try with FramelessWindowHint, like this:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label("Test!");
label.setWindowFlags(Qt::FramelessWindowHint);
label.showFullScreen();
return a.exec();
}
I tested it on Mac OS X 10.7.1 (which is not even supported) and seems to work.