C++11 wxTimer未调用重写通知()
我遇到了一个问题,我实现了一个派生的wxTimer类来覆盖Notify()调用,因为我没有使用中描述的所有者实现 当我调试运行时,我可以看到C++11 wxTimer未调用重写通知(),c++11,wxwidgets,C++11,Wxwidgets,我遇到了一个问题,我实现了一个派生的wxTimer类来覆盖Notify()调用,因为我没有使用中描述的所有者实现 当我调试运行时,我可以看到 正在实例化计时器 my\u timer\u instance->IsRunning()返回true 从未调用MyTimer::Notify() 这让我相信计时器正在设置和运行,但当它过期时,它调用的是基类Notify()过程,而不是我的重写,它没有调用Notify(),但我不知道为什么 编辑:我添加了frame->getTimer()->Notify(
- 正在实例化计时器
- my\u timer\u instance->IsRunning()返回true
- 从未调用MyTimer::Notify()
frame->getTimer()->Notify()编码>到我的应用程序,并调用正确的过程。因此,计时器在到期时不调用Notify
EDIT2:添加了这个最小的工作示例,计时器按预期工作。我将试着比较两者,看看问题出在哪里
MyApp.hpp
#pragma once
#ifndef __NONAME_H__
#define __NONAME_H__
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/statusbr.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/frame.h>
#include <wx/timer.h>
///////////////////////////////////////////////////////////////////////////
class MyTimerClass : public wxTimer
{
wxFrame* MyFrame;
public:
MyTimerClass(wxFrame* frame): MyFrame(frame) {};
void Notify() override;
};
///////////////////////////////////////////////////////////////////////////////
/// Class MyFrame1
///////////////////////////////////////////////////////////////////////////////
class MyFrame1 : public wxFrame
{
private:
protected:
wxStatusBar* m_statusBar1;
MyTimerClass* MyTimer;
public:
void StartTimer(int TimeInSeconds);
MyFrame1(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);
~MyFrame1();
};
#endif //__NONAME_H__
#include "MyApp.hpp"
#include "wx/wxprec.h"
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
///////////////////////////////////////////////////////////////////////////
void MyTimerClass::Notify()
{
MyFrame->SetStatusText("Timer popped", 0);
}
MyFrame1::MyFrame1(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(parent, id, title, pos, size, style)
{
MyTimer = new MyTimerClass(this);
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
m_statusBar1 = this->CreateStatusBar(1, wxSTB_SIZEGRIP, wxID_ANY);
this->Centre(wxBOTH);
this->StartTimer(5);
}
void MyFrame1::StartTimer(int TimeInSeconds)
{
SetStatusText("Timer started with " + std::to_string(TimeInSeconds) + " seconds.");
MyTimer->Start(TimeInSeconds * 1000);
}
MyFrame1::~MyFrame1()
{
}
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
// the application icon (under Windows it is in resources and even
// though we could still include the XPM here it would be unused)
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
enum
{
// menu items
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
// call the base class initialization method, currently it only parses a
// few common command-line options but it could be do more in the future
if (!wxApp::OnInit())
return false;
// create the main application window
MyFrame1 *frame = new MyFrame1(NULL, -1, "Test Frame");
frame->Show(true);
return true;
}
@BobbyTables
从文件中:
如果默认设置为
已使用构造函数,但未调用SetOwner()
是这样吗?您显示的代码中似乎没有任何错误(尽管我会更改一些内容,例如对我的计时器实例使用原始指针),因此问题一定出在其他地方。像往常一样,最好的办法是提出一个解决方案,如果没有这个方案,我只能猜测问题到底是什么
您正在运行事件循环吗?计时器只有在运行时才会启动,因此如果您阻止执行某些计算,就不会发生这种情况
另外,Notify()
中的frame
是什么?这是全局的吗(我宁愿将其作为参数传递给MyTimer
ctor)?因此,在模拟问题中提供的代码后,进行了以下更改:
我使用
void refreshttimer(int time,以秒为单位)
在父帧类中创建计时器,而不是让应用程序创建并传入计时器
我不明白为什么这两件事中的任何一件会改变计时器的行为,但计时器现在按预期工作。很抱歉,我无法确定一个具体的bug是问题的根源
注意:此行为是由在wxwindow线程外部调用的计时器引起的。使用wxwidgets作为GUI创建多线程程序时要小心。为了避免这个问题,因为我需要在不同的线程中调用计时器,我创建了自己的正确工作的计时器类 是的,我没有使用SetOwner(),但是我重写了MyTimer中的方法。@BobbyTables,您使用默认构造函数吗?你能发布你的计时器类定义吗?更多信息:在计时器应该弹出并检查IsRunning()后暂停10秒左右,看起来计时器实际上从未过期。因此,它有自己的SSCCE定义。看见