Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Casting QVector<int> to QVector<double>

    General and Desktop
    6
    8
    1947
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dovahin last edited by

      Is there an elegant way of casting QVector<int> to QVector<double> ? I know I could iterate through both of them, but looking for something better

      kshegunov 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        Why do you need to do that for ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply Reply Quote 0
        • kshegunov
          kshegunov Moderators @dovahin last edited by

          @dovahin said in Casting QVector<int> to QVector<double>:

          I know I could iterate through both of them, but looking for something better

          What @SGaist wrote. Also there's nothing better. QVector<int> and QVector<double> are two different and distinct classes, you can't just cast from one to the other.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply Reply Quote 4
          • D
            dovahin @SGaist last edited by

            @SGaist because I have QVector<int> and I need to pass it to qwt_plot_currve function setSamples which takes QVector<double>

            jsulm J.Hilk 2 Replies Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @dovahin last edited by

              @dovahin Either you use QVector<double> from the beginning, then there is no need to cast anything. Or you will have to iterate over the element of your QVector<int> and add each element to the QVector<double> which isn't efficient.
              As @kshegunov said both classes are different and you can't simply cast one to the other.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 1
              • J.Hilk
                J.Hilk Moderators @dovahin last edited by

                @dovahin

                you could try to initialize your QVectpr<double>(myOtherVector.size()) and than try direct data manipulation via T *QVector::data() but you can not simply memcopy the data over. It won't be the same size and is not reinterpret_cast compatible.

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  The propose idea to use QVector<double> in the first place is the fastest one. But if you really need to convert such a vector:

                  QVector<int> in;
                  QVector<double> out;
                  out.reserve(in.size());
                  std::copy(in.cbegin(), in.cend(), std::back_inserter(out));
                  

                  Qt has to stay free or it will die.

                  D 1 Reply Last reply Reply Quote 6
                  • D
                    dovahin @Christian Ehrlicher last edited by

                    @Christian-Ehrlicher thanks, thats solve my problem :)

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post