Alexhuszagh / BreezeStyleSheets

Breeze/BreezeDark-like Qt StyleSheets
MIT License
548 stars 128 forks source link

How to use this with CMake #21

Closed ruilvo closed 3 years ago

ruilvo commented 3 years ago

Hey! Thanks again for all the support with Qt-ADS!

I wanted to give something back, so here's a text on how to use your amazing work with projects built with CMake. Feel free to include it on your readme if you want:

First, this is what you need on your cmake file:

# This is how I usually setup Qt (makes it work with both Qt5 and Qt6 at the same time
# Setup Qt
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(
  QT NAMES Qt6 Qt5
  COMPONENTS Core
  REQUIRED)
find_package(
  Qt${QT_VERSION_MAJOR}
  COMPONENTS ${AE_REQUIRED_QT_COMPONENTS}
  REQUIRED)
# -------------------

# For this library:

# Get Python
find_package(Python COMPONENTS Interpreter)

include(FetchContent)

set(FETCHCONTENT_QUIET
    OFF
    CACHE BOOL "Silence fetch content" FORCE)

FetchContent_Declare(
  breeze_stylesheets
  GIT_REPOSITORY https://github.com/Alexhuszagh/BreezeStyleSheets.git
  GIT_TAG origin/master
  GIT_PROGRESS ON
  USES_TERMINAL_DOWNLOAD TRUE)

FetchContent_GetProperties(breeze_stylesheets)
if(NOT breeze_stylesheets_POPULATED)
  FetchContent_Populate(breeze_stylesheets)

  add_library(breeze_themes STATIC "${breeze_stylesheets_SOURCE_DIR}/breeze_themes.qrc")

  add_custom_target(
    run_python_breeze ALL
    COMMAND ${Python_EXECUTABLE} configure.py --extensions=advanced-docking-system
            --styles=dark-green,light-green --resource breeze_themes.qrc
    WORKING_DIRECTORY ${breeze_stylesheets_SOURCE_DIR}
    BYPRODUCTS "${breeze_stylesheets_SOURCE_DIR}/breeze_themes.qrc"
    COMMENT "Generating themes")

  add_dependencies(breeze_themes run_python_breeze)

endif()

And then, add your executable and link with the created library:

add_executable(myapp WIN32 MACOSX_BUNDLE "main.cpp")
target_link_libraries(myapp PRIVATE Qt${QT_VERSION_MAJOR}::Widgets breeze_themes)

On your main.cpp file you need to manually init the resources because they are coming from an external library:

// ...
QApplication qapp(argc, argv);

Q_INIT_RESOURCE(breeze_themes);
QFile file(":/dark-green/stylesheet.qss");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&file);
qapp.setStyleSheet(stream.readAll());
Alexhuszagh commented 3 years ago

If you want to submit a PR with this added to the README, feel free :D. I'd be more than happy to include it.

Alexhuszagh commented 3 years ago

Closed as of https://github.com/Alexhuszagh/BreezeStyleSheets/commit/8aff25b3a1b1400bd442f3e6818c18fae701608b. Note, I did change the directory for the generated styles to dist/, since I realized that building everything in-source might be a pretty bad idea with more and more themes.

This means that this line:

BYPRODUCTS "${breeze_stylesheets_SOURCE_DIR}/breeze_themes.qrc"

Should change to:

BYPRODUCTS "${breeze_stylesheets_SOURCE_DIR}/dist/breeze_themes.qrc"