Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Mixing QT with C++ Standard Library



  • Hi,

    is it feasible to mix QT commands with Standard Library
    commands?

    1. as far as I know QT Container are compatible with
      std-<algorithm> and std-iterators. Is this correct?
      Is this feasible? Does anybody use it?
      Does one need special considerations?

    2. What std commands do you use in your QT code?
      Can you provide examples?
      e.g. std::sort() seems useful for me.

    3. Do you also mix std containers with QT container in one
      project?

    Thanks for help.


  • Qt Champions 2019

    See https://doc.qt.io/qt-5/containers.html
    I don't see a reason why it should not be mixed but you should not convert it the whole time.



  • I am generally paranoid of mixing Qt containers with STL containers, but have no qualms about using STL for platform independent modules that need to exist within a Qt app. I just make sure to do explicit conversions if the behaviour documentation doesn't meet my standards. It usually does not.


  • Moderators

    @Kent-Dorfman said in Mixing QT with C++ Standard Library:

    I am generally paranoid of mixing Qt containers with STL containers

    Why's that? Have you experienced issues?

    @TauCeti said in Mixing QT with C++ Standard Library:

    is it feasible to mix QT commands with Standard Library
    commands?

    Yes.

    1. as far as I know QT Container are compatible with
      std-<algorithm> and std-iterators. Is this correct?
      Is this feasible? Does anybody use it?
      Does one need special considerations?

    Yes. Yes. Yes. Yes.

    For your last question, see the Implicitly Shared Iterator Problem: https://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem

    1. What std commands do you use in your QT code?
      Can you provide examples?
      e.g. std::sort() seems useful for me.

    2. Do you also mix std containers with QT container in one
      project?

    I quite happily do things like:

    • Put QString in a std::pair
    • Put std::shared_ptr<MyStruct> inside a QVector
    • Use std::sort(), std::copy_if() on my QVector

    However, I avoid converting a Qt container to an STL container and vice-versa, like @Christian-Ehrlicher said.


Log in to reply