Results 1 to 6 of 6

Thread: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

  1. #1
    Join Date
    Jul 2012
    Posts
    247
    Thanks
    29
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    Hi,

    I am using the standard autogenerated CMake-template, with vpckg.
    Thus I have this line at the very top:

    Qt Code:
    1. set(CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
    To copy to clipboard, switch view to plain text mode 

    Everything works fine when I open this in QtCreater 8 and build it.
    For Kits, I have 5.15 and 6.2 installed from the online installer. I configure both in QtCreator and can then switch between them in the build menu.


    This works fine ... as long as no Qt is installed in vcpkg. If I install "qtbase" vcpkg package (which contains Qt 6.3), then the build will *always* use this Qt installation, no matter what I select in Qt Creator (even when I select Qt 5.15).


    How can I resolve this situation so that CMake prefers the online installer Kits from QtCreator over the vcpkg package?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    Somewhere, either in the CMakelists.txt file that Qt Creator makes for your project, or perhaps in the vcpkg.cmake toolchain file, there will be a find_package() command that loads the Qt references. This find_package() command takes an optional "version" argument that appears after the package name. There is also a "REQUIRED" argument which causes CMake to fail if the package can't be found.

    There are some predefined constants that are passed in to the Qt package finder in CMake, one of which is QT_DEFAULT_MAJOR_VERSION. See the discussion here, especially the part about setting the value to either 5 or 6 before the find_package() command is called if you have mixed projects.

    You will probably have to modify your CMakelists.txt file to define this appropriately. Qt Creator has a "%(Qt:Version)" variable which I assume changes with the kit, but I don't have any idea how to reference this from within CMake.

    CMake can also take optional command line arguments that pass variables into CMake. I don't use Qt Creator much, especially not with CMake, so I do not know how Qt Creator invokes CMake. If you can specify a command line argument on a kit by kit basis, then that is where you could set a value for the QT_DEFAULT_MAJOR_VERSION variable.

    vpckg
    Thanks for this. I had no idea it existed and it looks like it could be useful. Sort of Microsoft's answer for the C++ world to Python's pip and conda I suppose.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    tuli (23rd September 2022)

  4. #3
    Join Date
    Jul 2012
    Posts
    247
    Thanks
    29
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    > vcpkg

    yep, it's great. I prefer it over conan. Unfortunately Qt package for Linux is barely usable, it ships only static version and has a bazillion dependencies on system-packages (at least that was the case a year ago).


    I made some progress with my issue:

    Default generated finding of Qt is this:

    Qt Code:
    1. find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
    2. find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    To copy to clipboard, switch view to plain text mode 

    Where the first findpackage() finds either Qt6 or Qt5 (prefers 6, if that doesnt exist looks for 5) and the second call does the actual importing/configuring.
    I can (and do) force the major version to 5 by setting QT_VERSION_MAJOR to 5 and skipping the first call.


    The reason why CMake prefers vcpkg's Qt package over QtCreators Kits is because the ONLY way QtCreator selects between Kits is setting CMAKE_PREFIX_PATH to the full path of the Kit. Hence if I select Qt5Kit in QtCreator find_package() only gets a path to Qt5, thus Qt6 cant be found and will default to Qt5.

    There are two issues with that:

    1. If vcpkg joins the party, CMAKE_PREFIX_PATH will contain paths to the Kit as well as to vcpkg packages. Now there is always a Qt6 found (the vcpkg one), and the path to the Qt5 Kit passed by QtCreator is ignored.

    2. Actually QtCreator appends the Kit path to CMAKE_PREFIX_PATH, vcpkg is faster, and its path comes before the Kit path. Thus if both vcpkg and QtCreator offer a Qt6 package, vcpkg satisfies find_package() first.


    Problem 2 can be worked around like so:

    Qt Code:
    1. list(POP_BACK CMAKE_PREFIX_PATH XX)
    2. list(PREPEND CMAKE_PREFIX_PATH ${XX})
    To copy to clipboard, switch view to plain text mode 


    Unfortunately I see no good way around Problem 1, besides very ugly string matching against the path to find out if the path is Qt5 or Qt6...
    If someone sees a good solution, please let me know...


    Added after 1 39 minutes:


    Ah, QtCreator sets Qt5_DIR or Qt6_DIR variables, depending on which one you select. If the project is built directly from CMake then neither of them is set.
    This should solve the second problem as well, I will check which variable is defined, and set QT_VERSION_MAJOR accordingly. If both are undefined I let find_package do its thing.


    edit: Nope, it will happily set both variables if it finds both version.... the search continues ...
    Last edited by tuli; 24th September 2022 at 00:26.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    the search continues ...
    You're already well past any help I can give, so good luck and please report back when you find a solution.

    I have 3 CMake books: Mastering CMake, CMake Cookbook, and Modern CMake for C++. I'll look through those and see if anything pops out.

    Edit:
    find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
    The link I posted above refers to a QT_DEFAULT_MAJOR_VERSION, not a QT_VERSION_MAJOR. Maybe using the former set to 5 or 6 will work?
    Last edited by d_stranz; 24th September 2022 at 03:13.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    tuli (27th September 2022)

  7. #5
    Join Date
    Jul 2012
    Posts
    247
    Thanks
    29
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    QT_DEFAULT_MAJOR_VERSION only helps to select between Qt5 and Qt6, which can be achieved e.g. by changing the order in the find_package call too, and sadly doesnt help here.
    I asked some other people and here are the results of this thread summarized:


    1. detect if built from within QtCreator vs plain Cmake: CMAKE_PREFIX_PATH is set to the path to the Qt Kit *before* the first call to project() vs being empty otherwise.
    2. detect if path is Qt5/Qt6: check for existence of e.g. Src/..../Qt6Config.cmake vs Qt5Config.cmake
    3. select between Qt5/Qt6 with QT_DEFAULT_MAJOR_VERSION or QT_MAJOR_VERSION (or a variety of other ways)
    4. it's not possible to ignore the vcpkg package in favor the QtCreator Kit. There are two solutions: a) maintain a separate vcpkg checkout that has no Qt package installed b) use vcpkg.json file, which basically does a) for you.

  8. The following user says thank you to tuli for this useful post:

    d_stranz (27th September 2022)

  9. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: CMake: vcpkg-qt takes precedence over onlineinstaller-qt

    4. it's not possible to ignore the vcpkg package in favor the QtCreator Kit. There are two solutions: a) maintain a separate vcpkg checkout that has no Qt package installed b) use vcpkg.json file, which basically does a) for you.
    Thanks for the update. It sounds like this is the way to go. Except for the convenience of having vcpkg manage the Qt build for you, is there a problem with not having a Qt package within vcpkg? If you are using Qt Creator for your builds, then selecting the right kit seems like it would solve the Qt version problem. (This being asked based on a lack of familiarity with both Qt Creator and vcpkg - I have used Visual Studio almost exclusively for literally decades and have manually managed project configurations).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. deploy vcpkg dependencies in QtCreator-Cmake
    By tuli in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd August 2021, 18:44
  2. Using QLoggingCategory, rules precedence seem backwards
    By don@thegagnes.com in forum Qt Programming
    Replies: 3
    Last Post: 5th December 2014, 19:17
  3. Qt server, which takes http-requests
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2012, 11:04
  4. QProgressBar takes big steps
    By Jayes in forum Qt Programming
    Replies: 14
    Last Post: 24th March 2012, 00:01
  5. QGraphicsItem.setPos takes a lot of time
    By ricofe25 in forum Qt Programming
    Replies: 2
    Last Post: 8th December 2009, 20:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.