Menu

Subclassing a Qt Class and Still using Base Class Methods

Help
Mortie
2019-03-30
2019-03-31
  • Mortie

    Mortie - 2019-03-30

    Hi
    I'm using PythonQt for creating customized objects on a frame.
    what am I doing is that I'm subclassing QFrame (for example) and adding some features and methods for my own class (MyFrame) . then I wrote a wrapper class like what examples of PythonQt said. the problem comes out when I create an instance of MyFrame (myFrame) it'll not recognizing MyFrame as a subclass of QFrame and also don't show it's base class methods for using. it'll just shows my own methods in the wrapper class and i'm not having access to other methods in the base class.

    codes below shows my tutorial:

    // The Original Class
    class Sensor : public QFrame {
        Q_OBJECT
    
    public:
        Sensor(QWidget *parent = nullptr);
        void setValue(int _value);
    
    private:
        QPixmap *pix;
        QLabel *pixlbl;
        QLCDNumber *lcd;
        int value = 0;
    };
    
    // The Wrapper Class
    class SensorWrapper : public QObject {
        Q_OBJECT
    
    public Q_SLOTS:
        Sensor *new_Sensor(QWidget *parent = nullptr) {return new Sensor(parent);}
        void delete_Sensor(Sensor *o) {delete o;}
        void setValue(Sensor *o, int _value) {o.setValue(_value);}
    }
    
    // Registering Classes
    PythonQt::self()->registerCPPClasses("Sensor", "", "Wrapped", PythonQtCreateObject<SensorWrapper>);
    PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
    mainContext.evalScript("from PythonQt.Wrapped import *")
    
    // script part:
    sense = Sensor()
    sense.     
    

    <<------- PROBLEM: where QFrame methods are not available as sensor's base class (just setValue is available not for example show() , hide(), setSizePolicy() and etc
    can you help me with this issue?
    it'll be very helpful :)

     

    Last edit: Mortie 2019-03-30
  • Florian Link

    Florian Link - 2019-03-31

    You probably need to initialize PythonQtAll, because it contains the wrappers for QFrame. without generated wrappers, PythonQt can only provide the slots,signals and properties, but not methods that the moc does not handle. Calling PythonQtAll::init() before registering your class should do the trick. You could also pass in the name of the parent clas when registerings, but that is not really needed on Qobjects, I think.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.