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 Tuesday, 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 1 Watching
  • 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 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

    JonBJ 1 Reply Last reply
    0
    • K Kris Revi

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      3
      • JonBJ JonB

        @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 last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • K Kris Revi

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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
          3
          • JonBJ JonB

            @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 last edited by
            #5

            @JonB thanks for that and i understand!

            Python

            class Gradient(QtWidgets.QWidget):
            

            is that

            C++

            class gradient : public QWidget
            

            ?

            JonBJ 1 Reply Last reply
            0
            • K Kris Revi

              @JonB thanks for that and i understand!

              Python

              class Gradient(QtWidgets.QWidget):
              

              is that

              C++

              class gradient : public QWidget
              

              ?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              0
              • JonBJ JonB

                @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 last edited by Kris Revi
                #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!

                JonBJ 1 Reply Last reply
                0
                • K Kris Revi

                  @JonB

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

                  what would be the equal here in C++ ?

                  im stuck at this one!

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #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
                  1
                  • JonBJ JonB

                    @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 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

                    JonBJ J.HilkJ 2 Replies Last reply
                    0
                    • K Kris Revi

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

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      0
                      • JonBJ JonB

                        @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 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
                        0
                        • K Kris Revi

                          @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 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
                          1
                          • jsulmJ jsulm

                            @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 last edited by
                            #13

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

                            jsulmJ 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @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 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 :/

                              JonBJ 1 Reply Last reply
                              0
                              • K Kris Revi

                                @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 :/

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 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
                                0
                                • K Kris Revi

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

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 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 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
                                    • JonBJ JonB

                                      @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 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

                                      JonBJ 1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 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

                                          @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

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #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

                                          • Login

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