wxTimer example 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.
2607
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Feb 03, 2012 4:00 pm

wxTimer example

Post by 2607 » Tue Feb 07, 2012 5:47 am

Can anyone provide a simple wxTimer example please. For example, a timer event is triggered every 10 sec.

Thanks.

DavidHart
Site Admin
Site Admin
Posts: 3917
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: wxTimer example

Post by DavidHart » Tue Feb 07, 2012 10:25 am

Hi,
Can anyone provide a simple wxTimer example please
When you want to find examples of use, your first stop should be the samples that come with the wx source. In this case, have a look at the 'statbar' sample.

Regards,

David

2607
Knows some wx things
Knows some wx things
Posts: 32
Joined: Fri Feb 03, 2012 4:00 pm

Re: wxTimer example

Post by 2607 » Wed Feb 08, 2012 5:43 am

Thanks, David. That sample code is very helpful.

Palloy
Earned a small fee
Earned a small fee
Posts: 15
Joined: Wed Oct 11, 2017 3:16 am

Re: wxTimer example

Post by Palloy » Fri Aug 31, 2018 11:03 pm

Can anyone provide a simple wxTimer example please
When you want to find examples of use, your first stop should be the samples that come with the wx source.
There are 88 folders in /samples/ and 162 *.cpp files within those folders, so this is not very helpful unless there is a /samples/timer/timer.cpp , which there isn't.
/samples/statbar/statbar.cpp is 1029 lines long and I couldn't understand it. It uses the "wxDECLARE_EVENT_TABLE" macro and doesn't use Connect() or Bind() way of doing things, which I think is more "natural".

I still need a simple wxTimer example, a short one, and one which is concentrating on wxTimer only, with all its ways of being used.
Thanks in advance.

catalin
Moderator
Moderator
Posts: 1527
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTimer example

Post by catalin » Sat Sep 01, 2018 2:50 am

Palloy wrote:There are 88 folders in /samples/ and 162 *.cpp files within those folders, so this is not very helpful
This is not a legitimate complaint. You know there are examples there and you should go ahead and search them yourself. How about grep?
Palloy wrote:/samples/statbar/statbar.cpp is 1029 lines long and I couldn't understand it.
So you were even told of a file with a good example - that does sound like helpful to me.
It's unfortunate that you don't understand it, but IMO you really should. It will be much faster to read the code in the samples (or other examples Google will find for you) than asking here and waiting for someone else to write it for you.
Palloy wrote:It uses the "wxDECLARE_EVENT_TABLE" macro and doesn't use Connect() or Bind() way of doing things, which I think is more "natural".
You can also find examples of using Bind in the same samples. There are plenty of them, especially in the latest version of the code.

Palloy
Earned a small fee
Earned a small fee
Posts: 15
Joined: Wed Oct 11, 2017 3:16 am

Re: wxTimer example

Post by Palloy » Sat Sep 01, 2018 4:15 am

It's unfortunate that you don't understand it, but IMO you really should. It will be much faster to read the code in the samples (or other examples Google will find for you) than asking here and waiting for someone else to write it for you.
Yes, I should really understand it, but I don't, I'm a beginner. Although I am familiar with wxPHP, (which has died from a lack of support), I am not very familiar with C++, and complex examples about some other control don't help. I have grepped looking for "Connect" in all the .cpp files, and of those 363 occurences, none of them reference "wxEVT_TIMER", like I was expecting. I must be on the wrong track.

I think the lack of a /samples/timer/timer.cpp is a legitimate complaint. There must be hundreds of people like me, who struggle to get going with wxWidgets for want of a simple example to follow, or for someone to put a little effort into helping us through the mist.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer example

Post by PB » Sat Sep 01, 2018 6:10 am

Palloy wrote:I am not very familiar with C++, and complex examples about some other control don't help.
I believe this the core of the issue. I am sorry, but if you are not proficient at the programming language, you will find doing most things quite difficult. C++ is not considered a simple language, compared to e.g. to BASIC. Additionally, wxWidgets is quite complex library.

One just has to study, when I did it I started with the official documentation and only then went to samples, not the other way around... E.g., it would be difficult to understand how timer works if you do not understand the event handling system. If you do, then using timer is extremely easy, as it is a very simple class.

E.g., it is possible if I were to post a very simple example of wxTimer like the one bellow, you still may have trouble understanding it either because of lack of knowledge of C++ (and the code emphasises briefness over proper C++) or the library.

Code: Select all

