I need to display text with a QLabel with following requirements:
I tried putting QLabel with sizePolicy (Preferred, Preferred) and QSpacerItem with sizePolicy (Expanding, Minimum) into QHBoxLayout.
And I expect that the text is not wrapped before reaches right side.
But I got that the text is wrapped before reaches right side.
How to prevent this unnecessary word-wrapping?
This question is about the similar problem, but there is no layout.
wordWrap
of the label only when needed. To trigger the size change of the label you can make a custom label by replementing from QLabel
. Below is an example. When a text is added to the label. Initially with word wrap false, It will expand until it reaches the frame size. If it crosses the frame size, the word wrap is set to true.
mylabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel : public QLabel
{
Q_OBJECT
public:
explicit MyLabel();
~MyLabel();
signals:
void labelSizeChange();
protected slots:
void resizeEvent(QResizeEvent *);
};
#endif // MYLABEL_H
mylabel.cpp
#include "mylabel.h"
MyLabel::MyLabel():QLabel()
{
}
MyLabel::~MyLabel()
{
}
void MyLabel::resizeEvent(QResizeEvent *)
{
emit labelSizeChange();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <mylabel.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void lableSettings();
void on_pbShort_clicked();
void on_pbMedium_clicked();
void on_pbLong_clicked();
void addTextToLabel(QString text);
private:
Ui::MainWindow *ui;
MyLabel myLabel;
QString lorem;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->horizontalLayout->addWidget(&myLabel);
ui->horizontalLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding));
myLabel.setStyleSheet("Background-color:black;color:white");
myLabel.setWordWrap(false);
myLabel.setMinimumWidth(0);
myLabel.setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(&myLabel,SIGNAL(labelSizeChange()),this,SLOT(lableSettings()));
lorem ="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addTextToLabel(QString text)
{
myLabel.setWordWrap(false);
myLabel.setMinimumWidth(0);
myLabel.setText(text);
}
void MainWindow::lableSettings()
{
if(myLabel.width()> ui->frame->width()-20)
{
myLabel.setWordWrap(true);
myLabel.setMinimumWidth(ui->frame->width()-20);
// Note the value 20 depends on the layout spacing
//between the Horizontal layout and the frame.
//If this value is less. The whole windo will start resizing.
}
}
void MainWindow::on_pbShort_clicked()
{
addTextToLabel(lorem.left(15));
}
void MainWindow::on_pbMedium_clicked()
{
addTextToLabel(lorem.left(150));
}
void MainWindow::on_pbLong_clicked()
{
addTextToLabel(lorem);
}
GUI layout : HorizontalLayout inside a frame.
I've encountered same problem when making a chat app. Inspired by techneaz's answer, I found that using QFontMetrics is a cleaner way to compute the text width.
So if the computed text width plus some padding is less than the disired max width, set the label's fixed width to "the computed text width plus some padding", otherwise set it to the disired max width
Here's my code in pyqt:
class TextBubbleView(BubbleView):
PADDING = 18
MAX_WIDTH = 400
def __init__(self, msg: Message):
super().__init__(msg)
self.setWordWrap(True)
fm = QFontMetrics(self.font())
width = fm.width(msg.content) + TextBubbleView.PADDING
if width < TextBubbleView.MAX_WIDTH:
self.setFixedWidth(width)
else:
self.setFixedWidth(TextBubbleView.MAX_WIDTH)
self.setText(msg.content)
BubbleView is just a subclass of QLabel.
before
after
Sorry about my bad english.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With