QPushbutton: Two different Font size - c++

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.

Related

QPlainTextEdit set font for only one line

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)

Drag Text from an item of QListWidget to QLabel

I'm looking for the simpliest way, using Qt Designer and a little code, to drag text from a QListWidget (apparently there are options of draggable content in Qt Designer) to a QLabel (but I can't find any droppable action option ...) so its text is set to the text of the item we dragged.
Any ideas ?
[C++, Windows, Qt5]
Well, yeah, the most straightforward idea is subclassing QLabel and reimplementing it's
void dropEvent(QDropEvent* event)
QDrag object will help you pass text from QListWidget via it's mimeData, just create one in mouse event handler.
Check similar questions or examples for more information.

Putting a line on a QPushButton

I am creating a program that draws graphs; I want to be able to have a button that a user can press to choose the line color and style. I want to be able to visually show what the current selection is.
Currently, I know that I can do that using two seperate widgets, a QPush button, and then a widget I make myself that just draws a line across it using QPen.
I would like to turn these two widgets into a single widget. I want this widget to be a pushable "button" that the user presses and I can get a signal out of to run a routine that sets a new QPen.
Is this functionality built in? Or would I need to create a new widget that re-implements either QPushButton or QActionButton? Or should I just make my widget which listens to mouseclick events on it and create a signal slot from there?
You could use a QLabel, set the style sheet, and use that as your line on the graph. You could use the bounds of a QGroupBox to define x and y axis.
Maybe something like this:
yourQLabel.setStyleSheet("QWidget {background-color: rgb(255, 0, 0); color:white; border: 1px solid black;}"); // red with black border
Then you can set the height, width and position of the QLabel based on values for your graph. Of course this will only work if lines on your graphs are rectangular. If they aren't, then you'll probably have to use something other than QLabel.
Use the setGeometry method of QLabel to set this.
yourQLabel.setGeometry(x, y, width, height);
As far as your button, you can do something similar with that if you want. A QPushButton also has the setGeometry and setStyleSheet methods.
If you want to tell your button to do something on click, assuming you have a QPushButton object called myButton, add this to your header file (.h):
class YourClass
{
Q_OBJECT
//...
private slots:
void on_myButton_clicked();
}
Then in your source file (.cpp):
void YourClass::on_myButton_clicked()
{
// stuff your button does on click
}

QLineEdit extended popup

Idea for widget:
By default the widget is basically a QLineEdit where the user can input text. The QLineEdit can obviously only display a certain amount of character (limit). The idea would be that once the mentioned limit is reached, the widget should be extended with a popup window which is overlayed other GUI controls (like QComboBox poup window). This popup window would then contain the text which the "QLineEdit" could not show. The poup window size would need to be dynamic to handle multiple lines. When the widget loses focus it will show the QLineEdit box and part of the original text.
What would be a good design for this widget? Would it be possible to extend an existing widget or combine multiple widgets or ?
Tried looking at the QComboBox source code but it is rather complicated.
Update:

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.