#include <wx/wx.h>
#include <wx/timer.h>

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "wxTimer example")
    {       
        wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

        m_startStopTimerBtn = new wxButton(this, wxID_ANY, "&Start timer");
        m_startStopTimerBtn->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnStartStopTimer, this);        
        mainSizer->Add(m_startStopTimerBtn, 0, wxEXPAND | wxALL , 5);
   
        wxTextCtrl* logCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, 
            wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);        
        mainSizer->Add(logCtrl, 1, wxEXPAND | wxALL , 5);
        wxLog::SetActiveTarget(new wxLogTextCtrl(logCtrl));        
     
        SetSizer(mainSizer);         

        m_timer.Bind(wxEVT_TIMER, &MyFrame::OnTimer, this);
    }	
private:
    static const int timerInterval = 1000; // 1000 ms

    wxTimer   m_timer;
    wxButton* m_startStopTimerBtn;

    void OnStartStopTimer(wxCommandEvent&)
    {
        if ( m_timer.IsRunning() )
        {
            m_timer.Stop();            
            wxLogMessage("Timer stopped.");
            m_startStopTimerBtn->SetLabel("&Start timer");
        }
        else
        {
            m_timer.Start(timerInterval);
            wxLogMessage("Timer started.");
            m_startStopTimerBtn->SetLabel("&Stop timer");
        }
    }

    void OnTimer(wxTimerEvent&)
    {
        wxLogMessage("OnTimer called.");
    }    
};

class MyApp : public wxApp
{
public:	
	bool OnInit()
	{
        (new MyFrame)->Show();
        return true;
	}
}; wxIMPLEMENT_APP(MyApp);
Anyway, it is neither possible nor sensible fo wxWidgets to have an example for each and every class, considering the number of classes and their purpose...

BTW, when searching for a usage of class example, you should search for the class name, e.g. wxTimer in your case.

catalin
Moderator
Moderator
Posts: 1527
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTimer example

Post by catalin » Sat Sep 01, 2018 7:25 am

Palloy wrote:Yes, I should really understand it, but I don't, I'm a beginner.
That is perfectly fine, but IMO you should spend time with those samples until you understand them, maybe start with simpler ones if it makes things easier. There are more complicated things out there than wxTimer use, and you will hit them sooner or later.
Palloy wrote:I have grepped looking for "Connect" in all the .cpp files, and of those 363 occurences, none of them reference "wxEVT_TIMER"
Well, as I wrote earlier, you might consider looking at the latest code version of, say, power sample.
But that was not the point. The point was that you have 2 things there: the use of a wxTimer, and using Bind() to hook up to an event. You can find examples of each one of them in the samples.
Palloy wrote:I think the lack of a /samples/timer/timer.cpp is a legitimate complaint.
That was not the part that I replied to, see the quoted text that preceded it.
And if wxW lacks something, this is not the place to complain about. This is a user forum, and maintainers seldomly come here, if ever. (Another thing worth mentioning is the fact that wxW being open source, one should not complain about missing features, but rather contribute them or ask whether someone else can. However my personal opinion remains that wxTimer use is sufficiently exemplified right now.)
Palloy wrote:There must be hundreds of people like me, who struggle to get going with wxWidgets for want of a simple example to follow, or for someone to put a little effort into helping us through the mist.
I hope there are even more, but I don't think that many are really struggling. Some c++ knowledge and the abundance of code out there that uses wxW apparently is enough for most people. There is also not a little, but a lot of effort put into helping others, just skim through this forum to convince yourself, take a look at the mailing list archives, stackoverflow etc. But you need to put "a little effort" yourself, and not settle with "I found it but I didn't understand it so I need a better one".

Palloy
Earned a small fee
Earned a small fee
Posts: 15
Joined: Wed Oct 11, 2017 3:16 am

Re: wxTimer example

Post by Palloy » Sat Sep 01, 2018 8:09 am

Thanks PB, I can understand that, more or less, although I am surprised the compiler didn't error the use of m_timer on line 22 (and 32) as "not declared in this scope". Also you have semicolons after the } of your classes. I shall have to go back and read all about scope. Now I need a lie down.
Catalin wrote:
But you need to put "a little effort" yourself, and not settle with "I found it but I didn't understand it so I need a better one".
You have no idea of how much effort I have put in, you are just guessing. I have spent 5 days compiling my simple example hundreds of times, I have searched thousands of pages for help (a lot on the diabolical error messages from g++). Your suggestion to read and understand all of /samples/ before asking for help is unsuitable as a solution for any beginner.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer example

Post by PB » Sat Sep 01, 2018 8:24 am

Palloy, no offense but, based on your comments from the post above, I suggest learning basics of C++ before trying to tackle wxWidgets. If you are proficient at PHP, this should be rather easy as the syntax is mostly quite similar. Perhaps there is a decent "C++ for PHP programmers" guide somewhere...

catalin
Moderator
Moderator
Posts: 1527
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTimer example

Post by catalin » Sat Sep 01, 2018 11:17 am

Palloy wrote:You have no idea of how much effort I have put in, you are just guessing. I have spent 5 days
No, really, 5 days is way too little for learning C++.

And no, I didn't suggest you read all samples, only a few of the ones that turn up when searching for the features you need. But now that I keep reading your replies, I think reading and indeed understanding all of them would serve you very good.

Palloy
Earned a small fee
Earned a small fee
Posts: 15
Joined: Wed Oct 11, 2017 3:16 am

Re: wxTimer example

Post by Palloy » Sat Sep 01, 2018 10:11 pm

In writing a "how to" for wxPHP for beginners, I identified the minimum code necessary to implement the basic controls, and all its actual parameters, and put it in a "snippets" file. The app can then be constructed by copy-pasting the snippets, without wasting any time on the PHP language syntax. I expect to write my C++ app the same way. So for wxTimer() I have:

Code: Select all

<?php  //snippets file

// timer configuration
$timer_function = "OnTimer";
$timer_interval = (1000);   // milliseconds

// timer create
$timer = new wxTimer();
$timer->Connect(wxEVT_TIMER, array(wxID_TOP, $timer_function);
$timer->Start($timer_interval);

// timer function
function OnTimer()	// note: can't use the $timer_function string here
{   // actions
}
?>
So all I need is a basic framework of a C++ app and the wxTimer snippet.
I am surprised no one has adopted this simple approach to writing apps with wxWidgets.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer example

Post by PB » Sun Sep 02, 2018 6:46 am

My two cents

TBH, I would say that the approach described above sounds very bad.

But this is quite common mistake, trying to bring the habits from a programming language one is familiar with to a new one. Going from a scripting language to a compiled one, and C++ in particular where declarations and definitions are split into two files, this will probably not lead to a good code.

Instead of trying to write PHP in C++ you should check how proper C++ code looks, I think there's quite a lot of examples.

Palloy
Earned a small fee
Earned a small fee
Posts: 15
Joined: Wed Oct 11, 2017 3:16 am

Re: wxTimer example

Post by Palloy » Sun Sep 02, 2018 8:27 am

... this will probably not lead to a good code.
You are right I'm trying to write PHP in C++, that's because I want to avoid C++ as much as possible. I have been coding for 51 years now, in PLAN (ICL machine code), Fortran-4, Algol-68, COBOL, BASIC, Pascal, PHP and probably a few others, and C++ seems to be the worst of the lot. It can't even append two strings together, nor can it do: rtrim(file_get_contents(filename)) . Show me that in "good code".

Using your example as a basis, I have now coded and successfully compiled my app. But when I run it, it immediately seg faults without even entering OnInit(). #-o

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2117
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer example

Post by PB » Sun Sep 02, 2018 9:26 am

Palloy wrote:
... this will probably not lead to a good code.
You are right I'm trying to write PHP in C++, that's because I want to avoid C++ as much as possible. I have been coding for 51 years now, in PLAN (ICL machine code), Fortran-4, Algol-68, COBOL, BASIC, Pascal, PHP and probably a few others, and C++ seems to be the worst of the lot. It can't even append two strings together, nor can it do: rtrim(file_get_contents(filename)) . Show me that in "good code".

Using your example as a basis, I have now coded and successfully compiled my app. But when I run it, it immediately seg faults without even entering OnInit(). #-o
No offense, but let me be blunt here.

Sure C++ is not perfect, no language is. OTOH, PHP is often used as an example of one of the worst langauge designs. Anyway, as in this very thread you admitted that you lack even the very basic understanding of C++, you must know you are poorly qualified to judge it. Obviously, there's little sense in comparing a scripting language used mostly for serving web pages to performance oriented compiled language.

For example, this is one of the ways how to join two strings

Code: Select all

wxString a, b, c;

a = "Hello"; b = "World";
c = a + " " + b;
Not that hard, is it?

And this is one way to read a text file into a string (please notice that using filename without a path, relying on the CWD, is a bad practice)

Code: Select all

wxFFile file("myfile.txt");
wxString fileContent;

if ( file.IsOpened() && file.ReadAll(&fileContent) )
    fileContent.Trim();
Without a context, the code above does not make any sense, as it would trim only the last line of the file.

Anyway, if you have further issues, please open a new thread.

BTW, what about Python?

Post Reply