Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert small PyQt script to Qt C++
Forum Update on Monday, May 27th 2025

Convert small PyQt script to Qt C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 5 Posters 5.4k Views
  • 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on 7 Dec 2020, 08:02 last edited by
    #1

    anyone wanna convert a small PyQt scrip to Qt C++ for me, for 11$? (why 11$? well it's a small script and that's all i have having christmas coming up)! :)

    This is the script:
    https://github.com/learnpyqt/python-qtwidgets/blob/master/qtwidgets/gradient/gradient.py

    J 1 Reply Last reply 7 Dec 2020, 08:14
    0
    • K Kris Revi
      7 Dec 2020, 08:02

      anyone wanna convert a small PyQt scrip to Qt C++ for me, for 11$? (why 11$? well it's a small script and that's all i have having christmas coming up)! :)

      This is the script:
      https://github.com/learnpyqt/python-qtwidgets/blob/master/qtwidgets/gradient/gradient.py

      J Offline
      J Offline
      JonB
      wrote on 7 Dec 2020, 08:14 last edited by JonB 12 Jul 2020, 08:15
      #2

      @Kris-Revi
      I don't know how fast other people type, but I wouldn't get through that in 1 hour for my $11 :)

      There really isn't much to it! Declare all the variables used with a specific type. Remove all the self.s (or change to this-> if you really prefer), because that just refers to member variables. Change self as a parameter to this. Change all (most) of the variable.s to variable->s. Change object.signal.emit() to emit object->signal(). Change the defs to a suitable function return result. And I'm sure you can guess, change all the indentations to { ... }. :) There really doesn't look to be much which is Python-y there.

      K 1 Reply Last reply 7 Dec 2020, 08:39
      3
      • J JonB
        7 Dec 2020, 08:14

        @Kris-Revi
        I don't know how fast other people type, but I wouldn't get through that in 1 hour for my $11 :)

        There really isn't much to it! Declare all the variables used with a specific type. Remove all the self.s (or change to this-> if you really prefer), because that just refers to member variables. Change self as a parameter to this. Change all (most) of the variable.s to variable->s. Change object.signal.emit() to emit object->signal(). Change the defs to a suitable function return result. And I'm sure you can guess, change all the indentations to { ... }. :) There really doesn't look to be much which is Python-y there.

        K Offline
        K Offline
        Kris Revi
        wrote on 7 Dec 2020, 08:39 last edited by
        #3

        @JonB thanks for the reply :) and the pointers! but im still unsure about 90% of this (conversion)

        J 1 Reply Last reply 7 Dec 2020, 09:00
        0
        • K Kris Revi
          7 Dec 2020, 08:39

          @JonB thanks for the reply :) and the pointers! but im still unsure about 90% of this (conversion)

          J Offline
          J Offline
          JonB
          wrote on 7 Dec 2020, 09:00 last edited by JonB 12 Jul 2020, 09:02
          #4

          @Kris-Revi
          Well, I thought I'd just pick one method to give you an idea:

          Python:

              def _constrain_gradient(self):
                  self._gradient = [
                      # Ensure values within valid range.
                      (max(0.0, min(1.0, stop)), color)
                      for stop, color in self._gradient
                  ]
          

          C++:

          // class type declaration
          struct StopColor {
              double stop;
              QString color;
          };
          // class member declaration
          QVector<StopColor> _gradient;
          // or you might choose a `QPair` instead of my `struct`
          // and/or you might choose a `StopColor []` instead of my `QVector<StopColor>`
          
          void Gradient::_constrain_gradient()
          {
              // Ensure values within valid range.
              for (StopColor &stopColor : _gradient)
                  stopColor.stop = std::max(0.0, std::min(1.0, stopColor.stop));
          }
          

          But I'm not going to offer the rest, best of luck :)

          K 1 Reply Last reply 7 Dec 2020, 09:06
          3
          • J JonB
            7 Dec 2020, 09:00

            @Kris-Revi
            Well, I thought I'd just pick one method to give you an idea:

            Python:

                def _constrain_gradient(self):
                    self._gradient = [
                        # Ensure values within valid range.
                        (max(0.0, min(1.0, stop)), color)
                        for stop, color in self._gradient
                    ]
            

            C++:

            // class type declaration
            struct StopColor {
                double stop;
                QString color;
            };
            // class member declaration
            QVector<StopColor> _gradient;
            // or you might choose a `QPair` instead of my `struct`
            // and/or you might choose a `StopColor []` instead of my `QVector<StopColor>`
            
            void Gradient::_constrain_gradient()
            {
                // Ensure values within valid range.
                for (StopColor &stopColor : _gradient)
                    stopColor.stop = std::max(0.0, std::min(1.0, stopColor.stop));
            }
            

            But I'm not going to offer the rest, best of luck :)

            K Offline
            K Offline
            Kris Revi
            wrote on 7 Dec 2020, 09:06 last edited by
            #5

            @JonB thanks for that and i understand!

            Python

            class Gradient(QtWidgets.QWidget):
            

            is that

            C++

            class gradient : public QWidget
            

            ?

            J 1 Reply Last reply 7 Dec 2020, 09:17
            0
            • K Kris Revi
              7 Dec 2020, 09:06

              @JonB thanks for that and i understand!

              Python

              class Gradient(QtWidgets.QWidget):
              

              is that

              C++

              class gradient : public QWidget
              

              ?

              J Offline
              J Offline
              JonB
              wrote on 7 Dec 2020, 09:17 last edited by JonB 12 Jul 2020, 09:36
              #6

              @Kris-Revi
              Absolutely yes! Though we're going to go very slowly if you need to ask at each level like this question :)

              However, please follow the same convention in Python as in C++ and name your classes with an initial capital letter, class Gradient : public QWidget!

              Not sure how much it will help, but at least some of the PySide2 doc pages show the Python equivalent of the same C++ doc page, e.g. as a purely random example, compare the sample boxed code in
              https://doc.qt.io/qtforpython/PySide2/QtWidgets/QDataWidgetMapper.html#detailed-description
              https://doc.qt.io/qt-5/qdatawidgetmapper.html#details

              K 1 Reply Last reply 7 Dec 2020, 11:42
              0
              • J JonB
                7 Dec 2020, 09:17

                @Kris-Revi
                Absolutely yes! Though we're going to go very slowly if you need to ask at each level like this question :)

                However, please follow the same convention in Python as in C++ and name your classes with an initial capital letter, class Gradient : public QWidget!

                Not sure how much it will help, but at least some of the PySide2 doc pages show the Python equivalent of the same C++ doc page, e.g. as a purely random example, compare the sample boxed code in
                https://doc.qt.io/qtforpython/PySide2/QtWidgets/QDataWidgetMapper.html#detailed-description
                https://doc.qt.io/qt-5/qdatawidgetmapper.html#details

                K Offline
                K Offline
                Kris Revi
                wrote on 7 Dec 2020, 11:42 last edited by Kris Revi 12 Jul 2020, 11:52
                #7

                @JonB

                self._gradient = sorted(self._gradient, key=lambda g:g[0])
                

                what would be the equal here in C++ ?

                im stuck at this one!

                J 1 Reply Last reply 7 Dec 2020, 11:56
                0
                • K Kris Revi
                  7 Dec 2020, 11:42

                  @JonB

                  self._gradient = sorted(self._gradient, key=lambda g:g[0])
                  

                  what would be the equal here in C++ ?

                  im stuck at this one!

                  J Offline
                  J Offline
                  JonB
                  wrote on 7 Dec 2020, 11:56 last edited by JonB 12 Jul 2020, 11:56
                  #8

                  @Kris-Revi
                  I assumed you would be familiar with C++, given that you want to translate from Python to C++?! :)

                  To sort a QVector --- or if you pick another type like std::vector --- you will want to use std::sort.

                  See e.g. https://stackoverflow.com/questions/47989208/sort-qvector-of-pointers-to-custom-objects if you do the items as pointers, or I gave you struct see https://forum.qt.io/topic/47037/solved-sorting-possible-on-a-qvector-that-has-a-struct-as-elements (that is old and some posts use qSort(), I suggest following @Chris-Kawa's post at https://forum.qt.io/topic/47037/solved-sorting-possible-on-a-qvector-that-has-a-struct-as-elements/8 which uses std::sort()).

                  I admit I'm not 100% sure what the Python key=lambda g:g[0] is. I think it's just picking out the first field, stop, to sort on, not on the color field!

                  K 1 Reply Last reply 8 Dec 2020, 10:34
                  1
                  • J JonB
                    7 Dec 2020, 11:56

                    @Kris-Revi
                    I assumed you would be familiar with C++, given that you want to translate from Python to C++?! :)

                    To sort a QVector --- or if you pick another type like std::vector --- you will want to use std::sort.

                    See e.g. https://stackoverflow.com/questions/47989208/sort-qvector-of-pointers-to-custom-objects if you do the items as pointers, or I gave you struct see https://forum.qt.io/topic/47037/solved-sorting-possible-on-a-qvector-that-has-a-struct-as-elements (that is old and some posts use qSort(), I suggest following @Chris-Kawa's post at https://forum.qt.io/topic/47037/solved-sorting-possible-on-a-qvector-that-has-a-struct-as-elements/8 which uses std::sort()).

                    I admit I'm not 100% sure what the Python key=lambda g:g[0] is. I think it's just picking out the first field, stop, to sort on, not on the color field!

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on 8 Dec 2020, 10:34 last edited by
                    #9

                    @JonB aaand i gave up :/ i bet there is a ton of errors but...

                    Header -> https://pastebin.com/UdDHhpBZ
                    CPP -> https://pastebin.com/Eezyiqcg

                    J J.HilkJ 2 Replies Last reply 8 Dec 2020, 10:40
                    0
                    • K Kris Revi
                      8 Dec 2020, 10:34

                      @JonB aaand i gave up :/ i bet there is a ton of errors but...

                      Header -> https://pastebin.com/UdDHhpBZ
                      CPP -> https://pastebin.com/Eezyiqcg

                      J Offline
                      J Offline
                      JonB
                      wrote on 8 Dec 2020, 10:40 last edited by
                      #10

                      @Kris-Revi
                      From a glance, it seems to me you are doing well. I hope you don't mean you are giving up having got this far? I can't spare much time, but do you want me to tell you the C++ for the couple of bits I see commented out in your .cpp?

                      K 2 Replies Last reply 8 Dec 2020, 10:43
                      0
                      • J JonB
                        8 Dec 2020, 10:40

                        @Kris-Revi
                        From a glance, it seems to me you are doing well. I hope you don't mean you are giving up having got this far? I can't spare much time, but do you want me to tell you the C++ for the couple of bits I see commented out in your .cpp?

                        K Offline
                        K Offline
                        Kris Revi
                        wrote on 8 Dec 2020, 10:43 last edited by
                        #11

                        @JonB sadly i am giving up :/ when i ran this it gave me a shit load of errors (i know that 1 error can cause other things to error out aswell) but i couldn't figure out the error anyway so

                        jsulmJ 1 Reply Last reply 8 Dec 2020, 10:44
                        0
                        • K Kris Revi
                          8 Dec 2020, 10:43

                          @JonB sadly i am giving up :/ when i ran this it gave me a shit load of errors (i know that 1 error can cause other things to error out aswell) but i couldn't figure out the error anyway so

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 8 Dec 2020, 10:44 last edited by
                          #12

                          @Kris-Revi said in Convert small PyQt script to Qt C++:

                          i know that 1 error can cause other things to error out aswell

                          That's why you should always fix the first error first :-)

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

                          K 1 Reply Last reply 8 Dec 2020, 10:50
                          1
                          • jsulmJ jsulm
                            8 Dec 2020, 10:44

                            @Kris-Revi said in Convert small PyQt script to Qt C++:

                            i know that 1 error can cause other things to error out aswell

                            That's why you should always fix the first error first :-)

                            K Offline
                            K Offline
                            Kris Revi
                            wrote on 8 Dec 2020, 10:50 last edited by
                            #13

                            @jsulm i coded everything needed (the cpp and h i linked) befor testing so :/

                            jsulmJ 1 Reply Last reply 8 Dec 2020, 11:40
                            0
                            • J JonB
                              8 Dec 2020, 10:40

                              @Kris-Revi
                              From a glance, it seems to me you are doing well. I hope you don't mean you are giving up having got this far? I can't spare much time, but do you want me to tell you the C++ for the couple of bits I see commented out in your .cpp?

                              K Offline
                              K Offline
                              Kris Revi
                              wrote on 8 Dec 2020, 11:06 last edited by
                              #14

                              @JonB said in Convert small PyQt script to Qt C++:

                              but do you want me to tell you the C++ for the couple of bits I see commented out in your .cpp?

                              i bet it wouldn't fix it anyway :/

                              J 1 Reply Last reply 8 Dec 2020, 11:11
                              0
                              • K Kris Revi
                                8 Dec 2020, 11:06

                                @JonB said in Convert small PyQt script to Qt C++:

                                but do you want me to tell you the C++ for the couple of bits I see commented out in your .cpp?

                                i bet it wouldn't fix it anyway :/

                                J Offline
                                J Offline
                                JonB
                                wrote on 8 Dec 2020, 11:11 last edited by
                                #15

                                @Kris-Revi
                                It would not fix errors, no! I'm not clear whether you are saying your "errors" are at compile/link time, or at runtime....

                                K 1 Reply Last reply 8 Dec 2020, 11:57
                                0
                                • K Kris Revi
                                  8 Dec 2020, 10:50

                                  @jsulm i coded everything needed (the cpp and h i linked) befor testing so :/

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 8 Dec 2020, 11:40 last edited by
                                  #16

                                  @Kris-Revi said in Convert small PyQt script to Qt C++:

                                  i coded everything needed (the cpp and h i linked) befor testing so :/

                                  I don't know what you mean here.
                                  You wrote that you get errors when building the code, right?
                                  So, start with the first error...

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

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 8 Dec 2020, 11:47 last edited by
                                    #17

                                    Hi,

                                    One issue in your code is in your constructor, you are checking if the vector is empty but you have the logic reversed. You do something with it when it's actually empty.

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

                                    1 Reply Last reply
                                    1
                                    • J JonB
                                      8 Dec 2020, 11:11

                                      @Kris-Revi
                                      It would not fix errors, no! I'm not clear whether you are saying your "errors" are at compile/link time, or at runtime....

                                      K Offline
                                      K Offline
                                      Kris Revi
                                      wrote on 8 Dec 2020, 11:57 last edited by
                                      #18

                                      @JonB said in Convert small PyQt script to Qt C++:

                                      @Kris-Revi
                                      It would not fix errors, no! I'm not clear whether you are saying your "errors" are at compile/link time, or at runtime....

                                      @ compile

                                      J 1 Reply Last reply 8 Dec 2020, 12:00
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 8 Dec 2020, 11:58 last edited by
                                        #19

                                        Can you share the errors you got ?

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

                                        1 Reply Last reply
                                        0
                                        • K Kris Revi
                                          8 Dec 2020, 11:57

                                          @JonB said in Convert small PyQt script to Qt C++:

                                          @Kris-Revi
                                          It would not fix errors, no! I'm not clear whether you are saying your "errors" are at compile/link time, or at runtime....

                                          @ compile

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 8 Dec 2020, 12:00 last edited by JonB 12 Aug 2020, 12:01
                                          #20

                                          @Kris-Revi
                                          Then, if you can't figure it, show us the first line at fault and what the error message is, if you'd like help... :)

                                          BTW, it won't affect your compile behaviour, but you absolutely need to heed @SGaist's earlier observation for your run-time behaviour. Plus, I didn't follow the logic for the consequence, but you still have the commented-out initialization of the array when it's empty to deal with.

                                          1 Reply Last reply
                                          0

                                          1/23

                                          7 Dec 2020, 08:02

                                          • Login

                                          • Login or register to search.
                                          1 out of 23
                                          • First post
                                            1/23
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved