Qt application development - the use of QLabel

Table of contents

1. Constructor

2. Properties and methods

3. Example 1 display content and setting style

4. Example 2 Use html to process hyperlinks

5. Example 3 display image

6. Example 4 displays GIF

7. Example 5 Partner Mechanism


        QLabel inherits from QFrame. As the most basic and commonly used control of Qt Widget, it can be used to do many things in actual development.

        QLabel does not provide user interaction functions. The visual appearance of the label can be configured in a variety of ways, and it can be used as a label for other interfaces. QLabel can be used to display the following:

  • plain text: plain text data
  • Rich text: Also known as formatted text, it has information such as style and typesetting, such as color, style (bold, italic, etc.), font size, characteristics (such as hyperlinks), etc.
  • display image information
  • show gif file
  • Directly display a double or int type number
  • Nothing can be displayed

1. Constructor

        QLabel supports two construction methods, the difference is whether to add initial content by default

QLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
QLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())

2. Properties and methods

The text attribute represents the content of the current label

QString text() const                //当前内容
void setText(const QString &)       //设置内容

The alignment attribute represents the horizontal and vertical alignment of the text, and the alignment in both directions can be specified at the same time. The default alignment is horizontal left and vertical center.

Qt::Alignment alignment() const;        // 获取对齐方式 
void setAlignment(Qt::Alignment align); // 设置对齐方式 
水平标志
Qt::AlignLeft:         //水平靠左。
Qt::AlignRight:        //水平靠右。
Qt::AlignHCenter:      //水平居中。
Qt::AlignJustify:      //水平调整间距两端对齐。
垂直标志
Qt::AlignTop:          //垂直靠上。
Qt::AlignBottom:       //垂直靠下。
Qt::AlignVCenter:      //垂直居中。
Qt::AlignBaseline:     //垂直与基线对齐。
组合标志
Qt::AlignCenter:       两个维度的中心,等价于 Qt::AlignHCenter | Qt::AlignVCenter。

The indent attribute represents the indentation value of the text

int indent() const;             // 获取文本缩进值 
void setIndex(int indent);      //设置文本缩进值 

The margin attribute represents the margin of the displayed content

int margin() const;             // 获取边距 
void setMargin(int margin);     // 设置边距 

The wordWrap attribute represents whether the displayed text allows line breaks

bool wordWrap() const;         // 判断是否允许换行 
void setWordWrap(bool on);     // 设置是否允许换行 

scaledContents This property is used to determine whether the label scales its content to fill all available space.

bool hasScaledContenes() const;      // 判断是否允许图像缩放 
void setScaledContents(bool on);     // 设置是否允许图像缩放 
bool hasScaledContenes() const;      // 判断是否允许图像缩放
void setScaledContents(bool on);     // 设置是否允许图像缩放

QPixmap display image

const QPixmap *pixmap() const;         // 获取显示的图像 
void setPixmap(const QPixmap &pixmap); // 设置显示的图像

QMovie shows gif

QMovie *movie() const;       //获取当前的gif信息
void setMovie(QMovie *movie);//显示一个通过QMovie加载的gif文件

There are also some commonly used methods inherited from the parent class, which you can query through the documentation, and I won’t explain too much here.

QFont font():     //当前是显示的字体QFont
int width():      //宽度
int height():     //高度
const int x():    //坐标
const int y():    //坐标

3. Example 1 display content and setting style

 QLabel *label = new QLabel(this);
 label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
 label->setStyleSheet("color: red");
 label->setText("first line\nsecond line");
 label->setAlignment(Qt::AlignBottom | Qt::AlignRight);

        The label content is changed by adding ''\n'', the horizontal direction of the file is set by Alignment, and the text style is set by StyleSheet. Later , we will introduce the use of StyleSheet. Since QLabel inherits from QFrame, the control style can be drawn by setFrameStyle.

  • QFrame::NoFrame does not draw anything
  • QFrame::Box draws a border around it
  • QFrame::Panel draws a panel to make the content raised or lowered
  • QFrame::StyledPane draws a rectangular panel whose appearance depends on the current GUI style. It can be raised or recessed.
  • QFrame::HLine draws a horizontal line that contains nothing (used as a separator)
  • QFrame::VLine draws a vertical line that contains nothing (can be used as a separator)
  • QFrame::WinPanel Draws a rectangular panel that can be raised or sunken like panels in Windows 2000. Specifying this shape sets the Line Width to 2 px. WinPanel provides compatibility. In order to ensure GUI style independence, it is recommended to use StyledPanel.
  • QFrame::Plain The frame and content are consistent with the surrounding environment; use the palette QPalette::WindowText color to draw (without any 3D effects)
  • QFrame::Raised The frame and content are raised, and a 3D raised line is drawn using the light and dark colors of the current color group
  • QFrame::Sunken The frame and content are in a sinking state, and a 3D sinking line is drawn using the light and dark colors of the current color group

 

4. Example 2 Use html to process hyperlinks

ui->label->setOpenExternalLinks(true);
ui->label->setText("<a style='color: green; text-decoration: none' href = https://blog.csdn.net/u014491932/article/details/131740931?spm=1001.2014.3001.5501>CSDN");
ui->label->setAlignment(Qt::AlignCenter);


ui->label_2->setText("<a style='color: green; text-decoration: none' href = https://blog.csdn.net/u014491932/article/details/131740931?spm=1001.2014.3001.5501>CSDN");
ui->label_2->setAlignment(Qt::AlignCenter);
connect(ui->label_2, &QLabel::linkActivated, [=](QString url){
        QDesktopServices::openUrl(QUrl(url));
    });

        QLabel is capable of displaying rich text specified using a subset of HTML 4 tags.

        openExternalLinks specifies whether the QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal.

5. Example 3 display image

QLabel *label = new QLabel(this);
QPixmap picture1 ;
picture1.load("D:/creator-gs-01.png");
label = new QLabel(this);
label->setPixmap( picture1);
label->setScaledContents(true);//自适应大小
label->resize(200, 200);

         QPixmap loads a png image, and then displays the image through the setPixmap method. The scaledContents default value is false, when enabled and the label displays a pixmap, it will scale the pixmap to fill the available space.

6. Example 4 displays GIF

QLabel *label = new QLabel(this);
ui->label->resize(320,200);
QMovie *movie = new QMovie("D:/earth.gif");
movie->setSpeed(200);//设置速度
movie->start();
ui->label->setMovie(movie);

         Use QMovie to load the gif file to be played, and set the playback speed, 200 means 200%, which is 2X speed.

7. Example 5 Partner Mechanism

ui->label->setText("&Name:");
ui->label_2->setText("&Phone:");
ui->label->setBuddy(ui->lineEdit);
ui->label_2->setBuddy(ui->lineEdit_2);

        Pull a grid layout on the ui interface, add two Qlabels and two lineedits, set the first label to &Name, the second label to &Phone, and set the two input boxes as their partners respectively. When the user presses Alt+N, the focus jumps to the Name field, and when the user presses Alt+P, the focus jumps to the Phone field.

        The buddy mechanism only works with qlabels that contain text prefixed with an "&" symbol, which is set as a shortcut key.

Guess you like

Origin blog.csdn.net/u014491932/article/details/131740931