Convert small PyQt script to Qt C++
-
wrote on 7 Dec 2020, 08:02 last edited by
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 -
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.pywrote on 7 Dec 2020, 08:14 last edited by JonB 12 Jul 2020, 08:15@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 tothis->
if you really prefer), because that just refers to member variables. Changeself
as a parameter tothis
. Change all (most) of thevariable.
s tovariable->
s. Changeobject.signal.emit()
toemit object->signal()
. Change thedef
s 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. -
@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 tothis->
if you really prefer), because that just refers to member variables. Changeself
as a parameter tothis
. Change all (most) of thevariable.
s tovariable->
s. Changeobject.signal.emit()
toemit object->signal()
. Change thedef
s 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. -
@JonB thanks for the reply :) and the pointers! but im still unsure about 90% of this (conversion)
wrote on 7 Dec 2020, 09:00 last edited by JonB 12 Jul 2020, 09:02@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 :)
-
@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 :)
-
@JonB thanks for that and i understand!
Python
class Gradient(QtWidgets.QWidget):
is that
C++
class gradient : public QWidget
?
wrote on 7 Dec 2020, 09:17 last edited by JonB 12 Jul 2020, 09:36@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 -
@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 -
self._gradient = sorted(self._gradient, key=lambda g:g[0])
what would be the equal here in C++ ?
im stuck at this one!
wrote on 7 Dec 2020, 11:56 last edited by JonB 12 Jul 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 likestd::vector
--- you will want to usestd::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 useqSort()
, 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 usesstd::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 thecolor
field! -
@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 likestd::vector
--- you will want to usestd::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 useqSort()
, 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 usesstd::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 thecolor
field!wrote on 8 Dec 2020, 10:34 last edited by@JonB aaand i gave up :/ i bet there is a ton of errors but...
Header -> https://pastebin.com/UdDHhpBZ
CPP -> https://pastebin.com/Eezyiqcg -
@JonB aaand i gave up :/ i bet there is a ton of errors but...
Header -> https://pastebin.com/UdDHhpBZ
CPP -> https://pastebin.com/Eezyiqcgwrote on 8 Dec 2020, 10:40 last edited by@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
? -
@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
? -
@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
@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 :-)
-
@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 :-)
-
@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
?wrote on 8 Dec 2020, 11:06 last edited by@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 :/
-
@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 :/
wrote on 8 Dec 2020, 11:11 last edited by@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.... -
@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... -
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.
-
@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....wrote on 8 Dec 2020, 11:57 last edited by@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
-
Can you share the errors you got ?
-
@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
wrote on 8 Dec 2020, 12:00 last edited by JonB 12 Aug 2020, 12:01@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/23