百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Q

QtMvvm

> 编程语言
开源

一个面向 MVVM 的 Qt 库,用于同时创建 Widgets 和 Quick 项目

360 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个面向 MVVM 的 Qt 库,用于同时创建 Widgets 和 Quick 项目

QtMvvm

A mvvm oriented library for Qt, to create Projects for Widgets and Quick Controls 2 in parallel.

For more images, check the Images page

Features

The main feature of QtMvvm is the seperation between ui and logic. With this library, you can create a core library, containing your application logic, as well as ui controllers (called "ViewModels"), and create multiple ui projects on top of it. This way you can for example provide both, a widgets and a qt quick based application, or create different uis for different devices, without having to code anything twice.

The key features are:

  • Create ViewModels in the core application to prepare data for presentation without binding to any concret GUI
    • Supports singleton ViewModels
    • Supports automatic presentation of container ViewModels
  • Functions to show messageboxes (info, warning, error, etc.) from your core app
    • Asynchronous, with result handling
    • Supports input dialogs and native file dialogs out of the box
      • Supports native file pickers on Android
    • Supports color picker dialog
    • Supports progress and busy indicator dialogs
    • custom dialog types can be created
  • Methods to create Two-Way Bindings from C++ and QML
  • Macros and a ServiceRegistry to make Dependency Injection possible for Services and ViewModels
  • A generic Presenter Interface so you can create your own custom GUI implementations
    • Widgets and Qt Quick Controls 2 are supported out of the box
  • Generic Edit-View factories to create simple edits for any kind of data from the core code
  • Supports an extensive "Settings GUI" via a simple XML format

QtMvvm Datasync

The QtMvvmDatasync modules help you to integrate QtDataSync (An easy and reliable synchronization library) into your projects. It adds ViewModels and Views to:

  • Monitor and control the connection and synchronization status
  • Manage your account and account devices
  • Easy file based import and exports, as well as a fronted for the local Network-Exchange

The Mvvm Pattern

If you don't know the Mvvm pattern already, you can read up on the links below. It's basically a clever seperation of logic (the models), presentation logic (the viewmodels) and the actual GUI (the views) that is very useful when creating applications that need to support different uis for the same data. Good links to get started:

  • https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
  • https://msdn.microsoft.com/en-us/library/hh848246.aspx

Download/Installation

  1. Package Managers: The library is available via:
    • Arch-Linux: AUR-Repository: qt5-qtmvvm
    • Ubuntu: Launchpad-PPA: ppa:skycoder42/qt-modules, package libqt5mvvm[1/-dev]
    • MacOs:
      • Tap: brew tap Skycoder42/qt-modules
      • Package: qtmvvm
      • IMPORTANT: Due to limitations of homebrew, you must run source /usr/local/opt/qtmvvm/bashrc.sh before you can use the module. Some goes for the qtdatasync dependency.
  2. Simply add my repository to your Qt MaintenanceTool (Image-based How-To here: Add custom repository):
    1. Start the MaintenanceTool from the commandline using /path/to/MaintenanceTool --addTempRepository with one of the following urls (GUI-Method is currently broken, see QTIFW-1156) - This must be done every time you start the tool:
      • On Linux: https://install.skycoder42.de/qtmodules/linux_x64
      • On Windows: https://install.skycoder42.de/qtmodules/windows_x86
      • On Mac: https://install.skycoder42.de/qtmodules/mac_x64
    2. A new entry appears under all supported Qt Versions (e.g. Qt > Qt 5.11 > Skycoder42 Qt modules)
    3. You can install either all of my modules, or select the one you need: Qt Mvvm
    4. Continue the setup and thats it! you can now use the module for all of your installed Kits for that Qt Version
  3. Download the compiled modules from the release page. Note: You will have to add the correct ones yourself and may need to adjust some paths to fit your installation! In addition to that, you will have to download the modules this one depends on as well. See Section "Requirements" below.
  4. Build it yourself! Note: This requires all build an runtime dependencies to be available (See Section "Requirements" below). If you don't have/need cmake, you can ignore the related warnings. To automatically build and install to your Qt installation, run:
    • qmake
    • make qmake_all
    • make (If you want the tests/examples/etc. run make all)
    • Optional steps:
      • make doxygen to generate the documentation
      • make lrelease to generate the translations
    • make install

Requirements

The library only has a few dependencies. The main modules only depends on qtbase and qtquick respectively. However, the Datasync extensions need QtDataSync of course.

  • Custom Qt modules:
    • QtDataSync (DataSync extensions only)
  • Additional stuff for building it yourself:
    • perl
    • qpmx
    • qpm
    • Please note that you need the QtDataSync for building the datasync modules, but if you don't have it, the build wont fail. The datasync modules will simply be skipped.

Modules

  • QtMvvmCore: The core part of QtMvvm
  • QtMvvmWidgets: The basic frontend for QtWidgets
  • QtMvvmQuick: The basic frontend for QtQuick Controls 2
  • QtMvvmDataSyncCore: The core part of the QtMvvm DataSync extension (requires QtDataSync)
  • QtMvvmDataSyncWidgets: The frontend extensions of QtMvvm DataSync for QtWidgets (requires QtDataSync)
  • QtMvvmDataSyncQuick: The frontend extensions of QtMvvm DataSync for QtQuick Controls 2 (requires QtDataSync)

Usage

The following chapters will explain how to create a QtMvvm Project and how to correctly implement applications with it. A Mvvm Project always consists of one core project, with the application logic, and one or more gui projects with the View implementations. In the following section it is explained how to use QtMvvm without going into the depths. For more details you can check the sample projects. If you want to go deeper on how the Framework works and what detailed steps are needed, check out the Documentation of the following classes:

  • CoreApp
  • ViewModel
  • ServiceRegistry
  • IPresenter

The easiest way to create a QtMvvm Project is to use the provided project template. If you did not install via a package manager or the repository, follow the steps below to add the wizard.

Add the custom wizard

If you did install the module as module you can skip this part. To create a new QtMvvm project, you can use a custom wizard for QtCreator. You will have to add it to your computer once. To do this, you will have to copy the contents of the ProjectTemplate folder to a location known by QtCreator (Pro Tip: Use Kinolien's Gitzip to download that directory only). The locations can be found here: Locating Wizards. If you are, for example, working on linux, create a new folder called QtMvvm inside of $HOME/.config/QtProject/qtcreator/templates/wizards and copy the contents there. After restarting QtCreator, the project template should appear in the Applications section of the new-dialog as QtMvvm Application Project.

Create and initialize the QtMvvm Project

Follow the setup to create the project. You can select the GUI-frontends you want to use, as well as additional features. After that you get a basic project skeleton with a simple CoreApp and a ViewModel, as well as the corresponding views.

For more Details on these classes, check the Documentation.

Important: Due to QTBUG-69963, you must always link to QtMvvmQuick when creating a QtQuick application for windows (See Issue #1). Since windows optimizes library and only links them if referenced at least once, simply add the line

QtMvvm::QuickPresenter::getInputViewFactory(); //Workaround for QTBUG-69963

to your main.cpp and it should work as expected

Adding new ViewModels and Views

The most important part is to know how to add new ViewModels and Views.

Create the ViewModel

  • Add a new c++ class to your core project. Let it inherit from QtMvvm::ViewModel
  • Make sure the Constructor has the following signature: Q_INVOKABLE MyClass(QObject *parent);
  • See examples/mvvmcore/SampleCore/sampleviewmodel.h for an example ViewModel

Create the View for QtWidgets

  • Create a new widget class in your widgets gui project.
  • In order to use it as widget for a viewmodel, you should name it accordingly: If your viewmodel is named MyCustomViewModel, the widgets name must start with MyCustom as well (e.g. MyCustomWindow, MyCustomDialog, MyCustomView, etc.)
  • Modify the constructor to look like this: Q_INVOKABLE MyCustomWindow(QtMvvm::ViewModel *viewModel, QWidget *parent = nullptr);
  • Create a member variable with your viewmodel type, e.g. MyCustomViewModel *_viewModel;
  • In the constructors implementation, cast the viewModel to your viewmodel class and assign it to _viewModel
  • As final step, you need to register the widget. This can be done by adding the line WidgetPresenter::registerView(); to your main.cpp, before calling QApplication::exec
  • See examples/mvvmwidgets/SampleWidgets/sampleview.h for an example widget

Create the quick ui

  • Optional: Register the viewmodel for qml. This way autocomplete will work. Add the line qmlRegisterUncreatableType("com.example.mvvmexample", 1, 0, "MyCustomViewModel", "ViewModels cannot be created!"); to your main cpp before creating the engine.
  • Create a new qml file inside of the folder :/qml/views (This is required to automatically find the view).
  • In order to use it as view for a viewmodel, you should name it accordingly: If your viewmodel is named MyCustomViewModel, the views name must start with MyCustom as well (e.g. MyCustomView, MyCustomActivity, etc.)
  • Add the following imports to the qml file:
qml
import QtQuick 2.10
import QtQuick.Controls 2.3
import de.skycoder42.QtMvvm.Core 1.1
import de.skycoder42.QtMvvm.Quick 1.1
import com.example.mvvmexample 1.0 //adjust to the module defined in your main.cpp
  • Add a property named viewmodel to the root element: property MyCustomViewModel viewmodel: null (If you did not register the viewmodel, use var instead of MyCustomViewModel as property type)
  • Add a presenter progress element to the root element. This way lazy loading views will show a progress: PresenterProgress {}
  • See examples/mvvmquick/SampleQuick/SampleView.qml for an example view

Understanding how QtMvvm works

The general idea is the following: You create viewmodels in your core project, which represent uis. They typically contain all the properties relevant for the ui, methods (slots) that can be called (e.g. on a button click) and signals to inform the ui about changes and other events. Thus, you can use the as ui "placeholders". Of course, They only contain the ui logic, not the actual uis.

The CoreApp is what's reponsible for managing those viewmodels. Showing a viewmodel, as well as messages (alert dialogs) are all controlled by the coreapp. The coreapp uses a so called presenters to create the

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++mvvmmvvm-patternqmlqpm

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言