Unlocking Standard Font Sizes for Your Qt GUI: A Guide to QFontDatabase::standardSizes()
Purpose
- This static function retrieves a list of standard font sizes available on the system. These are common point sizes frequently used in user interfaces (UIs).
Return Value
- A
QList<int>
containing the standard font sizes in points. The list might be empty if no standard sizes are found.
Functionality
- The exact set of standard sizes may vary depending on the operating system and font configuration. Common examples include 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, and 96 points.
standardSizes()
specifically queries the font database for predefined standard sizes.QFontDatabase
provides access to information about fonts installed on the system.
Usage
#include <QtGui/QFontDatabase>
Call the function
QList<int> standardSizes = QFontDatabase::standardSizes();
Process the list
- Iterate through the
standardSizes
list to access individual font sizes. - You can use these sizes when creating
QFont
objects to specify desired font sizes for your UI elements.
- Iterate through the
Example
#include <QApplication>
#include <QLabel>
#include <QtGui/QFontDatabase>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Get standard font sizes
QList<int> standardSizes = QFontDatabase::standardSizes();
// Create a label with a standard font size
QLabel label("Standard Font Size Example");
if (!standardSizes.isEmpty()) {
label.setFont(QFont("Arial", standardSizes.first())); // Use the first standard size
}
label.show();
return app.exec();
}
Key Points
- For more granular control over font sizes, you can create custom values within a reasonable range.
- Consider using
smoothSizes()
if you need anti-aliased (smoother) fonts on high-resolution displays. - Use
standardSizes()
in conjunction with otherQFontDatabase
functions likefamilies()
to explore available fonts and their properties.
Selecting a Standard Size from User Choice
#include <QApplication>
#include <QComboBox>
#include <QLabel>
#include <QtGui/QFontDatabase>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Get standard font sizes
QList<int> standardSizes = QFontDatabase::standardSizes();
// Create a combo box to select a size
QComboBox comboBox("Select Font Size");
comboBox.addItems(QStringList::fromList(standardSizes));
// Create a label to display the chosen font size
QLabel label("Selected Font Size:");
// Connect the combo box selection change signal to update the label
QObject::connect(&comboBox, SIGNAL(currentIndexChanged(int)), &label, SLOT(setNum(int)));
// Initial label display (optional)
if (!standardSizes.isEmpty()) {
label.setNum(standardSizes.first());
}
// Layout (replace with your preferred layout management)
QVBoxLayout layout;
layout.addWidget(&comboBox);
layout.addWidget(&label);
QWidget window;
window.setLayout(&layout);
window.setWindowTitle("Font Size Selection");
window.show();
return app.exec();
}
In this example, the user can select a standard font size from a combo box, and the chosen size is displayed in the label.
Using a Range of Standard Sizes
#include <QApplication>
#include <QLabel>
#include <QtGui/QFontDatabase>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// Get standard font sizes
QList<int> standardSizes = QFontDatabase::standardSizes();
// Create labels with a range of standard sizes (modify start/end indexes as needed)
for (int size : standardSizes.mid(3, 5)) { // Use a subset of standard sizes (indices 3 to 7)
QLabel* label = new QLabel(QString("Font Size: %1").arg(size));
label->setFont(QFont("Arial", size));
label->show();
}
return app.exec();
}
This code creates multiple labels, each with a different font size from a specified range of standard sizes.
Custom Font Sizes
- However, ensure the chosen sizes are visually appealing and align with your UI design principles.
- You can directly specify custom font sizes in points when creating
QFont
objects. This provides complete control over the font size, allowing you to use values outside the standard set.
Example
QFont myFont("Arial", 13); // Custom font size of 13 points
System Metrics
- For example, you could set the font size to be a certain percentage of the screen height.
- Qt provides access to system metrics through the
QApplication::desktop()->screenGeometry()
function. This can be used to calculate font sizes relative to the screen resolution.
Example
int screenHeight = QApplication::desktop()->screenGeometry().height();
int fontSize = screenHeight * 0.02; // 2% of screen height
QFont myFont("Arial", fontSize);
UI Design Guidelines
- Many UI design frameworks and libraries have established font size guidelines for different UI elements like headings, body text, buttons, etc. You can leverage these guidelines to create a consistent and visually appealing user interface.
- Following established UI design guidelines ensures a consistent and well-structured user interface.
- If you require more granular control or want to dynamically adjust font sizes based on screen size, consider custom sizes or system metrics.
- If you need the flexibility of using predefined standard sizes,
QFontDatabase::standardSizes()
is a good choice.