From 98d352a7c1862ab069d90157137daa0e2dce3e01 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 11 Apr 2020 23:16:24 +0800 Subject: [PATCH] fix: single app does not works under windows --- CMakeLists.txt | 12 ++++++++++-- singleapplicationmanager.cpp | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a344fb2..18a2d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(Qt5 COMPONENTS Widgets Multimedia REQUIRED) +find_package(Qt5 COMPONENTS Widgets Multimedia Network REQUIRED) set (EXE_NAME pmusic) @@ -35,7 +35,15 @@ add_executable(${EXE_NAME} ID3v2Pic.h ) -target_link_libraries(${EXE_NAME} PRIVATE Qt5::Widgets Qt5::Multimedia) +target_link_libraries(${EXE_NAME} PRIVATE Qt5::Widgets Qt5::Multimedia Qt5::Network) + +# Extra build settings +if (WIN32) + set_property ( + TARGET ${EXE_NAME} + PROPERTY WIN32_EXECUTABLE true + ) +endif () # Helper macros for install settings macro (pmusic_convert_to_relative_path _var) diff --git a/singleapplicationmanager.cpp b/singleapplicationmanager.cpp index d1c5c10..1b597e6 100644 --- a/singleapplicationmanager.cpp +++ b/singleapplicationmanager.cpp @@ -16,6 +16,7 @@ void SingleApplicationManager::on_localSocket_newConnection() QScopedPointer socket(m_localServer->nextPendingConnection()); if (socket) { socket->waitForReadyRead(500); + QDataStream dataStream(socket.data()); QVariant data; dataStream.startTransaction(); @@ -44,11 +45,14 @@ bool SingleApplicationManager::checkSingleInstance(QVariant data) QLocalSocket socket; socket.connectToServer(m_applicationKey); if (socket.waitForConnected(500)) { - QDataStream dataStream(&socket); - dataStream << data; + QByteArray block; + QDataStream out(&block, QIODevice::WriteOnly); + out << data; + socket.write(block); socket.waitForBytesWritten(); - socket.close(); + socket.flush(); + socket.disconnectFromServer(); return true; }