Use dialogs in Qt 6
-
Hi,
I'm trying to use a
MessageDialog
in the following simple QtQuick 6.2 project, called QML_3, with CMake as the build system:import QtQuick import Qt.labs.platform Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MessageDialog { buttons: MessageDialog.Ok text: "The document has been modified." visible: true } }
When I run the project, the `Window' is shown empty without the MessageDialog! And I get this warning:
ERROR: No native MessageDialog implementation available.
Qt Labs Platform requires Qt Widgets on this setup.
Add 'QT += widgets' to .pro and create QApplication in main().So I went for adding the two lines below to the CMakeLists.txt file:
find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(appQML_3 PRIVATE Qt6::Widgets)But still the sane result!
How to solve this problem, please? -
Did you create a QApplication instead of QGuiApplication in your main.cpp?
-
Yes, I used it and the project ran, but the output is very odd! Here're my files:
main.cpp
:#include <QApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(u"qrc:/QML_3/main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
CMakeLists.txt
:cmake_minimum_required(VERSION 3.16) project(QML_3 VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Quick REQUIRED) find_package(Qt6 COMPONENTS Widgets REQUIRED) qt_add_executable(appQML_3 main.cpp ) qt_add_qml_module(appQML_3 URI QML_3 VERSION 1.0 QML_FILES main.qml ) set_target_properties(appQML_3 PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_compile_definitions(appQML_3 PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>) target_link_libraries(appQML_3 PRIVATE Qt6::Quick) target_link_libraries(appQML_3 PRIVATE Qt6::Widgets)
And this is the output! :|
Why is it this way, please!?
The element MessageDialog is still not recognized! :( -
It's not recognised by Qt Creator but it does work. I can see the window and the dialog in this screenshot. Qt Creator's QML code model will often fail to recognise stuff, don't worry about it. As long as it works at runtime, all is good.
I see the dialog lacks text which you've set, though. That might be a problem. Do you see any warnings or errors in the console output?
-
No, almost everything looks fine! I just get these in the Output Window:
00:47:51: Running steps for project QML_3...
00:47:51: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build D:/Projects/QML/QML_3/build-QML_3-Desktop_Qt_6_2_3_MinGW_64_bit-Debug --target all
[1/10 7.0/sec] Automatic MOC for target appQML_3
[2/9 9.6/sec] Running AUTOMOC file extraction for target appQML_3
[3/4 1.1/sec] Building CXX object CMakeFiles/appQML_3.dir/main.cpp.obj
[4/4 1.3/sec] Linking CXX executable appQML_3.exe
00:47:55: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited normally.
00:47:55: Elapsed time: 00:03.So what should be the reason for the window to appear this way to you, please?
That's really strange we have to do that much modification, and suffer from the output and the red line under the element just to be able to use it in the code! :| -
Sorry I don't understand anymore what the problem is.
So at runtime the program works OK, no problems, right?
You are only complaining about the editor telling you about MessageDialog being not recognised? That's just Qt Creator's code model being stupid. You can just ignore it, it's pretty common.
-
OK, I misunderstood your previous comment then.
I've never used this MessageDialog thing so it's hard for me to say. The code looks ok, maybe take a look at command line output (not compilation output you pasted above but Application Output tab in Qt Creator). Maybe there are some warnings there?
The documentation suggests this dialog is NOT available on Windows:
A native platform message dialog is currently available on the following platforms:
iOS
Android
WinRT(https://doc.qt.io/qt-5/qml-qt-labs-platform-messagedialog.html)
maybe that's the reason. Try the MessageDialog from QtQuick.Dialogs instead of Qt.labs. https://doc.qt.io/qt-5/qml-qtquick-dialogs-messagedialog.html
-
Application Output tab in Qt Creator). Maybe there are some warnings there?
No, nothing different. Normal.
maybe that's the reason
Yes, that must be.
Try the MessageDialog from QtQuick.Dialogs instead of Qt
I created a new QtQuick project using CMake and Qt version 5.15 and tested an example of that page but get this error!
QQmlApplicationEngine failed to load component
qrc:/main.qml:3:1: module "QtQuick.Dialogs" version 1.3 is not installed -
I don't know, maybe try some lower import like 1.2 or 1.1.
-
@qcoderpro said in Use dialogs in Qt 6:
qrc:/main.qml:3:1: module "QtQuick.Dialogs" version 1.3 is not installed*
If you're using Qt 6.2, then do
import QtQuick.Dialogs 6.2
.
If you're using Qt 5.15, then doimport QtQuick.Dialogs 1.3
. -
My project is using Qt 5.15 and CMake as its build system, and as stated in the prior posts, that version is tested, the error message is:
module "QtQuick.Dialogs" version 1.3 is not installedNeither Qt 5.15 nor Qt 6.2 is doing well with that element!