Convert small PyQt script to Qt C++
-
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 -
@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
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
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
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! -
@JonB aaand i gave up :/ i bet there is a ton of errors but...
Header -> https://pastebin.com/UdDHhpBZ
CPP -> https://pastebin.com/Eezyiqcg -
@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 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 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.
-
@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 ?
-
@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.