wxTimer example
Posted: 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.
Thanks.
Official forum for the wxWidgets Cross-Platform GUI Toolkit
https://forums.wxwidgets.org/
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
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.
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.
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".
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 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.
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);
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"
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.
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.
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".
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
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
}
?>
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.
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
Code: Select all
wxString a, b, c;
a = "Hello"; b = "World";
c = a + " " + b;
Code: Select all
wxFFile file("myfile.txt");
wxString fileContent;
if ( file.IsOpened() && file.ReadAll(&fileContent) )
fileContent.Trim();