Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

multiple inheritance



  • i would like to add the Thread class to my customButton class

    thread-basics

    custombutton.h file

    #ifndef CUSTOMBUTTON_H
    #define CUSTOMBUTTON_H
    
    #include <QPushButton>
    #include <QThread>
    
    class CustomButton : public QPushButton, public QThread
    {
    public:
        CustomButton( const QString& text, QWidget* parent = 0 );
    
        // Enhancement, it will reverse the button text
        void reverseText();
    private:
        void run();
    };
    
    #endif // CUSTOMBUTTON_H
    
    

    customButton.cpp file

    #include "CustomButton.h"
    #include "algorithm"
    #include <QDebug>
    #include "CustomButton.h"
    #include "algorithm"
    #include <QDebug>
    #include <QApplication>
    
    CustomButton::CustomButton( const QString& text, QWidget* parent )
        : QPushButton( text, parent )
    {
        //this->set
        const QSize btnSize = QSize(150, 150);
        this->setFixedSize(btnSize);
        this->move(1000, 10);
    
    }
    
    void CustomButton::reverseText()
    {
        QString buttonText = text();
        std::reverse(buttonText.begin(), buttonText.end());
        setText( buttonText );
    }
    
    void CustomButton::run(){
        int randomNumber = rand();
        setText(  QString::number(randomNumber) );
    }
    
    

    main.cpp

    .
    ..
    ...
    CustomButton *mybutton = new CustomButton( QString::number(size.width()) + " abc " + QString::number(size.height()));
        mybutton->show();
        mybutton->start(); // <---
        mybutton->reverseText();
    ...
    ..
    .
    

    how do i make random numbers appear on the button every x time?

    if you comment out:

    //mybutton->start();
    

    the "run" method doesn't get executed.

    does the thread class only apply to private methods?



  • @Natural_Bugger said in multiple inheritance:

    class CustomButton : public QPushButton, public QThread

    How does it make sense to create an object which inherits from a QPushButton and from QThread?

    I do not see any connection. Keep any threading quite separate.



  • @JonB

    well, i wanna try to give that button some properties.
    i need to access the button properties.
    just trying some stuff.

    what do you suggest?



  • @Natural_Bugger
    It's difficult to know, because I have no idea what you are thinking of conceptually....

    Why are you going anywhere near threads at all? They add a lot of complexity, what use case do you have here for threads?

    how do i make random numbers appear on the button every x time?

    Maybe use a QTimer to change the text on the button over time??

    Or, if you really, really have a thread generating random numbers all the time and you want to display them, use the pattern that the worker thread raises signals with the desired number/text and the main UI thread has a slot for the signal which updates whatever on the UI.



  • Well, just want to say that multiple inheritance of classes which inherit QObject is not allowed.



  • @JonB

    i would like to make the button fall at a rate of 9.81 pixels per second and if i lift it up again.
    so my guess: is that my button needs to check autonomously where it resides.



  • @Bonnie raises a good point: you cannot inherit twice from anything inheriting from QObject. So you won't get class CustomButton : public QPushButton, public QThread to work anyway, which is just as well! :)



  • @Natural_Bugger said in multiple inheritance:

    i would like to make the button fall at a rate of 9.81 pixels per second and if i lift it up again.

    Not that I know what this means, but it sounds like you want a 1-second repeating QTimer in which you do some update to the UI?


  • Moderators

    @Natural_Bugger I'm surprised this compiled

    https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first

    Inheriting from multiple QObjects is not allowed/supported



  • @J-Hilk

    it actually runs.

    mybutton->start(); // <---
    

    if i comment out "mybutton->start(); // <---", the run function doesn't work, otherwise it does.



  • @JonB

    it's just an experiment.
    trying to give the button some "life"



  • @Natural_Bugger
    Well, it's the wrong experiment! Conceptually widgets and threads have nothing to do with each other. What's wrong with a QTimer, which sounds like all you are looking for?


  • Lifetime Qt Champion

    Hi
    For such animations, you can also use
    https://doc.qt.io/qt-5/qpropertyanimation.html


Log in to reply