QLabel 類

The 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 * )

額外繼承成員

詳細描述

The 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 .

注意: The 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 () 信號。

注意: The 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

此特性保持選中文本

若沒有選中文本,此特性的值為空字符串。

默認情況下,此特性包含空字符串。

注意: The textInteractionFlags 設置標簽需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

該特性在 Qt 4.7 引入。

訪問函數:

QString selectedText () const

另請參閱 hasSelectedText ().

text : QString

此特性保持標簽的文本

若沒有設置文本,這會返迴空字符串。設置文本將清零任何先前內容。

將把文本解釋成純文本 (或富文本),從屬文本格式設定;見 setTextFormat ()。默認設置為 Qt::AutoText ;即 QLabel 將試著自動檢測文本集格式。見 支持的 HTML 子集 對於富文本的定義。

若有設置好友,從新文本更新好友助記符鍵。

注意, QLabel 非常適閤顯示小富文本文檔,譬如,從標簽調色闆和字體特性獲取文檔具體設定 (字體、文本顔色、鏈接顔色) 的小文檔。對於大文檔,使用 QTextEdit 以隻讀方式代替。 QTextEdit 還能提供滾動條,當有必要時。

注意: 此函數啓用鼠標追蹤若 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())

構造空標簽。

The parent 和 Widget 標誌 f ,自變量被傳遞給 QFrame 構造函數。

另請參閱 setAlignment (), setFrameStyle (),和 setIndent ().

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

構造標簽顯示文本 text .

The 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 ().

[虛擬] 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 ().

[虛擬] 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() 返迴標簽第 1 選中字符的索引,或 -1 若未選中文本。

注意: The textInteractionFlags 設置標簽需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

該函數在 Qt 4.7 引入。

另請參閱 selectedText ().

void QLabel:: setBuddy ( QWidget * buddy )

將此標簽的好友設為 buddy .

當用戶按下此標簽指示的快捷鍵時,將轉移鍵盤聚焦到標簽的好友 Widget。

好友機製隻可用於包含文本的某個字符以 & 和號作前綴的 QLabel。此字符被設為快捷鍵。見 QKeySequence::mnemonic () 文檔編製瞭解細節 (要顯示實際和號,使用 &&)。

可以在對話框中創建 2 數據輸入 Widget 並為每個 Widget 創建標簽,然後設置幾何布局,使各標簽位於其數據輸入 Widget (其好友) 左側,例如:

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)
					

采用以上代碼,聚焦跳轉到 Name 字段當用戶按下 Alt+N 時,和跳轉到 Phone 字段當用戶按 Alt+P 時。

要取消先前設置的好友,調用此函數采用 buddy set to 0.

另請參閱 buddy (), setText (), QShortcut ,和 setAlignment ().

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

將標簽內容設為 movie 。清零任何先前內容。標簽未擁有影片的所有權。

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

另請參閱 movie () 和 setBuddy ().

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

使設置標簽內容的純文本包含正文錶示的整數 num 。清零任何先前內容。什麼都不做,若整數的字符串錶示如同標簽的當前內容。

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

另請參閱 setText (), QString::setNum (),和 setBuddy ().

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

這是重載函數。

使設置標簽內容的純文本包含正文錶示的雙精度 num 。清零任何先前內容。什麼都不做,若雙精度的字符串錶示如同標簽的當前內容。

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

另請參閱 setText (), QString::setNum (),和 setBuddy ().

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

將標簽內容設為 picture 。清零任何先前內容。

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

另請參閱 picture () 和 setBuddy ().

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

選擇文本從位置 start 和對於 length 字符。

注意: The textInteractionFlags 設置標簽需要包括 TextSelectableByMouse 或 TextSelectableByKeyboard。

該函數在 Qt 4.7 引入。

另請參閱 selectedText ().

[虛擬] QSize QLabel:: sizeHint () const

重實現自 QWidget::sizeHint ().