📄 qlabel.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpainter.h"#include "qevent.h"#include "qdrawutil.h"#include "qapplication.h"#include "qabstractbutton.h"#include "qstyle.h"#include "qstyleoption.h"#include <limits.h>#include "qaction.h"#include "qclipboard.h"#include <qdebug.h>#include <qurl.h>#include "qlabel_p.h"/*! \class QLabel \brief The QLabel widget provides a text or image display. \ingroup basicwidgets \ingroup text \mainclass 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: \table \header \o Content \o Setting \row \o Plain text \o Pass a QString to setText(). \row \o Rich text \o Pass a QString that contains rich text to setText(). \row \o A pixmap \o Pass a QPixmap to setPixmap(). \row \o A movie \o Pass a QMovie to setMovie(). \row \o A number \o Pass an \e int or a \e double to setNum(), which converts the number to plain text. \row \o Nothing \o The same as an empty plain text. This is the default. Set by clear(). \endtable When the content is changed using any of these functions, any previous content is cleared. The look of a QLabel can be tuned in several ways. All the settings of QFrame are available for specifying a widget frame. The positioning of the content within the QLabel widget area can be tuned with setAlignment() and setIndent(). Text content can also wrap lines along word bounderies with setWordWrap(). For example, this code sets up a sunken panel with a two-line text in the bottom right corner (both lines being flush with the right side of the label): \code QLabel *label = new QLabel(this); label->setFrameStyle(QFrame::Panel | QFrame::Sunken); label->setText("first line\nsecond line"); label->setAlignment(Qt::AlignBottom | Qt::AlignRight); \endcode 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: \code QLineEdit* phoneEdit = new QLineEdit(this); QLabel* phoneLabel = new QLabel("&Phone:", this); phoneLabel->setBuddy(phoneEdit); \endcode In this example, keyboard focus is transferred to the label's buddy (the QLineEdit) when the user presses Alt+P. If the buddy was a button (inheriting from QAbstractButton), triggering the mnemonic would emulate a button click. \table 100% \row \o \inlineimage macintosh-label.png Screenshot of a Macintosh style label \o A label shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. \row \o \inlineimage plastique-label.png Screenshot of a Plastique style label \o A label shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}. \row \o \inlineimage windowsxp-label.png Screenshot of a Windows XP style label \o A label shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}. \endtable \sa QLineEdit, QTextEdit, QPixmap, QMovie, {fowler}{GUI Design Handbook: Label}*/#ifndef QT_NO_PICTURE/*! Returns the label's picture or 0 if the label doesn't have a picture.*/const QPicture *QLabel::picture() const{ Q_D(const QLabel); return d->picture;}#endif/*! Constructs an empty label. The \a parent and widget flag \a f, arguments are passed to the QFrame constructor. \sa setAlignment(), setFrameStyle(), setIndent()*/QLabel::QLabel(QWidget *parent, Qt::WindowFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); d->init();}/*! Constructs a label that displays the text, \a text. The \a parent and widget flag \a f, arguments are passed to the QFrame constructor. \sa setText(), setAlignment(), setFrameStyle(), setIndent()*/QLabel::QLabel(const QString &text, QWidget *parent, Qt::WindowFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); d->init(); setText(text);}#ifdef QT3_SUPPORT/*! \obsolete Constructs an empty label. The \a parent, \a name and widget flag \a f, arguments are passed to the QFrame constructor. \sa setAlignment(), setFrameStyle(), setIndent()*/QLabel::QLabel(QWidget *parent, const char *name, Qt::WindowFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); if (name) setObjectName(QString::fromAscii(name)); d->init();}/*! \obsolete Constructs a label that displays the text, \a text. The \a parent, \a name and widget flag \a f, arguments are passed to the QFrame constructor. \sa setText(), setAlignment(), setFrameStyle(), setIndent()*/QLabel::QLabel(const QString &text, QWidget *parent, const char *name, Qt::WindowFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); if (name) setObjectName(QString::fromAscii(name)); d->init(); setText(text);}/*! \obsolete Constructs a label that displays the text \a text. The label has a buddy widget, \a buddy. If the \a text contains an underlined letter (a letter preceded by an ampersand, \&), when the user presses Alt+ the underlined letter, focus is passed to the buddy widget. The \a parent, \a name and widget flag, \a f, arguments are passed to the QFrame constructor. \sa setText(), setBuddy(), setAlignment(), setFrameStyle(), setIndent()*/QLabel::QLabel(QWidget *buddy, const QString &text, QWidget *parent, const char *name, Qt::WindowFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); if (name) setObjectName(QString::fromAscii(name)); d->init();#ifndef QT_NO_SHORTCUT setBuddy(buddy);#endif setText(text);}#endif //QT3_SUPPORT/*! Destroys the label.*/QLabel::~QLabel(){ Q_D(QLabel); d->clearContents();}void QLabelPrivate::init(){ Q_Q(QLabel); valid_hints = false; margin = 0;#ifndef QT_NO_MOVIE movie = 0;#endif#ifndef QT_NO_SHORTCUT shortcutId = 0;#endif pixmap = 0; scaledpixmap = 0; cachedimage = 0;#ifndef QT_NO_PICTURE picture = 0;#endif align = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs; indent = -1; scaledcontents = false; textLayoutDirty = false; textDirty = false; textformat = Qt::AutoText; control = 0; textInteractionFlags = Qt::LinksAccessibleByMouse; isRichText = false; isTextLabel = false; q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::Label));#ifndef QT_NO_CURSOR validCursor = false; onAnchor = false;#endif openExternalLinks = false; setLayoutItemMargins(QStyle::SE_LabelLayoutItem);}/*! \property QLabel::text \brief the label's text If no text has been set this will return an empty string. Setting the text clears any previous content. The text will be interpreted either as plain text or as rich text, depending on the text format setting; see setTextFormat(). The default setting is Qt::AutoText; i.e. QLabel will try to auto-detect the format of the text set. If a buddy has been set, the buddy mnemonic key is updated from the new text. Note that 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. \note This function enables mouse tracking if \a text contains rich text. \sa setTextFormat(), setBuddy(), alignment*/void QLabel::setText(const QString &text){ Q_D(QLabel); if (d->text == text) return; QTextControl *oldControl = d->control; d->control = 0; d->clearContents(); d->text = text; d->isTextLabel = true; d->textDirty = true; d->isRichText = d->textformat == Qt::RichText || (d->textformat == Qt::AutoText && Qt::mightBeRichText(d->text)); d->control = oldControl; if (d->needTextControl()) { d->ensureTextControl(); } else { delete d->control; d->control = 0; } if (d->isRichText) { setMouseTracking(true); } else { // Note: mouse tracking not disabled intentionally }#ifndef QT_NO_SHORTCUT if (d->buddy) d->updateShortcut();#endif d->updateLabel();}QString QLabel::text() const{ Q_D(const QLabel); return d->text;}/*! Clears any label contents.*/void QLabel::clear(){ Q_D(QLabel); d->clearContents(); d->updateLabel();}/*! \property QLabel::pixmap \brief 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.*/void QLabel::setPixmap(const QPixmap &pixmap){ Q_D(QLabel); if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { d->clearContents(); d->pixmap = new QPixmap(pixmap); } if (d->pixmap->depth() == 1 && !d->pixmap->mask()) d->pixmap->setMask(*((QBitmap *)d->pixmap)); d->updateLabel();}const QPixmap *QLabel::pixmap() const{ Q_D(const QLabel); return d->pixmap;}#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->picture = 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. \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. \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); 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -