Skip to content
Snippets Groups Projects
Commit e7e600d5 authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

add service object that spins new event loop with message box

parent d1bb93c4
No related branches found
No related tags found
No related merge requests found
......@@ -11,10 +11,12 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Widgets REQUIRED)
add_executable(quick-nested-event-loop
helloservice.cpp
helloservice.h
main.cpp
qml.qrc
)
......@@ -22,4 +24,7 @@ add_executable(quick-nested-event-loop
target_compile_definitions(quick-nested-event-loop
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(quick-nested-event-loop
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)
PRIVATE
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::Widgets)
#include <QMessageBox>
#include "helloservice.h"
HelloService::HelloService(QObject *parent) : QObject(parent)
{
}
void HelloService::greeting() const
{
QMessageBox dlg(QMessageBox::NoIcon, tr("Hello world!"), tr("Hello world!"));
dlg.exec();
}
#ifndef HELLOSERVICE_H
#define HELLOSERVICE_H
#include <QObject>
#include <QString>
class HelloService : public QObject
{
Q_OBJECT
public:
explicit HelloService(QObject *parent = nullptr);
Q_INVOKABLE void greeting() const;
};
#endif // HELLOSERVICE_H
#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVariant>
#include <QVector>
#include "helloservice.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QApplication app(argc, argv);
HelloService service;
QQmlApplicationEngine engine;
auto *context = engine.rootContext();
context->setContextProperties({
{QStringLiteral("serviceObject"), QVariant::fromValue(&service)},
});
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment