QPlainTextEdit set font for only one line - c++

Is that possible to display text with different fonts with QPlainTextEdit?
i've tried this, but it seems that font changes for a moment for all widget and the returns to normal:
QFont font;
font.setBold(true);
ui->plainTextEdit->setFont(font);
ui->plainTextEdit->insertPlainText("Some text:\n");
font.setBold(false);
ui->plainTextEdit->setFont(font);
I've tried to change QPlainTextEdit to QTextEdit it didn't helped

QPlainTextEdit and QTextEdit both inherit setFont from QWidget, and a QWidget only has one font type at a time.
However, QTextEdit exposes an interface to set the font for different parts of the text via QTextCharFormat
In your case, the easiest way to fix this should be to use a QTextEdit, and the setCurrentFont method.
QFont font;
font.setBold(true);
ui->textEdit->setCurrentFont(font);
ui->textEdit->insertPlainText("Some text:\n");
font.setBold(false);
ui->textEdit->setCurrentFont(font);
(In this very specific case, you could also use setFontWeight to select bold/normal font, but for more general modifications setCurrentFont is more appropriate)

Related

QPushbutton: Two different Font size

Is it possible to have two different Font size for the QPushButton?
For example, for the text "LIVE VIDEO' in a QPushButton, I want to have the font-size 16 for 'LIVE' and 12 for 'VIDEO'.
Derive from QPushButton and draw the text yourself. You can refer this post for reference.
Two colours text in QPushButton
While what #abhilb suggests is possible I would go with a custom QLabel that is clickable, which is faster to implement.
Unlike QPushButton QLabel supports rich text formatting. If you set the text formatting to Qt::RichText you can put HTML inside meaning you can use <font/>, <b/> etc.
myLabel.setTextFormat(Qt::RichText);
myLabel.setText("<font size='16'>LIVE</font><font size='12'/>VIDEO</font>");
I've added this formatting to a label in a widget of mine and you can see the results:
You just need to handle the void mouseReleaseEvent(QMouseEvent* event); or void mousePressEvent(QMouseEvent* event); in order to make it properly clickable. The final touch would be to emit your own clicked() signal.

QPushButton with Image not getting highlighted when focused (MAC)

I have a QPushButton with image, i have set focus policy to strong focus but, while focusing that QPushButton it is not getting highlighted(no dot frame or default blue border)
You should take a look at the QPalette(in Qt Designer) of your QPushButton and in particular, the HighLight color which is supposed to handle this effect. Moreover, make sure you didn't define focusPolicy in the stylesheet of a parent of that QPushButton, as that could cause problems, because QPushButton would inherit that property.

Qt QGroupBox border

Im working on a little program that require a QGroupBox that have inside a QLineEdit. I want to make "invisible" the border of the QGroupBox using:
groupBoxName->setStyleSheet("border:0;");
The problem is that even the QLineEdit inside of it inherit this style.
How can I make invisible the QGroupBox border but not the QLineEdit border?
Thanks
Give an object name for the groupbox using setObjectName() function,
group_box->setObjectName("MyBox");
Then you could style it as a css object.
group_box->setStyleSheet("#MyBox{border:0
This will only affect the #MyBox

How to get font of widget in Qt set by stylesheet?

I have Qt application with custom stylesheet applied to it (and for all widgets in general) with custom font included in this stylesheet. But when try to get font of some widget font() method return different font. I want to get the font of a QWidget which is set by a stylesheet. The font() method always returns the global system font or the font set by setFont(), but not the font which is set by setStyleSheet() and is used for painting in the widget. I need the font to do some calculations based on the font size. I use Qt 4.6. How can I get real font of widget (that is showing when application run) set by stylesheet?
After some investigations I saw that if I apply defined stylesheet to some widget I can get proper font information (defined by stylesheet) with myWidget->font() method. Also when I set stylesheet to whole MainWindow I can get proper font information with font() method for all the widgets that MainWindow contains. But, when I set stylesheet to instance of QApplication the font() method for all the widgets return default font or the font previously set by setFont(). Why so?
You can retrieve a font of a specific widget reading it's properties, as the following:
//Get pushbutton font.
QFont font = ui->pushButton->property("font").value<QFont>();
qDebug() << font.family() << font.pointSize();
//Get MainWindow font.
QFont font2 = property("font").value<QFont>();
qDebug() << font2.family() << font2.pointSize();
To load values from Qt Stylesheet you should call this methods:
widget->style()->unpolish(widget);
widget->style()->polish(widget);
widget->update();
After this all values of your widget will be updated according your stylesheet values specified.
The best I can tell from QStyleSheetStyle::updateStyleSheetFont, the widget always contains the resolved font from the stylesheet. I'd expect QWidget::font() to return the resolved font that you've set using the stylesheet - i.e. the font that is the merged application font, any parent widget fonts, and the stylesheet font.
The widget must be polished first, of course, unless you're querying after the events have been delivered (i.e. from within the event loop).
// https://github.com/KubaO/stackoverflown/tree/master/questions/style-font-query-test-45422885
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QLabel label("Test");
auto font1 = label.font();
label.setStyleSheet("font-size: 49pt;");
label.show();
label.ensurePolished();
auto font2 = label.font();
Q_ASSERT(font1.pointSize() != 49);
Q_ASSERT(font2.pointSize() == 49);
Q_ASSERT(font1.family() == font2.family());
}

Make QLabel text selectable?

I have a QLabel in my application that displays error messages to the user. I would like to make the text of the label selectable so users can copy and paste the error message if needed.
However, when I use the mouse to click and drag over the text, nothing happens - the text is not selected.
How can I make the text within a QLabel selectable by the mouse?
Code
The text of a QLabel can be made selectable by mouse like so:
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
This is found in the QLabel documentation.
You can use that same function to make links selectable by keyboard, highlight URL links, and make the text editable. See Qt::TextInteractionFlag.
Designer
Search for textInteractionFlags under the QLabel menu and set the flag TextSelectableByMouse.
Here is another method, for reference...
You could create a QLineEdit subclass instead, tweaked to look and act like a QLabel,
in the constructor:
setReadOnly(true);
setFrame(false);
QPalette palette = this->palette();
palette.setColor(QPalette::Base, palette.color(QPalette::Background));
setPalette(palette);
I think the accepted answer is simpler and preferable to this though.