QLabel 类

QLabel 小部件提供文本或图像显示。 更多...

头: #include <QLabel>
qmake: QT += widgets
继承: QFrame

特性

公共函数

QLabel (QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
QLabel (const QString & text , QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
~QLabel ()
Qt::Alignment alignment () const
QWidget * buddy () const
bool hasScaledContents () const
bool hasSelectedText () const
int indent () const
int margin () const
QMovie * movie () const
bool openExternalLinks () const
const QPicture * picture () const
const QPixmap * pixmap () const
QString selectedText () const
int selectionStart () const
void setAlignment ( Qt::Alignment )
void setBuddy (QWidget * buddy )
void setIndent ( int )
void setMargin ( int )
void setOpenExternalLinks (bool open )
void setScaledContents ( bool )
void setSelection (int start , int length )
void setTextFormat ( Qt::TextFormat )
void setTextInteractionFlags (Qt::TextInteractionFlags flags )
void setWordWrap (bool on )
QString text () const
Qt::TextFormat textFormat () const
Qt::TextInteractionFlags textInteractionFlags () const
bool wordWrap () const

重实现公共函数

virtual int heightForWidth (int w ) const
virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const

公共槽

void clear ()
void setMovie (QMovie * movie )
void setNum (int num )
void setNum (double num )
void setPicture (const QPicture & picture )
void setPixmap ( const QPixmap & )
void setText ( const QString & )

信号

void linkActivated (const QString & link )
void linkHovered (const QString & link )

重实现保护函数

virtual void changeEvent (QEvent * ev )
virtual void contextMenuEvent (QContextMenuEvent * ev )
virtual bool event (QEvent * e )
virtual void focusInEvent (QFocusEvent * ev )
virtual bool focusNextPrevChild (bool next )
virtual void focusOutEvent (QFocusEvent * ev )
virtual void keyPressEvent (QKeyEvent * ev )
virtual void mouseMoveEvent (QMouseEvent * ev )
virtual void mousePressEvent (QMouseEvent * ev )
virtual void mouseReleaseEvent (QMouseEvent * ev )
virtual void paintEvent ( QPaintEvent * )

额外继承成员

详细描述

QLabel 小部件提供文本或图像显示。

QLabel is used for displaying text or an image. No user interaction functionality is provided. The visual appearance of the label can be configured in various ways, and it can be used for specifying a focus mnemonic key for another widget.

A QLabel can contain any of the following content types:

内容 设置
纯文本 传递 QString to setText ().
富文本 传递 QString 包含富文本到 setText ().
像素图 传递 QPixmap to setPixmap ().
影片 传递 QMovie to setMovie ().
数字 传递 int double to setNum (),将数字转换为纯文本。
什么都没有 如同空纯文本。这是默认。设置通过 clear ().

警告: 当传递 QString 到构造函数或调用 setText (), make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text, a subset of HTML 4 markup. You may want to call setTextFormat () 明确,如:若期望按纯文本格式却无法控制文本源 (例如:当显示从 Web 加载的数据时)。

当使用这些函数中的任一改变内容时,任何先前内容被清零。

默认情况下,标签显示 左对齐,垂直居中 文本和图像,要显示文本中的任何 Tab 都会 自动展开 . However, the look of a QLabel can be adjusted and fine-tuned in several ways.

The positioning of the content within the QLabel widget area can be tuned with setAlignment () 和 setIndent ()。文本内容也可以沿单词边界自动换行采用 setWordWrap ()。例如,此代码在右下角设置具有两行文本的凹陷面板 (两行文本与标签右侧齐平):

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

The properties and functions QLabel 继承自 QFrame 还可以用于指定要用于任何给定标签的 Widget 框架。

A QLabel is often used as a label for an interactive widget. For this use QLabel provides a useful mechanism for adding an mnemonic (see QKeySequence ) that will set the keyboard focus to the other widget (called the QLabel 's "buddy"). For example:

QLineEdit *phoneEdit = new QLineEdit(this);
QLabel *phoneLabel = new QLabel("&Phone:", this);
phoneLabel->setBuddy(phoneEdit);
					

在此范例中,键盘焦点被转移给标签好友 ( QLineEdit ) 当用户按下 Alt+P 键时。若好友是按钮 (继承自 QAbstractButton ),触发助记键将模拟按钮点击。

另请参阅 QLineEdit , QTextEdit , QPixmap , QMovie ,和 GUI 设计手册:标签 .

特性文档编制

alignment : Qt::Alignment

此特性保持标签内容的对齐方式

默认情况下,标签内容左对齐且垂直居中。

访问函数:

Qt::Alignment alignment () const
void setAlignment ( Qt::Alignment )

另请参阅 text .

hasSelectedText : const bool

此特性保持是否有选中任何文本。

hasSelectedText() 返回 true 若用户有选中部分或全部文本;否则返回 false .

默认情况下此特性为 false .

注意: textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

该特性在 Qt 4.7 引入。

访问函数:

bool hasSelectedText () const

另请参阅 selectedText ().

indent : int

此特性保持标签的文本缩进 (以像素为单位)

若标签显示文本,缩进被应用到左边缘,若 alignment () 是 Qt::AlignLeft ,到右边缘若 alignment () 是 Qt::AlignRight ,到上边缘若 alignment () 是 Qt::AlignTop ,和到下边缘若 alignment () 是 Qt::AlignBottom .

若缩进为负数或未设置缩进,标签将计算有效缩进,如下所示:若 frameWidth () 为 0,有效缩进变为 0。若 frameWidth () 大于 0,有效缩进变为小部件 x 字符宽度的一半相对当前 font ().

默认情况下,缩进为 -1,意味着有效缩进按上述方式计算。

访问函数:

int indent () const
void setIndent ( int )

另请参阅 alignment , margin , frameWidth (),和 font ().

margin : int

此特性保存边距的宽度

边距是框架最内像素和内容最外像素之间的距离。

默认边距为 0。

访问函数:

int margin () const
void setMargin ( int )

另请参阅 indent .

指定是否 QLabel 应自动打开链接使用 QDesktopServices::openUrl () 而不是发射 linkActivated () 信号。

注意: textInteractionFlags 在标签上设置需要包括 LinksAccessibleByMouse 或 LinksAccessibleByKeyboard。

默认值为 false。

该特性在 Qt 4.2 引入。

访问函数:

bool openExternalLinks () const
void setOpenExternalLinks (bool open )

另请参阅 textInteractionFlags ().

pixmap : QPixmap

This property holds the label's pixmap

If no pixmap has been set this will return 0.

设置像素图会清零任何之前内容。好友快捷方式 (若有的话) 被禁用。

访问函数:

const QPixmap * pixmap () const
void setPixmap ( const QPixmap & )

scaledContents : bool

此特性保持标签是否比例缩放其内容,以填充所有可用空间。

当启用且标签展示像素图时,它将比例缩放像素图以填充可用空间。

此特性默认为 false。

访问函数:

bool hasScaledContents () const
void setScaledContents ( bool )

selectedText : const QString

此特性保持选中文本

若没有选中文本,此特性的值为空字符串。

默认情况下,此特性包含空字符串。

注意: textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

该特性在 Qt 4.7 引入。

访问函数:

QString selectedText () const

另请参阅 hasSelectedText ().

text : QString

此特性保持标签的文本

若没有设置文本,这将返回空字符串。设置文本清零任何先前内容。

The text will be interpreted either as plain text or as rich text, depending on the text format setting; see setTextFormat ()。默认设置为 Qt::AutoText ;即 QLabel will try to auto-detect the format of the text set. See 支持的 HTML 子集 对于富文本的定义。

若有设置好友,从新文本更新好友助记键。

注意, QLabel is well-suited to display small rich text documents, such as small documents that get their document specific settings (font, text color, link color) from the label's palette and font properties. For large documents, use QTextEdit in read-only mode instead. QTextEdit can also provide a scroll bar when necessary.

注意: 此函数启用鼠标跟踪若 text 包含富文本。

访问函数:

QString text () const
void setText ( const QString & )

另请参阅 setTextFormat (), setBuddy (),和 alignment .

textFormat : Qt::TextFormat

此特性保持标签的文本格式

Qt::TextFormat 枚举了解可能选项的解释。

默认格式为 Qt::AutoText .

访问函数:

Qt::TextFormat textFormat () const
void setTextFormat ( Qt::TextFormat )

另请参阅 text ().

textInteractionFlags : Qt::TextInteractionFlags

指定标签应如何与用户输入交互,若它显示文本。

若标志包含 Qt::LinksAccessibleByKeyboard 聚焦策略还被自动设为 Qt::StrongFocus 。若 Qt::TextSelectableByKeyboard 有设置那么聚焦策略被设为 Qt::ClickFocus .

默认值为 Qt::LinksAccessibleByMouse .

该特性在 Qt 4.2 引入。

访问函数:

Qt::TextInteractionFlags textInteractionFlags () const
void setTextInteractionFlags (Qt::TextInteractionFlags flags )

wordWrap : bool

此特性保持标签的自动换行策略

若此特性为 true 那么标签文本会在单词必要断开处换行;否则根本不换行。

默认情况下,自动换行是禁用的。

访问函数:

bool wordWrap () const
void setWordWrap (bool on )

另请参阅 text .

成员函数文档编制

QLabel:: QLabel ( QWidget * parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())

构造空标签。

parent 和 Widget 标志 f ,自变量被传递给 QFrame 构造函数。

另请参阅 setAlignment (), setFrameStyle (),和 setIndent ().

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

构造标签显示文本 text .

parent 和 Widget 标志 f ,自变量被传递给 QFrame 构造函数。

另请参阅 setText (), setAlignment (), setFrameStyle (),和 setIndent ().

QLabel:: ~QLabel ()

销毁标签。

QWidget *QLabel:: buddy () const

Returns this label's buddy, or 0 if no buddy is currently set.

另请参阅 setBuddy ().

[virtual protected] void QLabel:: changeEvent ( QEvent * ev )

重实现自 QWidget::changeEvent ().

[slot] void QLabel:: clear ()

清零任何标签内容。

[virtual protected] void QLabel:: contextMenuEvent ( QContextMenuEvent * ev )

重实现自 QWidget::contextMenuEvent ().

[virtual protected] bool QLabel:: event ( QEvent * e )

重实现自 QObject::event ().

[virtual protected] void QLabel:: focusInEvent ( QFocusEvent * ev )

重实现自 QWidget::focusInEvent ().

[virtual protected] bool QLabel:: focusNextPrevChild ( bool next )

重实现自 QWidget::focusNextPrevChild ().

[virtual protected] void QLabel:: focusOutEvent ( QFocusEvent * ev )

重实现自 QWidget::focusOutEvent ().

[virtual] int QLabel:: heightForWidth ( int w ) const

重实现自 QWidget::heightForWidth ().

[virtual protected] void QLabel:: keyPressEvent ( QKeyEvent * ev )

重实现自 QWidget::keyPressEvent ().

[signal] void QLabel:: linkActivated (const QString & link )

此信号被发射当用户点击链接时。由锚点引用的 URL 被传入 link .

该函数在 Qt 4.2 引入。

另请参阅 linkHovered ().

[signal] void QLabel:: linkHovered (const QString & link )

此信号被发射当用户悬停在链接之上时。通过锚点引用的 URL 被传入 link .

该函数在 Qt 4.2 引入。

另请参阅 linkActivated ().

[virtual] QSize QLabel:: minimumSizeHint () const

重实现自 QWidget::minimumSizeHint ().

[virtual protected] void QLabel:: mouseMoveEvent ( QMouseEvent * ev )

重实现自 QWidget::mouseMoveEvent ().

[virtual protected] void QLabel:: mousePressEvent ( QMouseEvent * ev )

重实现自 QWidget::mousePressEvent ().

[virtual protected] void QLabel:: mouseReleaseEvent ( QMouseEvent * ev )

重实现自 QWidget::mouseReleaseEvent ().

QMovie *QLabel:: movie () const

Returns a pointer to the label's movie, or 0 if no movie has been set.

另请参阅 setMovie ().

[virtual protected] void QLabel:: paintEvent ( QPaintEvent * )

重实现自 QWidget::paintEvent ().

const QPicture *QLabel:: picture () const

Returns the label's picture or 0 if the label doesn't have a picture.

另请参阅 setPicture ().

int QLabel:: selectionStart () const

selectionStart() returns the index of the first selected character in the label or -1 if no text is selected.

注意: textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

该函数在 Qt 4.7 引入。

另请参阅 selectedText ().

void QLabel:: setBuddy ( QWidget * buddy )

将此标签的好友设为 buddy .

When the user presses the shortcut key indicated by this label, the keyboard focus is transferred to the label's buddy widget.

The buddy mechanism is only available for QLabels that contain text in which one character is prefixed with an ampersand, '&'. This character is set as the shortcut key. See the QKeySequence::mnemonic () documentation for details (to display an actual ampersand, use '&&').

In a dialog, you might create two data entry widgets and a label for each, and set up the geometry layout so each label is just to the left of its data entry widget (its "buddy"), for example:

QLineEdit *nameEdit  = new QLineEdit(this);
QLabel    *nameLabel = new QLabel("&Name:", this);
nameLabel->setBuddy(nameEdit);
QLineEdit *phoneEdit  = new QLineEdit(this);
QLabel    *phoneLabel = new QLabel("&Phone:", this);
phoneLabel->setBuddy(phoneEdit);
// (layout setup not shown)
					

With the code above, the focus jumps to the Name field when the user presses Alt+N, and to the Phone field when the user presses Alt+P.

要取消先前设置的好友,调用此函数采用 buddy set to 0.

另请参阅 buddy (), setText (), QShortcut ,和 setAlignment ().

[slot] void QLabel:: setMovie ( QMovie * movie )

将标签内容设为 movie . Any previous content is cleared. The label does NOT take ownership of the movie.

好友快捷方式 (若有的话) 被禁用。

另请参阅 movie () 和 setBuddy ().

[slot] void QLabel:: setNum ( int num )

Sets the label contents to plain text containing the textual representation of integer num . Any previous content is cleared. Does nothing if the integer's string representation is the same as the current contents of the label.

好友快捷方式 (若有的话) 被禁用。

另请参阅 setText (), QString::setNum (),和 setBuddy ().

[slot] void QLabel:: setNum ( double num )

这是重载函数。

Sets the label contents to plain text containing the textual representation of double num . Any previous content is cleared. Does nothing if the double's string representation is the same as the current contents of the label.

好友快捷方式 (若有的话) 被禁用。

另请参阅 setText (), QString::setNum (),和 setBuddy ().

[slot] void QLabel:: setPicture (const QPicture & picture )

将标签内容设为 picture 。任何先前内容被清零。

好友快捷方式 (若有的话) 被禁用。

另请参阅 picture () 和 setBuddy ().

void QLabel:: setSelection ( int start , int length )

选择文本从位置 start 和对于 length 字符。

注意: textInteractionFlags 设置标签需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

该函数在 Qt 4.7 引入。

另请参阅 selectedText ().

[virtual] QSize QLabel:: sizeHint () const

重实现自 QWidget::sizeHint ().