A powerful code generator for Qt C++ classes that creates boilerplate code from simple declarative files.
QObject Class Generator simplifies Qt development by generating C++ header and implementation files from lightweight .qobj definition files. Instead of writing repetitive boilerplate code for Qt properties, signals, and slots, you can define your classes declaratively and let QObject Class Generator handle the rest.
- 🚀 Fast Development: Generate complete Qt classes from simple definitions
- 📝 Declarative Syntax: Clean, readable
.qobjfile format - 🎯 Multiple Templates: Support for QObject, QAbstractListModel, and QSettings-based classes
- 🔧 Customizable: Flexible template system using Jinja2
- Python 3.6 or higher
- Jinja2 template engine
pip install jinja2git clone https://github.com/yourusername/qobjgen.git
cd qobjgen- Create a
.qobjfile describing your Qt class:
class Person
prop QString name RWN
prop int age RWN
prop QString email RWN
- Generate the C++ files:
python qobjgen.py person.qobj -o output/- Use the generated files in your Qt project:
person.qobj.h- Header file with class declarationperson.qobj.cpp- Implementation file with getters, setters, and signals
python qobjgen.py [OPTIONS] QOBJFILE [QOBJFILE ...]| Option | Description |
|---|---|
--tpldir TPLDIR |
Custom template directory |
--nosuffix |
Don't add .qobj suffix to output files |
--suffix SUFFIX |
Custom suffix for output files (default: .qobj) |
-f, --force |
Overwrite existing files |
-o OUTDIR |
Output directory for generated files |
-h, --help |
Show help message |
# Generate files with default settings
python qobjgen.py examples/message.qobj
# Generate multiple files to specific directory
python qobjgen.py examples/*.qobj -o output/
# Use custom suffix and overwrite existing files
python qobjgen.py person.qobj --suffix .gen -f -o src/# Comments start with #
class ClassName [BaseClass]
prop PropertyType propertyName [Access] [key1 value1 key2 value2 ...]
tpl templateName
var variableName value
class MyClass # Inherits from QObject (default)
class MyClass QWidget # Inherits from QWidget
Properties support different access modes:
R- Read-only (generates getter and signal)W- Write-only (generates setter)N- Notify (generates change signal)RW- Read-Write (default if not specified)RWN- Read-Write with notification
prop int id R # Read-only
prop QString name RW # Read-Write
prop bool enabled RWN # Read-Write with notifications
prop double value W # Write-only
Choose different code generation templates:
tpl qobject # Standard QObject (default)
tpl listmodel # QAbstractListModel subclass
tpl settings # QSettings-based configuration class
Define template variables for customization:
var section main # For settings template
var namespace MyApp # Custom namespace
class Person
prop QString firstName RWN
prop QString lastName RWN
prop int age RWN
prop QString email RWN
Generates a Qt class with:
- Properties with getters, setters, and change signals
- Q_PROPERTY macros for QML integration
- Proper const-correctness
class AppSettings
tpl settings
var section main
prop bool debug R qvartype Bool default false
prop QString host qvartype String section network default "localhost"
prop QString username qvartype String section auth
prop QString password qvartype String section auth
Generates a QSettings-based configuration class with:
- Automatic loading/saving to settings file
- Section-based organization
- Default values
- Type-safe property access
class MessagesModel
tpl listmodel
prop QList<Message*> messages RWN
Generates a QAbstractListModel subclass for use with QML ListView.
QObject Class Generator uses Jinja2 templates located in the templates/ directory:
qobject_h.tpl/qobject_cpp.tpl- Standard QObject classeslistmodel_h.tpl/listmodel_cpp.tpl- List model classessettings_h.tpl/settings_cpp.tpl- Settings classes
You can create custom templates and use them with --tpldir:
python qobjgen.py myclass.qobj --tpldir /path/to/custom/templates/- Qt Properties: Full Q_PROPERTY declarations for QML binding
- Getters/Setters: Const-correct getters and type-safe setters
- Signals: Change notification signals for reactive programming
- Memory Safe: Proper RAII and Qt parent-child relationships
- QML Ready: Properties automatically exposed to QML
This project is licensed under the MIT License - see the LICENSE file for details.