📄 qpushbutton.cpp
字号:
void QPushButton::setDefault(bool enable){ Q_D(QPushButton); if (d->defaultButton == enable) return; d->defaultButton = enable; if (d->defaultButton) { if (QDialog *dlg = d->dialogParent()) dlg->d_func()->setMainDefault(this); } update();#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::StateChanged);#endif}bool QPushButton::isDefault() const{ Q_D(const QPushButton); return d->defaultButton;}/*! \reimp*/QSize QPushButton::sizeHint() const{ Q_D(const QPushButton); if (d->sizeHint.isValid()) return d->sizeHint; ensurePolished(); int w = 0, h = 0; QStyleOptionButton opt; initStyleOption(&opt); // calculate contents size...#ifndef QT_NO_ICON if (!icon().isNull()) { int ih = opt.iconSize.height(); int iw = opt.iconSize.width() + 4; w += iw; h = qMax(h, ih); }#endif#ifndef QT_NO_MENU if (menu()) w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);#endif QString s(text()); bool empty = s.isEmpty(); if (empty) s = QString::fromLatin1("XXXX"); QFontMetrics fm = fontMetrics(); QSize sz = fm.size(Qt::TextShowMnemonic, s); if(!empty || !w) w += sz.width(); if(!empty || !h) h = qMax(h, sz.height()); d->sizeHint = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this). expandedTo(QApplication::globalStrut())); return d->sizeHint;}/*! \reimp */QSize QPushButton::minimumSizeHint() const{ return sizeHint();}/*!\reimp*/void QPushButton::paintEvent(QPaintEvent *){ QStylePainter p(this); QStyleOptionButton option; initStyleOption(&option); p.drawControl(QStyle::CE_PushButton, option);}/*! \reimp */void QPushButton::keyPressEvent(QKeyEvent *e){ Q_D(QPushButton); switch (e->key()) { case Qt::Key_Enter: case Qt::Key_Return: if (autoDefault() || d->defaultButton) { click(); break; } // fall through default: QAbstractButton::keyPressEvent(e); }}/*! \reimp*/void QPushButton::focusInEvent(QFocusEvent *e){ Q_D(QPushButton); if (e->reason() != Qt::PopupFocusReason && autoDefault() && !d->defaultButton) { d->defaultButton = true; QDialog *dlg = qobject_cast<QDialog*>(window()); if (dlg) dlg->d_func()->setDefault(this); } QAbstractButton::focusInEvent(e);}/*! \reimp*/void QPushButton::focusOutEvent(QFocusEvent *e){ Q_D(QPushButton); if (e->reason() != Qt::PopupFocusReason && autoDefault() && d->defaultButton) { QDialog *dlg = qobject_cast<QDialog*>(window()); if (dlg) dlg->d_func()->setDefault(0); else d->defaultButton = false; } QAbstractButton::focusOutEvent(e);#ifndef QT_NO_MENU if (d->menu && d->menu->isVisible()) // restore pressed status setDown(true);#endif}#ifndef QT_NO_MENU/*! Associates the popup menu \a menu with this push button. This turns the button into a menu button, which in some styles will produce a small triangle to the right of the button's text. Ownership of the menu is \e not transferred to the push button. \table 100% \row \o \inlineimage plastique-pushbutton-menu.png Screenshot of a Plastique style push button with popup menu. \o \inlineimage cleanlooks-pushbutton-menu.png Screenshot of a Cleanlooks style push button with popup menu. \o Push buttons with popup menus shown in the \l{Plastique Style Widget Gallery}{Plastique widget style} (left) and \l{Cleanlooks Style Widget Gallery}{Cleanlooks widget style} (right). \endtable \sa menu()*/void QPushButton::setMenu(QMenu* menu){ Q_D(QPushButton); if (menu == d->menu) return; if (menu && !d->menu) { disconnect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed())); connect(this, SIGNAL(pressed()), this, SLOT(_q_popupPressed())); } if (d->menu) removeAction(d->menu->menuAction()); d->menu = menu; if (d->menu) addAction(d->menu->menuAction()); d->resetLayoutItemMargins(); d->sizeHint = QSize(); update(); updateGeometry();}/*! Returns the button's associated popup menu or 0 if no popup menu has been set. \sa setMenu()*/QMenu* QPushButton::menu() const{ Q_D(const QPushButton); return d->menu;}/*! Shows (pops up) the associated popup menu. If there is no such menu, this function does nothing. This function does not return until the popup menu has been closed by the user.*/void QPushButton::showMenu(){ Q_D(QPushButton); if (!d || !d->menu) return; setDown(true); d->_q_popupPressed();}void QPushButtonPrivate::_q_popupPressed(){ Q_Q(QPushButton); if (!down || !menu) return; menu->setNoReplayFor(q); bool horizontal = true;#if !defined(QT_NO_TOOLBAR) QToolBar *tb = qobject_cast<QToolBar*>(q->parentWidget()); if (tb && tb->orientation() == Qt::Vertical) horizontal = false;#endif QWidgetItem item(q); QRect rect = item.geometry(); rect.setRect(rect.x() - q->x(), rect.y() - q->y(), rect.width(), rect.height()); QSize menuSize = menu->sizeHint(); QPoint globalPos = q->mapToGlobal(rect.topLeft()); int x = globalPos.x(); int y = globalPos.y(); if (horizontal) { if (globalPos.y() + rect.height() + menuSize.height() <= qApp->desktop()->height()) { y += rect.height(); } else { y -= menuSize.height(); } if (q->layoutDirection() == Qt::RightToLeft) x += rect.width() - menuSize.width(); } else { if (globalPos.x() + rect.width() + menu->sizeHint().width() <= qApp->desktop()->width()) x += rect.width(); else x -= menuSize.width(); } QPointer<QPushButton> guard(q); //Because of a delay in menu effects, we must keep track of the //menu visibility to avoid flicker on button release menuOpen = true; menu->exec(QPoint(x, y)); if (guard) { menuOpen = false; q->setDown(false); }}#endif // QT_NO_MENUvoid QPushButtonPrivate::resetLayoutItemMargins(){ Q_Q(QPushButton); QStyleOptionButton opt; q->initStyleOption(&opt); setLayoutItemMargins(QStyle::SE_PushButtonLayoutItem, &opt);}void QPushButton::setFlat(bool flat){ Q_D(QPushButton); if (d->flat == flat) return; d->flat = flat; d->resetLayoutItemMargins(); d->sizeHint = QSize(); update(); updateGeometry();}bool QPushButton::isFlat() const{ Q_D(const QPushButton); return d->flat;}/*! \reimp */bool QPushButton::event(QEvent *e){ Q_D(QPushButton); if (e->type() == QEvent::ParentChange) { if (QDialog *dialog = d->dialogParent()) { if (d->defaultButton) dialog->d_func()->setMainDefault(this); } } else if (e->type() == QEvent::StyleChange#ifdef Q_WS_MAC || e->type() == QEvent::MacSizeChange#endif ) { d->resetLayoutItemMargins(); updateGeometry(); } return QAbstractButton::event(e);}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QPushButton::QPushButton(QWidget *parent, const char *name) : QAbstractButton(*new QPushButtonPrivate, parent){ Q_D(QPushButton); setObjectName(QString::fromAscii(name)); d->init();}/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QPushButton::QPushButton(const QString &text, QWidget *parent, const char *name) : QAbstractButton(*new QPushButtonPrivate, parent){ Q_D(QPushButton); setObjectName(QString::fromAscii(name)); setText(text); d->init();}/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QPushButton::QPushButton(const QIcon& icon, const QString &text, QWidget *parent, const char *name) : QAbstractButton(*new QPushButtonPrivate, parent){ Q_D(QPushButton); setObjectName(QString::fromAscii(name)); setText(text); setIcon(icon); d->init();}#endif/*! \fn void QPushButton::openPopup() Use showMenu() instead.*//*! \fn bool QPushButton::isMenuButton() const Use menu() != 0 instead.*//*! \fn void QPushButton::setPopup(QMenu* popup) Use setMenu() instead.*//*! \fn QMenu* QPushButton::popup() const Use menu() instead.*/#include "moc_qpushbutton.cpp"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -