Derived class of wxTimer doesn't work Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Derived class of wxTimer doesn't work

Post by cneng3 »

Hello all,

I'm trying to create a simple class that derives from wxTImer. So I have code like this:

myClass.h

Code: Select all

class myClass : public wxTimer
{
	public:
		myClass();
		virtual ~myClass();
		void Notify() override;
}
myClass.cpp

Code: Select all

/// ctor and dtor

void myClass::Notify()
{
	wxLogMessage("Notified");
}
On my main thread:

Code: Select all

	m_myClass = new myClass();
	if (!m_myClass->Start(1000))
		wxLogError("Failed to start the timer");
After I ran my code. I didn't get the error message, so based on the documentation, it means the timer actually started. However, I didn't get the log message from Notify() either. So I'm really confused right now. Any help would be appreciated.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Derived class of wxTimer doesn't work

Post by doublemax »

That looks ok. Did you call the default wxTimer constructor?
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Derived class of wxTimer doesn't work

Post by PB »

As doublemax said, you probably did something wrong in myClass ctor.

This works as expected

Code: Select all

#include <wx/wx.h>

class myClass : public wxTimer
{
public:
    myClass() {};    
    void Notify() override { wxLogMessage("myClass::Notify"); }
};

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Test") 
    {
        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);     
        wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));    
    
        m_myClass = new myClass();
        if (!m_myClass->Start(1000))
            wxLogError("Failed to start the timer");
    }	
private:
    myClass* m_myClass;
};

class MyApp : public wxApp
{
public:         
    bool OnInit()
    {
        (new MyFrame())->Show();               
        return true;
    }   
}; wxIMPLEMENT_APP(MyApp);
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Derived class of wxTimer doesn't work

Post by cneng3 »

doublemax wrote:That looks ok. Did you call the default wxTimer constructor?
Yes, I'm not overriding the constructor. My constructor looks something like this

Code: Select all

myClass::myClass() {}
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Derived class of wxTimer doesn't work

Post by cneng3 »

PB wrote:As doublemax said, you probably did something wrong in myClass ctor.

This works as expected

Code: Select all

#include <wx/wx.h>

class myClass : public wxTimer
{
public:
    myClass() {};    
    void Notify() override { wxLogMessage("myClass::Notify"); }
};

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Test") 
    {
        wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);     
        wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));    
    
        m_myClass = new myClass();
        if (!m_myClass->Start(1000))
            wxLogError("Failed to start the timer");
    }	
private:
    myClass* m_myClass;
};

class MyApp : public wxApp
{
public:         
    bool OnInit()
    {
        (new MyFrame())->Show();               
        return true;
    }   
}; wxIMPLEMENT_APP(MyApp);
We have nearly identical code but it doesn't work for me :( :( :(
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Derived class of wxTimer doesn't work

Post by PB »

cneng3 wrote:We have nearly identical code but it doesn't work for me
Does my code works for you?

If it does, start removing parts of your code to make same as mine and once it starts working you will know where the issue is. It is probably something silly you just cannot see right now ...

If it does not there may be something wrong with your system / library build? I am on Windows BTW but that should make no difference.
Last edited by PB on Mon Jul 09, 2018 3:41 pm, edited 1 time in total.
cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Derived class of wxTimer doesn't work

Post by cneng3 »

My code just somehow magically worked!!! I didn't change anything. Thx for all the advice guys!
Post Reply