[SOLVED]QTimer in a Class - Problem
-
wrote on 5 Apr 2015, 21:24 last edited by pusheax 4 May 2015, 22:34
Hello dear community,
Before I start explaining my problem I would like to introduce myself since this is my very first post here :)!
I'm a developer for automatic "botting systems" for online games and experienced with Assembly/Python/C++ and a bit with C#.Now let me explain my problem:
For example I've created a fresh Qt Widgets Application with a QWidget and a class.
Here are my classes:
// mywidget.h //
Here// mywidget.cpp //
Here// myclass.h //
Here// myclass.cpp //
HereAnd the main (I don't think it's important but here):
HereI commented the code where I have trouble and FYI the code compiles fine but the Slot from the QTimer in the Class "myclass" is not being executed. Other than that the Slot in the "mywidget" class is working fine.
I hope that some people can help me and maybe even others who have the same issue. I hope everything is clear and please don't judge me and the code, I'm really new to Qt :).Edit: My target is to execute a function in a QTimer from another class when I click pushButton.
-
Hi and welcome to devnet,
Your MyClass instance in on_pushButton_clicked will only live as long as the line it's created on till then end of the method. So basically, it's destroyed right after it's created.
-
wrote on 5 Apr 2015, 21:35 last edited by
First of all thanks for the fast answer!
Hmm.. but if that's the case then I still wonder how I can initialize the instance without destroying it directly?
-
Before that, what exactly are you trying to achieve ? It's important to know to avoid using a hammer to solve a problem that requires a screwdriver.
-
wrote on 5 Apr 2015, 21:39 last edited by
Basically I want to create a QTimer in another class and call it from the button in the mywidget class.
-
Then if you want to re-use it, just make a member of it of your MyWidget class
-
wrote on 5 Apr 2015, 21:47 last edited by
I'm sorry but I don't really get it what you mean. Could you provide me an example please?
-
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include <QTimer> #include <QMessageBox> #include "myclass.h" namespace Ui { class MyWidget; } class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); ~MyWidget(); private slots: void on_pushButton_clicked(); private: Ui::MyWidget *ui; MyClass _myClass; }; #endif // MYWIDGET_H
#include "mywidget.h" #include "ui_mywidget.h" MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget) { ui->setupUi(this); } MyWidget::~MyWidget() { delete ui; } void MyWidget::on_pushButton_clicked() { _myClass.whateverFunction(); }
-
wrote on 5 Apr 2015, 22:22 last edited by
Thank you so much for your great help it worked fine!
Now I understand why it had to be declared in the mywidget thx!#Can be closed [SOLVED]
1/9