📄 qfontdialog.cpp
字号:
QFont font = QFontDialog::getFont(&ok, this); if (ok) { // font is set to the font the user selected } else { // the user canceled the dialog; font is set to the default // application font, QApplication::font() } \endcode*/QFont QFontDialog::getFont(bool *ok, QWidget *parent){ return QFontDialogPrivate::getFont(ok, 0, parent);}QFont QFontDialogPrivate::getFont(bool *ok, const QFont *def, QWidget *parent, const QString &caption){ QFont result; if (def) result = *def; QFontDialog *dlg = new QFontDialog(parent, true); dlg->setFont((def ? *def : QFont())); dlg->setWindowTitle(!caption.isEmpty() ? caption : QFontDialog::tr("Select Font")); bool res = (dlg->exec() == QDialog::Accepted); if (res) result = dlg->font(); if (ok) *ok = res; delete dlg; return result;}/*! \internal ### Qt5 - remove me*/QFont QFontDialog::getFont(bool *ok, const QFont *def, QWidget* parent){ return getFont(ok, *def, parent, QString());}/*! \internal An event filter to make the Up, Down, PageUp and PageDown keys work correctly in the line edits. The source of the event is the object \a o and the event is \a e.*/bool QFontDialog::eventFilter(QObject * o , QEvent * e){ Q_D(QFontDialog); if (e->type() == QEvent::KeyPress) { QKeyEvent * k = (QKeyEvent *)e; if (o == d->sizeEdit && (k->key() == Qt::Key_Up || k->key() == Qt::Key_Down || k->key() == Qt::Key_PageUp || k->key() == Qt::Key_PageDown)) { int ci = d->sizeList->currentItem(); (void)QApplication::sendEvent(d->sizeList, k); if (ci != d->sizeList->currentItem() && style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this)) d->sizeEdit->selectAll(); return true; } else if ((o == d->familyList || o == d->styleList) && (k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)) { k->accept(); accept(); return true; } } else if (e->type() == QEvent::FocusIn && style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this)) { if (o == d->familyList) d->familyEdit->selectAll(); else if (o == d->styleList) d->styleEdit->selectAll(); else if (o == d->sizeList) d->sizeEdit->selectAll(); } else if (e->type() == QEvent::MouseButtonPress && o == d->sizeList) { d->sizeEdit->setFocus(); } return QDialog::eventFilter(o, e);}/*! \internal Updates the contents of the "font family" list box. This function can be reimplemented if you have special requirements.*/void QFontDialog::updateFamilies(){ Q_D(QFontDialog); d->familyList->blockSignals(true); enum match_t { MATCH_NONE=0, MATCH_LAST_RESORT=1, MATCH_APP=2, MATCH_FALLBACK, MATCH_FAMILY=3 }; QStringList familyNames = d->fdb.families(d->writingSystem); d->familyList->model()->setStringList(familyNames); QString foundryName1, familyName1, foundryName2, familyName2; int bestFamilyMatch = -1; match_t bestFamilyType = MATCH_NONE; QFont f; // ##### do the right thing for a list of family names in the font. QFontDatabase::parseFontName(d->family, foundryName1, familyName1); QStringList::Iterator it = familyNames.begin(); int i = 0; for(; it != familyNames.end(); ++it, ++i) { QFontDatabase::parseFontName(*it, foundryName2, familyName2); //try to match.. if (familyName1 == familyName2) { bestFamilyType = MATCH_FAMILY; if (foundryName1 == foundryName2) { bestFamilyMatch = i; break; } if (bestFamilyMatch < MATCH_FAMILY) bestFamilyMatch = i; } //and try some fall backs match_t type = MATCH_NONE; if (bestFamilyType <= MATCH_NONE && familyName2 == f.lastResortFamily()) type = MATCH_LAST_RESORT; if (bestFamilyType <= MATCH_LAST_RESORT && familyName2 == f.family()) type = MATCH_APP; // ### add fallback for writingSystem if (type != MATCH_NONE) { bestFamilyType = type; bestFamilyMatch = i; } } if (i != -1 && bestFamilyType != MATCH_NONE) d->familyList->setCurrentItem(bestFamilyMatch); else d->familyList->setCurrentItem(0); d->familyEdit->setText(d->familyList->currentText()); if (style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this) && d->familyList->hasFocus()) d->familyEdit->selectAll(); d->familyList->blockSignals(false); updateStyles();}/*! \internal Updates the contents of the "font style" list box. This function can be reimplemented if you have special requirements.*/void QFontDialog::updateStyles(){ Q_D(QFontDialog); d->styleList->blockSignals(true); QStringList styles = d->fdb.styles(d->familyList->currentText()); d->styleList->model()->setStringList(styles); if (styles.isEmpty()) { d->styleEdit->clear(); d->smoothScalable = false; } else { if (!d->style.isEmpty()) { bool found = false; bool first = true; QString cstyle = d->style; redo: for ( int i = 0 ; i < (int)d->styleList->count() ; i++ ) { if ( cstyle == d->styleList->text(i) ) { d->styleList->setCurrentItem( i ); found = true; break; } } if (!found && first) { if (cstyle.contains(QLatin1String("Italic"))) { cstyle.replace(QLatin1String("Italic"), QLatin1String("Oblique")); first = false; goto redo; } else if (cstyle.contains(QLatin1String("Oblique"))) { cstyle.replace(QLatin1String("Oblique"), QLatin1String("Italic")); first = false; goto redo; } } if (!found) d->styleList->setCurrentItem(0); } d->styleEdit->setText(d->styleList->currentText()); if (style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this) && d->styleList->hasFocus()) d->styleEdit->selectAll(); d->smoothScalable = d->fdb.isSmoothlyScalable(d->familyList->currentText(), d->styleList->currentText()); } d->styleList->blockSignals(false); updateSizes();}/*! \internal Updates the contents of the "font size" list box. This function can be reimplemented if you have special requirements.*/void QFontDialog::updateSizes(){ Q_D(QFontDialog); d->sizeList->blockSignals(true); if (!d->familyList->currentText().isEmpty()) { QList<int> sizes = d->fdb.pointSizes(d->familyList->currentText(), d->styleList->currentText()); int i = 0; int current = -1; QStringList str_sizes; for(QList<int>::const_iterator it = sizes.constBegin() ; it != sizes.constEnd(); ++it) { str_sizes.append(QString::number(*it)); if (current == -1 && *it >= d->size) current = i; ++i; } d->sizeList->model()->setStringList(str_sizes); if (current == -1) // we request a size bigger than the ones in the list, select the biggest one current = d->sizeList->count() - 1; d->sizeList->setCurrentItem(current); d->sizeEdit->blockSignals(true); d->sizeEdit->setText((d->smoothScalable ? QString::number(d->size) : d->sizeList->currentText())); if (style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, this) && d->sizeList->hasFocus()) d->sizeEdit->selectAll(); d->sizeEdit->blockSignals(false); } else { d->sizeEdit->clear(); } d->sizeList->blockSignals(false); d->_q_updateSample();}void QFontDialogPrivate::_q_updateSample(){ Q_Q(QFontDialog); if (familyList->currentText().isEmpty()) sampleEdit->clear(); else sampleEdit->setFont(q->font());}/*! \internal*/void QFontDialogPrivate::_q_writingSystemHighlighted(int index){ Q_Q(QFontDialog); writingSystem = QFontDatabase::WritingSystem(index); sampleEdit->setText(fdb.writingSystemSample(writingSystem)); q->updateFamilies();}/*! \internal*/void QFontDialogPrivate::_q_familyHighlighted(int i){ Q_Q(QFontDialog); family = familyList->text(i); familyEdit->setText(family); if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) && familyList->hasFocus()) familyEdit->selectAll(); q->updateStyles();}/*! \internal*/void QFontDialogPrivate::_q_styleHighlighted(int index){ Q_Q(QFontDialog); QString s = styleList->text(index); styleEdit->setText(s); if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) && styleList->hasFocus()) styleEdit->selectAll(); style = s; q->updateSizes();}/*! \internal*/void QFontDialogPrivate::_q_sizeHighlighted(int index){ Q_Q(QFontDialog); QString s = sizeList->text(index); sizeEdit->setText(s); if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q) && sizeEdit->hasFocus()) sizeEdit->selectAll(); size = s.toInt(); _q_updateSample();}/*! \internal This slot is called if the user changes the font size. The size is passed in the \a s argument as a \e string.*/void QFontDialogPrivate::_q_sizeChanged(const QString &s){ // no need to check if the conversion is valid, since we have an QIntValidator in the size edit int size = s.toInt(); if (this->size == size) return; this->size = size; if (sizeList->count() != 0) { int i; for (i = 0 ; i < sizeList->count() - 1 ; i++) { if (sizeList->text(i).toInt() >= this->size) break; } sizeList->blockSignals(true); sizeList->setCurrentItem(i); sizeList->blockSignals(false); } _q_updateSample();}void QFontDialogPrivate::retranslateStrings(){ familyAccel->setText(QFontDialog::tr("&Font")); styleAccel->setText(QFontDialog::tr("Font st&yle")); sizeAccel->setText(QFontDialog::tr("&Size")); effects->setTitle(QFontDialog::tr("Effects")); strikeout->setText(QFontDialog::tr("Stri&keout")); underline->setText(QFontDialog::tr("&Underline")); sample->setTitle(QFontDialog::tr("Sample")); writingSystemAccel->setText(QFontDialog::tr("Wr&iting System"));}/*! \reimp*/void QFontDialog::changeEvent(QEvent *e){ Q_D(QFontDialog); if (e->type() == QEvent::LanguageChange) { d->retranslateStrings(); } QDialog::changeEvent(e);}/*! \internal Sets the font highlighted in the QFontDialog to font \a f. \sa font()*/void QFontDialog::setFont(const QFont &f){ Q_D(QFontDialog); d->family = f.family(); d->style = d->fdb.styleString(f); d->size = f.pointSize(); if (d->size == -1) { QFontInfo fi(f); d->size = fi.pointSize(); } d->strikeout->setChecked(f.strikeOut()); d->underline->setChecked(f.underline()); updateFamilies();}/*! \internal Returns the font which the user has chosen. \sa setFont()*/QFont QFontDialog::font() const{ Q_D(const QFontDialog); int pSize = d->sizeEdit->text().toInt(); QFont f = d->fdb.font(d->familyList->currentText(), d->style, pSize); f.setStrikeOut(d->strikeout->isChecked()); f.setUnderline(d->underline->isChecked()); return f;}/*! \fn QFont QFontDialog::getFont(bool *ok, const QFont &def, QWidget* parent, const char* name) Call getFont(\a ok, \a def, \a parent) instead. The \a name parameter is ignored.*//*! \fn QFont QFontDialog::getFont(bool *ok, QWidget* parent, const char* name) Call getFont(\a ok, \a parent) instead. The \a name parameter is ignored.*/#include "qfontdialog.moc"#include "moc_qfontdialog.cpp"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -