Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:54:57+00:00 2026-05-30T13:54:57+00:00

I have a QMainWindow that connects 2 buttons with 2 functions. I want to

  • 0

I have a QMainWindow that connects 2 buttons with 2 functions. I want to start the QTimer with the StartBTNClick function and stop the QTimer with the StopBTNClick function. My problem is, that the QTimer is not defined in StopBTNClick, that’s why I want to know how to define a public QTimer. (btw. I’m new to C++, please bear with me)

This is my code so far:

MyClass::MainWindow(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    statusBar()->showMessage("Status: Idle.");
    connect(ui.StartBTN, SIGNAL(clicked()), this, SLOT(StartBTNClick()));
    connect(ui.StopBTN, SIGNAL(clicked()), this, SLOT(StopBTNClick()));
}

void MyClass::StartBTNClick()
{
    QTimer *RunTimer = new QTimer(this);
    connect(RunTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
    RunTimer->start(5000);
    statusBar()->showMessage("Status: Running.");
}

void MyClass::StopBTNClick()
{
    RunTimer->stop(); // Not working. Says it's not defined.
    disconnect(RunTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
    statusBar()->showMessage("Status: Idle.");
}

void MyClass::TimerHandler()
{
    // I set QMessageBox to test if it's working.
    QMessageBox::information(this, "lala", "nomnom");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:55:00+00:00Added an answer on May 30, 2026 at 1:55 pm

    Make the QTimer a member variable of your class, and it will be accessible in both functions.

    class MyClass
    {
    ... //other variables and method definitions
    
    QTimer* runTimer;
    };
    

    Use the class member pointer, rather than a locally-defined one:

    void MyClass::StartBTNClick()
    {
        runTimer = new QTimer(this);//this would work, but it may be better to
                                      //initialize the timer in the constructor
        connect(runTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
        runTimer->start(5000);
        statusBar()->showMessage("Status: Running.");
    }
    
    void MyClass::StopBTNClick()
    {
        runTimer->stop(); // this will work now; if it hasn't been initialized, though,
                          // you will cause a crash.
        disconnect(RunTimer, SIGNAL(timeout()), this, SLOT(TimerHandler()));
        statusBar()->showMessage("Status: Idle.");
    }
    

    It would probably be better to initialize the time (runTimer = new QTimer(this) ) in the constructor rather than on a button click, and just start the timer when the button is clicked. If you don’t do it this way, you will have to guard against using an uninitialized pointer in StopBTNClick() in case that button is be clicked before the Start button.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits QMainWindow and I just want it to have
i have Qmainwindow that opens with show() method another QMainWindow the problem is that
I would like to have a QMainWindow that can change it's look at runtime,
I have a QMainWindow, and want to handle the clicked signal from a smaller
i have a QMainWindow that starts a QThread and waits for data from the
I have a small problem with adding widget to QMainWindow. When i do it
i have two functions and one of them call the other one. I want
I have an application with a QMainWindow that should ideally have a QGLWidget centered
I have a QTabWidget parented by a QMainWindow that manages QTextEdit widgets. I'd like
Hi I have problem in code that the following code retuning the error that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.