multiple inheritance
-
i would like to add the Thread class to my customButton class
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 fromQThread
?I do not see any connection. Keep any threading quite separate.
-
@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.
-
@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? -
@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
-
@Natural_Bugger
Well, it's the wrong experiment! Conceptually widgets and threads have nothing to do with each other. What's wrong with aQTimer
, which sounds like all you are looking for? -
Hi
For such animations, you can also use
https://doc.qt.io/qt-5/qpropertyanimation.html