wxTimer example Topic is solved
wxTimer example
Can anyone provide a simple wxTimer example please. For example, a timer event is triggered every 10 sec.
Thanks.
Thanks.
Re: wxTimer example
Hi,
Regards,
David
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.Can anyone provide a simple wxTimer example please
Regards,
David
Re: wxTimer example
Thanks, David. That sample code is very helpful.
Re: wxTimer example
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.Can anyone provide a simple wxTimer example pleaseWhen you want to find examples of use, your first stop should be the samples that come with the wx source.
/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.
Re: wxTimer example
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:There are 88 folders in /samples/ and 162 *.cpp files within those folders, so this is not very helpful
So you were even told of a file with a good example - that does sound like helpful to me.Palloy wrote:/samples/statbar/statbar.cpp is 1029 lines long and I couldn't understand it.
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.
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 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".
Re: wxTimer example
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.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.
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.
Re: wxTimer example
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.Palloy wrote:I am not very familiar with C++, and complex examples about some other control don't help.
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);
BTW, when searching for a usage of class example, you should search for the class name, e.g. wxTimer in your case.
Re: wxTimer example
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:Yes, I should really understand it, but I don't, I'm a beginner.
Well, as I wrote earlier, you might consider looking at the latest code version of, say, power sample.Palloy wrote:I have grepped looking for "Connect" in all the .cpp files, and of those 363 occurences, none of them reference "wxEVT_TIMER"
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.
That was not the part that I replied to, see the quoted text that preceded it.Palloy wrote:I think the lack of a /samples/timer/timer.cpp is a legitimate complaint.
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.)
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 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.
Re: wxTimer example
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.
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.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".
Re: wxTimer example
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...
Re: wxTimer example
No, really, 5 days is way too little for learning C++.Palloy wrote:You have no idea of how much effort I have put in, you are just guessing. I have spent 5 days
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.
Re: wxTimer example
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:
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.
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
}
?>
I am surprised no one has adopted this simple approach to writing apps with wxWidgets.
Re: wxTimer example
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.
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.
Re: wxTimer example
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".... this will probably not lead to a 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().
Re: wxTimer example
No offense, but let me be blunt here.Palloy wrote: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".... this will probably not lead to a 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
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;
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();
Anyway, if you have further issues, please open a new thread.
BTW, what about Python?