In macos 10.15, using CMake's MACOSX_BUNDLE causes qt5.12.11 to get devicePixelRatio of 1?

257 views Asked by At

CMake add to MACOSX_BUNDLE to use, I can build to get the .app folder, but Qt5.12.11 show devicePixelRatio: 1 (expected should be 2).

But when I block the MACOSX_BUNDLE macro, I can't build to get the .app folder, but Qt5.12.11 show devicePixelRatio: 2 (as expected).

This has been confusing me for over a month and I don't know if this is a Qt bug or a CMake bug? Does anyone know the answer?

In macos systems with high resolution display (Retina Display), the device pixel ratio is usually 2. deam:

// CMakeLists.txt
cmake_minimum_required(VERSION 3.5)

project(Example VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

set(PROJECT_SOURCES
        main.cpp
)

if (APPLE)
#    add_executable(Example MACOSX_BUNDLE ${PROJECT_SOURCES})   # devicePixelRatio: 1 ???
    add_executable(Example ${PROJECT_SOURCES})                # devicePixelRatio: 2   not build .app
else()
#    add_executable(Example WIN32 ${PROJECT_SOURCES})
endif()

target_link_libraries(Example PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
// main.cpp
#include <QApplication>
#include <QScreen>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qDebug()<< "devicePixelRatio:" <<QApplication::primaryScreen()->devicePixelRatio();

    return a.exec();
}

enter image description here

0

There are 0 answers