Parent window is still responsive when child window opens
-
Pretty new to Qt, I am developing a simple GUI DLL for db driver configuration in Windows.
First, the situation. I am improving an ODBC driver.
Windows ODBC Administrator allows you to write a GUI get configs from the user, and puts them into the registry.
We just have to create a DLL, and export the function ConfigDSN:BOOL CALLBACK ConfigDSN( HWND hwndParent, // Parent window handle WORD fRequest, LPCSTR driver_desc, LPCSTR connect_str) { int argc = 0; char *argv[] = {nullptr}; QApplication app(argc, argv); MyWidget widget; SetWindowLongPtr((HWND)widget.winId(), GWLP_HWNDPARENT, (LONG_PTR)hwnd); EnableWindow(hwnd, FALSE); widget.setWindowModality(Qt::WindowModality::ApplicationModal);// same issue with Qt::WindowModality::WindowModal widget.show(); int result = app.exec(); // Re-enable the parent window when the dialog is closed if (hwnd) { EnableWindow(hwnd, TRUE); SetForegroundWindow(hwnd); // Bring the parent window to the front } return result == 0; }As youmay see above, I have created an empty form MyWidget which I can open via ODBC Administrator.
I tried several combinations but the problem is, after opening the window:
The parent ODBC Administrator window size and location changes. And, it is still responsive to my clicks while it shouldn't be so until I close MyWidget.
How can I fix this?Here is my CMakeLists.txt if it helps:
cmake_minimum_required(VERSION 3.16) project(odbcsetup LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) list(APPEND CMAKE_PREFIX_PATH "C:\\Qt\\6.8.0\\msvc2022_64\\lib\\cmake") find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) set(TARGET_NAME "odbcsetup") add_library(${TARGET_NAME} SHARED odbcsetup_global.h dsngui.cpp dsngui.h DsnSetup.cpp DsnSetup.h dsnform.h dsnform.cpp dsnform.ui ) target_link_libraries(${TARGET_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_compile_definitions(${TARGET_NAME} PRIVATE ODBC_SETUP_LIBRARY) # Set the output directories for the target set_target_properties( ${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIG>" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIG>" ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIG>") install(TARGETS ${TARGET_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/lib") -
int result = app.exec();After that line, the window is no longer visible and
hwndmight even contain a stale pointer.To show a window containing a
QWidget, there is no need to call Windows API directly. Doing that is basically asking for trouble.
Remove all win32 calls and use Qt API only.