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. multiple inheritance
Qt 6.11 is out! See what's new in the release blog

multiple inheritance

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 4.0k 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by Natural_Bugger
    #1

    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?

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • N Natural_Bugger

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      N 1 Reply Last reply
      2
      • JonBJ JonB

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

        N Offline
        N Offline
        Natural_Bugger
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • N Natural_Bugger

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

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

          N 1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            2
            • JonBJ JonB

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

              N Offline
              N Offline
              Natural_Bugger
              wrote on last edited by
              #6

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

              JonBJ 1 Reply Last reply
              0
              • B Bonnie

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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! :)

                1 Reply Last reply
                0
                • N Natural_Bugger

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

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

                  N 1 Reply Last reply
                  0
                  • N Natural_Bugger

                    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?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    N 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @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

                      N Offline
                      N Offline
                      Natural_Bugger
                      wrote on last edited by
                      #10

                      @J-Hilk

                      it actually runs.

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

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

                      1 Reply Last reply
                      0
                      • JonBJ JonB

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

                        N Offline
                        N Offline
                        Natural_Bugger
                        wrote on last edited by
                        #11

                        @JonB

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

                        JonBJ 1 Reply Last reply
                        0
                        • N Natural_Bugger

                          @JonB

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

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          4
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            4

                            • Login

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