天天看點

QLabel

今天在QLabel裡面放了QPixmap的圖檔,把label和textEdit加到QSplitter裡面,發現拖動中間的線,圖檔可以拉大但是不能縮小。還是比較全面的看一下文檔比較好。

較長的描述

QLabel視窗提供了一個文本或者圖檔的展示

QLabel被用于顯示文本或者圖檔,沒有提供使用者互動功能。标簽的可視外觀可以通過多種方式配置,還可以用于為另一個小部件指定焦點助記鍵。

QLabel可以包含下面的這些内容:

Content                                           Setting

Plain text  (純文字)                          Pass a QString to setText().

Rich text   (富文本)                       Pass a QString that contains rich text to setText().

A pixmap    (圖檔)                         Pass a QPixmap to setPixmap().

A movie      (小電影)                           Pass a QMovie to setMovie().

A number        (數)                   Pass an int or a double to setNum(), which converts the number to plain text.

Nothing          (啥沒有)              The same as an empty plain text. This is the default. Set by clear().

警告:在将QString傳遞給構造函數或調用setText()時,請確定對輸入進行清理,因為QLabel試圖猜測它是将文本顯示為純文字還是富文本(HTML 4标記的子集)。您可能想要顯式地調用setTextFormat(),例如,如果您希望文本是純格式的,但是無法控制文本源(例如,在顯示從Web加載的資料時)。

當使用這些函數更改内容時,将清除以前的任何内容。

預設情況下,标簽會顯示左對齊、垂直居中的文本和圖像,其中要顯示的文本中的任何頁籤都會自動展開。然而,QLabel的外觀可以通過多種方式進行調整和微調。

可以使用setal()和setIndent()調整QLabel小部件區域内内容的位置。文本内容還可以使用setWordWrap()沿單詞邊界換行。例如,這段代碼在右下角用兩行文字建立了一個下陷面闆(兩行文字都與标簽的右側齊平):

  QLabel *label = new QLabel(this);
  label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
  label->setText("first line\nsecond line");
  label->setAlignment(Qt::AlignBottom | Qt::AlignRight); //右下角,右邊對其
           

繼承自QFrame的屬性和函數QLabel還可用于指定用于任何給定标簽的小部件架構。

QLabel通常用作互動式小部件的标簽。為此,QLabel提供了一種添加助記符(參見QKeySequence)的有用機制,該助記符将鍵盤焦點設定為另一個小部件(稱為QLabel的“buddy”)。例如:

QLineEdit *phoneEdit = new QLineEdit(this);
QLabel *phoneLabel = new QLabel("&Phone:", this);
phoneLabel->setBuddy(phoneEdit);   //因為Qlabe就是一個标簽,是以可以作為它所标記的空間的助記夥伴
           

在本例中,當使用者按下Alt+P時,鍵盤焦點被轉移到标簽的好友(QLineEdit)。如果好友是按鈕(繼承自QAbstractButton),觸發助記符将模拟按鈕單擊。

屬性文檔

alignment : Qt::Alignment   //對齊

This property holds the alignment of the label's contents

By default, the contents of the label are left-aligned and vertically-centered. //預設左對齊,縱向居中對齊

hasSelectedText : const bool  //是否有文字被選中

This property holds whether there is any text selected

hasSelectedText() returns true if some or all of the text has been selected by the user; otherwise returns false.

By default, this property is false.  /預設否

Note: The textInteractionFlags set on the label need to include either TextSelectableByMouse or TextSelectableByKeyboard.

indent : int  //縮進

此屬性儲存标簽的文本縮進(以像素為機關)

如果标簽顯示文本,則縮進适用于alignment()是Qt::AlignLeft則相對于左邊界,對右對齊()是Qt::AlignRight,對上對齊()是Qt::AlignTop,對下對齊()是Qt::AlignBottom。

如果縮進是負數,或者沒有設定縮進,那麼标簽按如下方式計算有效縮進:如果frameWidth()為0,則有效縮進為0。如果frameWidth()大于0,則有效縮進為小部件目前字型()的“x”字元寬度的一半。

預設情況下,縮進是-1,這意味着按照上面描述的方式計算有效的縮進。

margin : int

此屬性儲存頁邊距的寬度

邊距是指架構最内層像素與内容最外層像素之間的距離。

預設為0。

openExternalLinks : bool

指定QLabel是否應該使用QDesktopServices::openUrl()自動打開連結,而不是發出linkActivated()信号。

注意:标簽上設定的textInteractionFlags需要包括LinksAccessibleByMouse或LinksAccessibleByKeyboard。

預設值為false。

pixmap : QPixmap

This property holds the label's pixmap

If no pixmap has been set this will return 0.

Setting the pixmap clears any previous content. The buddy shortcut, if any, is disabled.

scaledContents : bool   //這是個挺有用的屬性

此屬性儲存标簽是否會縮放其内容以填充所有可用空間。

當啟用時,标簽顯示一個像素圖,它将縮放像素圖來填充可用空間。

此屬性的預設值為false。

selectedText : const QString

This property holds the selected text

If there is no selected text this property's value is an empty string.

By default, this property contains an empty string.

Note: The textInteractionFlags set on the label need to include either TextSelectableByMouse or TextSelectableByKeyboard.

text : QString

This property holds the label's text

If no text has been set this will return an empty string. Setting the text clears any previous content.

根據文本格式設定,文本将被解釋為純文字或富文本;看到setTextFormat ()。預設設定是Qt::AutoText;例如,QLabel将嘗試自動檢測文本集的格式。有關富文本的定義,請參閱受支援的HTML子集。

If a buddy has been set, the buddy mnemonic key is updated from the new text.

請注意,QLabel非常适合顯示小型富文本文檔,比如從标簽的調色闆和字型屬性獲得文檔特定設定(字型、文本顔色、連結顔色)的小型文檔。對于大型文檔,使用QTextEdit的隻讀模式。QTextEdit還可以在必要時提供滾動條。

textFormat : Qt::TextFormat

此屬性儲存标簽的文本格式

textInteractionFlags : Qt::TextInteractionFlags

指定标簽在顯示文本時應如何與使用者輸入互動。

如果标記包含Qt::LinksAccessibleByKeyboard,則焦點政策也會自動設定為Qt::StrongFocus。如果Qt::TextSelectableByKeyboard被設定,那麼焦點政策被設定為Qt::ClickFocus。

預設值是Qt::LinksAccessibleByMouse。

wordWrap : bool

此屬性儲存标簽的換行政策

如果此屬性為真,則在必要的斷句處對标簽文本進行換行;否則它根本就不會換行。

預設情況下,自動換行是禁用的。

成員函數文檔

//兩個構造

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

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

//傳回夥伴

QWidget *QLabel::buddy() const

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

Reimplemented from QWidget::changeEvent().

[slot] void QLabel::clear()  //清除内容

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

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

 QSize QLabel::minimumSizeHint() const  //推薦最小尺寸

QMovie *QLabel::movie() const  //傳回小電影

const QPicture *QLabel::picture() const  //傳回圖檔

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

//注意 Note: The textInteractionFlags set on the label need to include either TextSelectableByMouse or TextSelectableByKeyboard. 要設定能否選中

void QLabel::setBuddy(QWidget *buddy) //設定夥伴

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)
           

[slot] void QLabel::setMovie(QMovie *movie) //放電影

Sets the label contents to movie. Any previous content is cleared. The label does NOT take ownership of the movie.

The buddy shortcut, if any, is disabled.

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

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

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

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

[virtual] QSize QLabel::sizeHint() const

繼續閱讀