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. [SOLVED]QTimer in a Class - Problem
QtWS25 Last Chance

[SOLVED]QTimer in a Class - Problem

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 2.1k Views
  • 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.
  • P Offline
    P Offline
    pusheax
    wrote on last edited by pusheax
    #1

    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 //
    Here

    And the main (I don't think it's important but here):
    Here

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pusheax
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pusheax
            wrote on last edited by
            #5

            Basically I want to create a QTimer in another class and call it from the button in the mywidget class.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then if you want to re-use it, just make a member of it of your MyWidget class

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pusheax
                wrote on last edited by
                #7

                I'm sorry but I don't really get it what you mean. Could you provide me an example please?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8
                  #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();
                  }
                  

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • P Offline
                    P Offline
                    pusheax
                    wrote on last edited by
                    #9

                    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 Reply Last reply
                    0

                    • Login

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