📄 qlabel.cpp
字号:
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->indent = indent; d->updateLabel();}int QLabel::indent() const{ Q_D(const QLabel); return d->indent;}/*! \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. If \a w is 0 minimumSizeHint() is returned*/QSize QLabelPrivate::sizeForWidth(int w) const{ Q_Q(const QLabel); QSize contentsMargin(leftmargin + rightmargin, topmargin + bottommargin); QRect br; int hextra = 2 * margin; int vextra = hextra; QFontMetrics fm = q->fontMetrics(); if (pixmap) br = pixmap->rect();#ifndef QT_NO_PICTURE else if (picture) br = picture->boundingRect();#endif#ifndef QT_NO_MOVIE else if (movie) br = movie->currentPixmap().rect();#endif else if (isTextLabel) { int align = QStyle::visualAlignment(q->layoutDirection(), QFlag(this->align)); // Add indentation int m = indent; if (m < 0 && q->frameWidth()) // no indent, but we do have a frame m = fm.width(QLatin1Char('x')) - margin*2; if (m > 0) { if ((align & Qt::AlignLeft) || (align & Qt::AlignRight)) hextra += m; if ((align & Qt::AlignTop) || (align & Qt::AlignBottom)) vextra += m; } if (control) { ensureTextLayouted(); const qreal oldTextWidth = control->textWidth(); // Calculate the length of document if w is the width if (align & Qt::TextWordWrap) { if (w >= 0) { w = qMax(w-hextra-contentsMargin.width(), 0); // strip margin and indent control->setTextWidth(w); } else { control->adjustSize(); } } else { control->setTextWidth(-1); } br = QRect(QPoint(0, 0), control->size().toSize()); // restore state control->setTextWidth(oldTextWidth); } else { int flags = align; if (hasShortcut) { flags |= Qt::TextShowMnemonic; QStyleOption opt; opt.initFrom(q); if (!q->style()->styleHint(QStyle::SH_UnderlineShortcut, &opt, q)) flags |= Qt::TextHideMnemonic; } bool tryWidth = (w < 0) && (align & Qt::TextWordWrap); if (tryWidth) w = fm.averageCharWidth() * 80; else if (w < 0) w = 2000; w -= (hextra + contentsMargin.width()); br = fm.boundingRect(0, 0, w ,2000, flags, text); if (tryWidth && br.height() < 4*fm.lineSpacing() && br.width() > w/2) br = fm.boundingRect(0, 0, w/2, 2000, flags, text); if (tryWidth && br.height() < 2*fm.lineSpacing() && br.width() > w/4) br = fm.boundingRect(0, 0, w/4, 2000, flags, text); } } else { br = QRect(QPoint(0, 0), QSize(fm.averageCharWidth(), fm.lineSpacing())); } const QSize contentsSize(br.width() + hextra, br.height() + vextra); return contentsSize + contentsMargin;}/*! \reimp*/int QLabel::heightForWidth(int w) const{ Q_D(const QLabel); if (d->isTextLabel) return d->sizeForWidth(w).height(); return QWidget::heightForWidth(w);}/*! \property QLabel::openExternalLinks \since 4.2 Specifies whether QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the anchorClicked() signal. \bold{Note:} The textInteractionFlags set on the label need to include either LinksAccessibleByMouse or LinksAccessibleByKeyboard. The default value is false. \sa textInteractionFlags()*/bool QLabel::openExternalLinks() const{ Q_D(const QLabel); return d->openExternalLinks;}void QLabel::setOpenExternalLinks(bool open){ Q_D(QLabel); d->openExternalLinks = open; if (d->control) d->control->setOpenExternalLinks(open);}/*! \property QLabel::textInteractionFlags \since 4.2 Specifies how the label should interact with user input if it displays text. If the flags contain Qt::LinksAccessibleByKeyboard the focus policy is also automatically set to Qt::StrongFocus. If Qt::TextSelectableByKeyboard is set then the focus policy is set to Qt::ClickFocus. The default value is Qt::LinksAccessibleByMouse.*/void QLabel::setTextInteractionFlags(Qt::TextInteractionFlags flags){ Q_D(QLabel); if (d->textInteractionFlags == flags) return; d->textInteractionFlags = flags; if (flags & Qt::LinksAccessibleByKeyboard) setFocusPolicy(Qt::StrongFocus); else if (flags & (Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse)) setFocusPolicy(Qt::ClickFocus); else setFocusPolicy(Qt::NoFocus); if (d->needTextControl()) { d->ensureTextControl(); } else { delete d->control; d->control = 0; } if (d->control) d->control->setTextInteractionFlags(d->textInteractionFlags);}Qt::TextInteractionFlags QLabel::textInteractionFlags() const{ Q_D(const QLabel); return d->textInteractionFlags;}/*!\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) { if (d->sizePolicy == sizePolicy()) return d->msh; } ensurePolished(); d->valid_hints = true; d->sh = d->sizeForWidth(-1); // wrap ? golden ratio : min doc size QSize msh(-1, -1); if (!d->isTextLabel) { msh = d->sh; } else { msh.rheight() = d->sizeForWidth(QWIDGETSIZE_MAX).height(); // height for one line msh.rwidth() = d->sizeForWidth(0).width(); // wrap ? size of biggest word : min doc size if (d->sh.height() < msh.height()) msh.rheight() = d->sh.height(); } d->msh = msh; d->sizePolicy = sizePolicy(); return msh;}/*!\reimp*/void QLabel::mousePressEvent(QMouseEvent *ev){ Q_D(QLabel); d->sendControlEvent(ev);}/*!\reimp*/void QLabel::mouseMoveEvent(QMouseEvent *ev){ Q_D(QLabel); d->sendControlEvent(ev);}/*!\reimp*/void QLabel::mouseReleaseEvent(QMouseEvent *ev){ Q_D(QLabel); d->sendControlEvent(ev);}/*!\reimp*/void QLabel::contextMenuEvent(QContextMenuEvent *ev){#ifdef QT_NO_CONTEXTMENU Q_UNUSED(ev);#else Q_D(QLabel); if (!d->isTextLabel) { ev->ignore(); return; } QMenu *menu = d->createStandardContextMenu(ev->pos()); if (!menu) { ev->ignore(); return; } ev->accept(); menu->exec(ev->globalPos()); delete menu;#endif}/*! \reimp*/void QLabel::focusInEvent(QFocusEvent *ev){ Q_D(QLabel); if (d->isTextLabel) { d->ensureTextControl(); d->sendControlEvent(ev); } QFrame::focusInEvent(ev);}/*! \reimp*/void QLabel::focusOutEvent(QFocusEvent *ev){ Q_D(QLabel); d->sendControlEvent(ev); QFrame::focusOutEvent(ev);}/*!\reimp*/bool QLabel::focusNextPrevChild(bool next){ Q_D(QLabel); if (d->control && d->control->setFocusToNextOrPreviousAnchor(next)) return true; return QFrame::focusNextPrevChild(next);}/*!\reimp*/void QLabel::keyPressEvent(QKeyEvent *ev){ Q_D(QLabel); d->sendControlEvent(ev);}/*!\reimp*/bool QLabel::event(QEvent *e){ Q_D(QLabel); QEvent::Type type = e->type();#ifndef QT_NO_SHORTCUT if (type == QEvent::Shortcut) { QShortcutEvent *se = static_cast<QShortcutEvent *>(e); if (se->shortcutId() == d->shortcutId) { QWidget * w = d->buddy; 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; } } else#endif if (type == QEvent::Resize) { if (d->control) d->textLayoutDirty = true; } else if (e->type() == QEvent::StyleChange#ifdef Q_WS_MAC || e->type() == QEvent::MacSizeChange#endif ) { d->setLayoutItemMargins(QStyle::SE_LabelLayoutItem); d->updateLabel(); } return QFrame::event(e);}/*!\reimp*/void QLabel::paintEvent(QPaintEvent *){ Q_D(QLabel); QStyle *style = QWidget::style(); QPainter painter(this); drawFrame(&painter); QRect cr = contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); int align = QStyle::visualAlignment(layoutDirection(), QFlag(d->align));#ifndef QT_NO_MOVIE if (d->movie) { if (d->scaledcontents) style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size())); else style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap()); } else#endif if (d->isTextLabel) { QRectF lr = d->layoutRect(); if (d->control) {#ifndef QT_NO_SHORTCUT const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0); if (d->shortcutId != 0 && underline != d->shortcutCursor.charFormat().fontUnderline()) { QTextCharFormat fmt; fmt.setFontUnderline(underline); d->shortcutCursor.mergeCharFormat(fmt); }#endif d->ensureTextLayouted(); QAbstractTextDocumentLayout::PaintContext context; QStyleOption opt(0); opt.init(this); if (!isEnabled() && style->styleHint(QStyle::SH_EtchDisabledText, &opt, this)) { context.palette = palette(); context.palette.setColor(QPalette::Text, context.palette.light().color()); painter.save(); painter.translate(lr.x() + 1, lr.y() + 1); painter.setClipRect(lr.translated(-lr.x() - 1, -lr.y() - 1)); QAbstractTextDocumentLayout *layout = d->control->document()->documentLayout(); layout->draw(&painter, context); painter.restore(); } // Adjust the palette context.palette = palette(); if (foregroundRole() != QPalette::Text && isEnabled()) context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole())); painter.save(); painter.translate(lr.topLeft()); painter.setClipRect(lr.translated(-lr.x(), -lr.y())); d->control->setPalette(context.palette); d->control->drawContents(&painter, QRectF(), this); painter.restore(); } else { int flags = align; if (d->hasShortcut) { flags |= Qt::TextShowMnemonic; QStyleOption opt; opt.initFrom(this); if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this)) flags |= Qt::TextHideMnemonic; } style->drawItemText(&painter, lr.toRect(), flags, palette(), isEnabled(), d->text, foregroundRole()); } } else#ifndef QT_NO_PICTURE if (d->picture) { QRect br = d->picture->boundingRect(); int rw = br.width(); int rh = br.height(); if (d->scaledcontents) { painter.save(); painter.translate(cr.x(), cr.y()); painter.scale((double)cr.width()/rw, (double)cr.height()/rh); painter.drawPicture(-br.x(), -br.y(), *d->picture); painter.restore(); } else { int xo = 0; int yo = 0; if (align & Qt::AlignVCenter) yo = (cr.height()-rh)/2; else if (align & Qt::AlignBottom) yo = cr.height()-rh; if (align & Qt::AlignRight) xo = cr.width()-rw; else if (align & Qt::AlignHCenter) xo = (cr.width()-rw)/2; painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture); } } else#endif if (d->pixmap && !d->pixmap->isNull()) { QPixmap pix; if (d->scaledcontents) { if (!d->scaledpixmap || d->scaledpixmap->size() != cr.size()) { if (!d->cachedimage) d->cachedimage = new QImage(d->pixmap->toImage()); delete d->scaledpixmap; d->scaledpixmap = new QPixmap(QPixmap::fromImage(d->cachedimage->scaled(cr.size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation))); } pix = *d->scaledpixmap; } else pix = *d->pixmap; QStyleOption opt; opt.initFrom(this); if (!isEnabled()) pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt); style->drawItemPixmap(&painter, cr, align, pix); }}/*! Updates the label, but not the frame.*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -