How to prevent QLabel from unnecessary word portability?

I need to display text with QLabel with the following requirements:

  • Word wrapper
  • Expand from small width to full width according to the length of the text, while the label takes up one line
  • Always full width while label takes several lines
  • Label background filled with color

I tried putting QLabel with sizePolicy (Preferred, Preferred) and QSpacerItem with sizePolicy (Expanding, Minimum) in a QHBoxLayout.

layout

And I expect the text to not be wrapped before the right side appears.

expect

But I realized that the text is wrapped before the right side appears.

result

How to prevent this unnecessary word wrap?

Reproduction Code

Note

  • spacer HBox, 1 3, 2 (, , . ).
  • , 2, 1.

enter image description here enter image description here

, .

+4
1
  • , wordWrap , . , , QLabel. .

. false, , . , true.

  1. 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
    
  2. mylabel.cpp

    #include "mylabel.h"
    
    MyLabel::MyLabel():QLabel()
    {
    }
    
    MyLabel::~MyLabel()
    {
    }
    
    void MyLabel::resizeEvent(QResizeEvent *)
    {
        emit labelSizeChange();
    }
    
  3. 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
    
  4. 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);
        }
    
  5. GUI: .

    enter image description here

+2

Source: https://habr.com/ru/post/1598844/


All Articles