📄 qlabel.cpp
字号:
#ifndef QT_NO_PICTURE/*! Sets the label contents to \a picture. Any previous content is cleared. The buddy shortcut, if any, is disabled. \sa picture(), setBuddy()*/void QLabel::setPicture(const QPicture &picture){ Q_D(QLabel); d->clearContents(); d->lpicture = new QPicture(picture); d->updateLabel();}#endif // QT_NO_PICTURE/*! Sets the label contents to plain text containing the textual representation of integer \a num. Any previous content is cleared. Does nothing if the integer's string representation is the same as the current contents of the label. The buddy shortcut, if any, is disabled. The label resizes itself if auto-resizing is enabled. \sa setText(), QString::setNum(), setBuddy()*/void QLabel::setNum(int num){ QString str; str.setNum(num); setText(str);}/*! \overload Sets the label contents to plain text containing the textual representation of double \a num. Any previous content is cleared. Does nothing if the double's string representation is the same as the current contents of the label. The buddy shortcut, if any, is disabled. The label resizes itself if auto-resizing is enabled. \sa setText(), QString::setNum(), setBuddy()*/void QLabel::setNum(double num){ QString str; str.setNum(num); setText(str);}/*! \property QLabel::alignment \brief the alignment of the label's contents \sa text*/void QLabel::setAlignment(Qt::Alignment alignment){ Q_D(QLabel); if (alignment == (d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask))) return; d->align = (d->align & ~(Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)) | (alignment & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask)); d->updateLabel();}#ifdef QT3_SUPPORT/*! Use setAlignment(Qt::Alignment) instead. If \a alignment specifies text flags as well, use setTextFormat() to set those.*/void QLabel::setAlignment(int alignment){ Q_D(QLabel); d->align = alignment & ~(Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask|Qt::TextWordWrap);#ifndef QT_NO_SHORTCUT if (d->lbuddy) d->align |= Qt::TextShowMnemonic;#endif setAlignment(Qt::Alignment(QFlag(alignment)));}#endifQt::Alignment QLabel::alignment() const{ Q_D(const QLabel); return QFlag(d->align & (Qt::AlignVertical_Mask|Qt::AlignHorizontal_Mask));}/*! \property QLabel::wordWrap \brief the label's word-wrapping policy If this property is true then label text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all.*/void QLabel::setWordWrap(bool on){ Q_D(QLabel); if (on) d->align |= Qt::TextWordWrap; else d->align &= ~Qt::TextWordWrap; d->updateLabel();}bool QLabel::wordWrap() const{ Q_D(const QLabel); return d->align & Qt::TextWordWrap;}/*! \property QLabel::indent \brief the label's text indent in pixels If a label displays text, the indent applies to the left edge if alignment() is Qt::AlignLeft, to the right edge if alignment() is Qt::AlignRight, to the top edge if alignment() is Qt::AlignTop, and to to the bottom edge if alignment() is Qt::AlignBottom. If indent is negative, or if no indent has been set, the label computes the effective indent as follows: If frameWidth() is 0, the effective indent becomes 0. If frameWidth() is greater than 0, the effective indent becomes half the width of the "x" character of the widget's current font(). \sa alignment, margin, frameWidth(), font()*/void QLabel::setIndent(int indent){ Q_D(QLabel); d->extraMargin = indent; d->updateLabel();}int QLabel::indent() const{ Q_D(const QLabel); return d->extraMargin;}/*! \property QLabel::margin \brief the width of the margin The margin is the distance between the innermost pixel of the frame and the outermost pixel of contents. The default margin is 0. \sa indent*/int QLabel::margin() const{ Q_D(const QLabel); return d->margin;}void QLabel::setMargin(int margin){ Q_D(QLabel); if (d->margin == margin) return; d->margin = margin; d->updateLabel();}/*! Returns the size that will be used if the width of the label is \a w. If \a w is -1, the sizeHint() is returned.*/QSize QLabelPrivate::sizeForWidth(int w) const{ Q_Q(const QLabel); QSize contentsMargin(leftmargin + rightmargin, topmargin + bottommargin); w -= contentsMargin.width(); QRect br; QPixmap *pix = lpixmap;#ifndef QT_NO_PICTURE QPicture *pic = lpicture;#else const int pic = 0;#endif#ifndef QT_NO_MOVIE QMovie *mov = lmovie;#else const int mov = 0;#endif int hextra = 2 * margin; int vextra = hextra; QFontMetrics fm(q->fontMetrics()); int xw = fm.width('x'); if (!mov && !pix && !pic) { int m = extraMargin; if (m < 0 && frameWidth) // no indent, but we do have a frame m = (xw / 2 - margin) * 2; if (m >= 0) { int align = QStyle::visualAlignment(q->layoutDirection(), QFlag(this->align)); if ((align & Qt::AlignLeft) || (align & Qt::AlignRight)) hextra += m; if ((align & Qt::AlignTop) || (align & Qt::AlignBottom)) vextra += m; } } if (pix) br = pix->rect();#ifndef QT_NO_PICTURE else if (pic) br = pic->boundingRect();#endif#ifndef QT_NO_MOVIE else if (mov) br = mov->currentPixmap().rect();#endif else if (doc) { QTextDocumentLayout *layout = qobject_cast<QTextDocumentLayout *>(doc->documentLayout()); Q_ASSERT(layout); if (align & Qt::TextWordWrap) { if (w > 0) doc->setPageSize(QSize(w-hextra, INT_MAX)); else layout->adjustSize(); } else { doc->setPageSize(QSize(0, 100000)); } br = QRect(QPoint(0, 0), layout->documentSize().toSize()); } else { bool tryWidth = (w < 0) && (align & Qt::TextWordWrap); if (tryWidth) w = xw * 80; else if (w < 0) w = 2000; w -= hextra; QString text = q->text(); br = fm.boundingRect(0, 0, w ,2000, align, text); if (tryWidth && br.height() < 4*fm.lineSpacing() && br.width() > w/2) br = fm.boundingRect(0, 0, w/2, 2000, align, text); if (tryWidth && br.height() < 2*fm.lineSpacing() && br.width() > w/4) br = fm.boundingRect(0, 0, w/4, 2000, align, text); } int wid = br.width() + hextra; int hei = br.height() + vextra; return QSize(wid, hei) + contentsMargin;}/*! \reimp*/int QLabel::heightForWidth(int w) const{ Q_D(const QLabel); if ( d->doc || (d->align & Qt::TextWordWrap)) return d->sizeForWidth(w).height(); return QWidget::heightForWidth(w);}/*!\reimp*/QSize QLabel::sizeHint() const{ Q_D(const QLabel); if (!d->valid_hints) (void) QLabel::minimumSizeHint(); return d->sh;}/*! \reimp*/QSize QLabel::minimumSizeHint() const{ Q_D(const QLabel); if (d->valid_hints) return d->msh; ensurePolished(); d->valid_hints = true; d->sh = d->sizeForWidth(-1); QSize sz(-1, -1); if ( !d->doc && (d->align & Qt::TextWordWrap) == 0) { sz = d->sh; } else { // think about caching these for performance sz.rwidth() = d->sizeForWidth(0).width(); sz.rheight() = d->sizeForWidth(QWIDGETSIZE_MAX).height(); if (d->sh.height() < sz.height()) sz.rheight() = d->sh.height(); } if (sizePolicy().horizontalPolicy() == QSizePolicy::Ignored) sz.rwidth() = -1; if (sizePolicy().verticalPolicy() == QSizePolicy::Ignored) sz.rheight() = -1; d->msh = sz; return sz;}/*!\reimp*/bool QLabel::event(QEvent *e){ Q_D(QLabel); if (e->type() == QEvent::Shortcut) { QShortcutEvent *se = static_cast<QShortcutEvent *>(e); if (se->shortcutId() == d->shortcutId) { QWidget * w = d->lbuddy; QAbstractButton *button = qobject_cast<QAbstractButton *>(w); if (w->focusPolicy() != Qt::NoFocus) w->setFocus(Qt::ShortcutFocusReason); if (button && !se->isAmbiguous()) button->animateClick(); else window()->setAttribute(Qt::WA_KeyboardFocusChange); return true; } } return QFrame::event(e);}/*!\reimp*/void QLabel::paintEvent(QPaintEvent *){ Q_D(QLabel); QStyle *style = QWidget::style(); QPainter paint(this); drawFrame(&paint); QRect cr = contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); QPixmap pix; if (pixmap()) pix = *pixmap();#ifndef QT_NO_PICTURE const QPicture *pic = picture();#else const int pic = 0;#endif#ifndef QT_NO_MOVIE const QMovie *mov = movie();#else const int mov = 0;#endif int align = QStyle::visualAlignment(layoutDirection(), QFlag(d->align)); if (!mov && !pix && !pic) { int m = indent(); if (m < 0 && frameWidth()) // no indent, but we do have a frame m = fontMetrics().width('x') / 2 - d->margin; if (m > 0) { if (align & Qt::AlignLeft) cr.setLeft(cr.left() + m); if (align & Qt::AlignRight) cr.setRight(cr.right() - m); if (align & Qt::AlignTop) cr.setTop(cr.top() + m); if (align & Qt::AlignBottom) cr.setBottom(cr.bottom() - m); } }#ifndef QT_NO_MOVIE if (mov) { QRect r = style->itemPixmapRect(cr, align, mov->currentPixmap()); if (d->scaledcontents) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -