Is allow to use the QDrag class in Qt Webassembly?
Unsolved
Qt for WebAssembly
-
Application is hang whenever it is reach to line
drag->exec(Qt::CopyAction | Qt::MoveAction);
It is work fine with mingw, msvc compiler.
It is not work in brower.
Using:
Qt 6.5.1
emsdk 3.1.25#include "ElementDragEventHandler.h" #include "AbstractElement.h" #include <qevent.h> #include <qlabel.h> #include <qdrag.h> #include <qmimedata.h> bool ElementDragEventHandler::eventFilter(QObject* watched, QEvent* event) { if (event->type() == QEvent::MouseButtonPress) { QWidget* label = qobject_cast<QWidget*>(watched); if (label && label->property("element").isValid()) { // Get the element data stored in the label AbstractElement* element = qvariant_cast<AbstractElement*>(label->property("element")); // Create a drag object QDrag* drag = new QDrag(label); QMimeData* mimeData = new QMimeData; // Set the element as the mime data mimeData->setProperty("element", QVariant::fromValue(element)); drag->setMimeData(mimeData); // Set the drag pixmap QPixmap pixmap = element->getImage().scaled(90, 90); // Adjust the size as needed drag->setPixmap(pixmap); // Start the drag drag->exec(Qt::CopyAction | Qt::MoveAction); return true; } } return QObject::eventFilter(watched, event); }
anything I need to change.
# Define the project name and version TEMPLATE = app TARGET = myApp VERSION = 0.1.0 # Define the target platform and architecture QMAKE_TARGET.arch = wasm # Set the C++ version and compiler flags CONFIG += c++17 QMAKE_CXXFLAGS += -std=c++17 # Enable precompiled headers #PRECOMPILED_HEADER = $$PWD/src/pch.h # Define the source files SOURCES += $$PWD/src/main.cpp ...... # Define the header files HEADERS += $$PWD/src/MainWindow.h .... # Define the resource files RESOURCES += $$PWD/src/resources.qrc # Include the required Qt modules QT += core gui widgets # WebAssembly-specific settings wasm { # Set the output directory for WebAssembly build DESTDIR = $$PWD/build-wasm # Enable WebAssembly support CONFIG += wasm # Include the Emscripten libraries INCLUDEPATH += $$PWD/3rdParty/emsdk/upstream/emscripten/system/include LIBS += -L $$PWD/emsdk/upstream/emscripten/system/lib \ -lglut -lGLU -lGL # Set the deployment directory for WebAssembly build QMAKE_PREFIX_PATH += $$PWD/build-wasm/qtbase # Link against the Emscripten libraries LIBS += -s USE_GLFW=3 -s WASM=1 -s USE_WEBGL2=1 -s PTHREAD_POOL_SIZE=100 # Add WebAssembly-specific compiler flags QMAKE_CXXFLAGS += -s FORCE_FILESYSTEM=1 } # Link against the Qt libraries LIBS += -L "C:/Qt6/6.5.1/wasm_multithread/lib/libQt6Core.a" -L "C:/Qt6/6.5.1/wasm_multithread/lib/libQt6Gui.a" -L "C:/Qt6/6.5.1/wasm_multithread/lib/libQt6Widgets.a"
-
Drag and drop has never worked very well in QtWebAssembly.
But it is working in 6.6.0beta1 single threaded release.You need to use asyncify though:
https://doc.qt.io/qt-6/wasm.html#asyncifyGood news is, you no longer have to rebuild Qt to get asyncify, you just need to add a linker argument.
cmake: target_link_options(<target> PUBLIC -sASYNCIFY -Os)
qmake: QMAKE_LFLAGS += -sASYNCIFY -Os