📄 qlabel.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 "qlabel.h"#include "qpainter.h"#include "qevent.h"#include "qdrawutil.h"#include "qmovie.h"#include "qimage.h"#include "qbitmap.h"#include "qpicture.h"#include "qapplication.h"#include "qtextdocument.h"#include "qabstractbutton.h"#include "qstyle.h"#include "qstyleoption.h"#include "qframe_p.h"#include <limits.h>#include "../text/qtextdocumentlayout_p.h"class QLabelPrivate : public QFramePrivate{ Q_DECLARE_PUBLIC(QLabel)public: QLabelPrivate() : img(0), pix(0), valid_hints(false), margin(0) {} void init(); void clearContents(); void updateLabel(); QSize sizeForWidth(int w) const; QImage* img; // for scaled contents QPixmap* pix; // for scaled contents mutable QSize sh; mutable QSize msh; mutable bool valid_hints; int margin; QString ltext; QPixmap *lpixmap;#ifndef QT_NO_PICTURE QPicture *lpicture;#endif#ifndef QT_NO_MOVIE QMovie *lmovie; void _q_movieUpdated(const QRect&); void _q_movieResized(const QSize&);#endif QPointer<QWidget> lbuddy; int shortcutId; ushort align; short extraMargin; uint scaledcontents :1; Qt::TextFormat textformat; QTextDocument* doc;};/*! \class QLabel \brief The QLabel widget provides a text or image display. \ingroup basic \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->lpicture;}#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::WFlags 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::WFlags 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::WFlags 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::WFlags 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, \&), and the text is in plain text format, 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::WFlags f) : QFrame(*new QLabelPrivate(), parent, f){ Q_D(QLabel); if (name) setObjectName(QString::fromAscii(name)); d->init(); setBuddy(buddy); setText(text);}#endif //QT3_SUPPORT/*! Destroys the label.*/QLabel::~QLabel(){ Q_D(QLabel); d->clearContents();}void QLabelPrivate::init(){ Q_Q(QLabel); lpixmap = 0;#ifndef QT_NO_MOVIE lmovie = 0;#endif shortcutId = 0; lpixmap = 0;#ifndef QT_NO_PICTURE lpicture = 0;#endif align = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs; extraMargin = -1; scaledcontents = false; textformat = Qt::AutoText; doc = 0; q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));}/*! \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 the text is interpreted as a plain text and a buddy has been set, the buddy mnemonic key is updated from the new text. The label resizes itself if auto-resizing is enabled. 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 scrollbar when necessary. \sa setTextFormat(), setBuddy(), alignment*/void QLabel::setText(const QString &text){ Q_D(QLabel); if (d->ltext == text) return; d->clearContents(); d->ltext = text; if (d->textformat == Qt::RichText || ((d->textformat == Qt::AutoText) && Qt::mightBeRichText(d->ltext))) { if (!d->doc) d->doc = new QTextDocument(); d->doc->setUndoRedoEnabled(false); d->doc->setDefaultFont(font()); d->doc->setHtml(d->ltext); } d->updateLabel();}QString QLabel::text() const{ Q_D(const QLabel); return d->ltext;}/*! 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 an invalid pixmap. 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->lpixmap || d->lpixmap->serialNumber() != pixmap.serialNumber()) { d->clearContents(); d->lpixmap = new QPixmap(pixmap); } if (d->lpixmap->depth() == 1 && !d->lpixmap->mask()) d->lpixmap->setMask(*((QBitmap *)d->lpixmap)); d->updateLabel();}const QPixmap *QLabel::pixmap() const{ Q_D(const QLabel); return d->lpixmap;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -