When developing desktop applications with Qt, we often need to provide users with a way to select fonts dynamically. This is especially useful in text editing applications, style customizers, or any other scenario where typography customization is a feature. This tutorial explains how to display font families in dropdown list in Qt 6.
The provided code shows how to create a simple Qt application that displays a dropdown list of all available font families. A QComboBox
is populated with the list of font families retrieved with QFontDatabase::families
, which queries the system's font database.
#include <QApplication>
#include <QComboBox>
#include <QVBoxLayout>
#include <QFontDatabase>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
auto *window = new QWidget();
auto *layout = new QVBoxLayout(window);
auto *comboBox = new QComboBox();
comboBox->addItems(QFontDatabase::families());
layout->addWidget(comboBox);
window->show();
return QApplication::exec();
}
Here's how the application might look:
Leave a Comment
Cancel reply