gpt4 book ai didi

qt - 如何防止 QLabel 进行不必要的自动换行?

转载 作者:行者123 更新时间:2023-12-01 03:40:08 29 4
gpt4 key购买 nike

我需要使用具有以下要求的 QLabel 显示文本:

  • 自动换行
  • 根据文本的长度从小宽扩展到全宽,而标签占一行
  • 标签采用多行时始终为全宽
  • 标签的背景填充了一些颜色

  • 我尝试将带有 sizePolicy(首选,首选)的 QLabel 和带有 sizePolicy(扩展,最小)的 QSpacerItem 放入 QHBoxLayout。
    layout
    我希望文本是 不是 在到达右侧之前包裹。
    expect
    但是我知道文本在到达右侧之前就被包装了。
    result
    如何防止这种不必要的自动换行?
    复制代码
  • Here

  • 笔记
  • 如果我不把spacer放在HBox中,那么满足要求1和3,但满足2(文本不存在的区域用背景颜色填充。此时,我不希望这种行为)。
  • 如果我禁用自动换行,则满足要求 2,但满足 1。

  • enter image description here
    enter image description here
    相关问题
  • Why does QLabel prematurely wrap?

  • 这个问题是关于类似的问题,但没有布局。

    最佳答案

  • 这可以通过设置 wordWrap 来解决。仅在需要时才使用标签。要触发标签的大小更改,您可以通过从 QLabel 补充来制作自定义标签。 .下面是一个例子。

  • 将文本添加到标签时。最初使用自动换行假,它将扩展直到达到帧大小。如果超过帧大小,则自动换行设置为 true。
  • 我的标签.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
  • 我的标签文件
    #include "mylabel.h"

    MyLabel::MyLabel():QLabel()
    {
    }

    MyLabel::~MyLabel()
    {
    }

    void MyLabel::resizeEvent(QResizeEvent *)
    {
    emit labelSizeChange();
    }
  • 主窗口.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
  • 主窗口.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 布局:框架内的 Horizo​​ntalLayout。

    enter image description here
  • 关于qt - 如何防止 QLabel 进行不必要的自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535143/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com