wxWidgets Drop Files eventhandler初始化问题(无效static_cast)
wxWidgets Drop Files eventhandler initialization problem (invalid static_cast)
我正在尝试制作一个简单的程序,使用带有 (wxWidgets) wxListCtrl 的 C++ 将文件拖放到列表中。
- 我使用的工具:Code::Blocks 20.03、MinGW 17.1、wxWidgets 3.1.3、wxFormBuilder 3.9.0。
- OS: Windows 10 专业
我尝试了以下操作:(在 DnD_SimpleFrame class 构造函数中调用的函数 SetupGUI() 中。)
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
删除文件(从资源管理器)时调用的函数是:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
m_listCtrl1->Connect(...) 上的构建消息;行是:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:\SDKs\wx313\include\wx\event.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:\SDKs\wx313\include\wx\event.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我在这里做错了什么(或者忘记了)?
如果仔细看报错信息:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
你可以看到它说它不能将某些东西从一种类型转换为另一种类型。
如果您是 C++ 新手,这些类型可能难以解析,但您应该能够看到这些是函数指针(实际上,是指向 class 成员的指针)。查看您的代码,您还应该能够看到第一个是它们是您的 DnD_SimpleFrame::OnDropFiles
函数的类型,因此问题是它无法转换为预期的函数类型。
最后,这样做的原因只是你的函数没有正确的参数类型:你应该给 wxWidgets 一些取 wxDropFilesEvent&
的东西,但你的函数取而代之的是 wxArrayString&
.您必须将其更改为采用 wxDropFilesEvent& event
,然后使用事件对象的 GetFiles()
方法来获取实际文件。
关于一个完全不同的主题,您应该在新代码中使用 Bind()
而不是 Connect()
。如果您正在学习使用后者的教程,那么这是一个很好的迹象,表明它 非常 已经过时了。您的代码仍然无法使用 Bind()
进行编译,但错误消息会稍微简单一些。
我正在尝试制作一个简单的程序,使用带有 (wxWidgets) wxListCtrl 的 C++ 将文件拖放到列表中。
- 我使用的工具:Code::Blocks 20.03、MinGW 17.1、wxWidgets 3.1.3、wxFormBuilder 3.9.0。
- OS: Windows 10 专业
我尝试了以下操作:(在 DnD_SimpleFrame class 构造函数中调用的函数 SetupGUI() 中。)
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
删除文件(从资源管理器)时调用的函数是:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
m_listCtrl1->Connect(...) 上的构建消息;行是:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:\SDKs\wx313\include\wx\event.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:\SDKs\wx313\include\wx\event.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:\Data\__C++\wxApps\DnD_Simple\DnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我在这里做错了什么(或者忘记了)?
如果仔细看报错信息:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
你可以看到它说它不能将某些东西从一种类型转换为另一种类型。
如果您是 C++ 新手,这些类型可能难以解析,但您应该能够看到这些是函数指针(实际上,是指向 class 成员的指针)。查看您的代码,您还应该能够看到第一个是它们是您的 DnD_SimpleFrame::OnDropFiles
函数的类型,因此问题是它无法转换为预期的函数类型。
最后,这样做的原因只是你的函数没有正确的参数类型:你应该给 wxWidgets 一些取 wxDropFilesEvent&
的东西,但你的函数取而代之的是 wxArrayString&
.您必须将其更改为采用 wxDropFilesEvent& event
,然后使用事件对象的 GetFiles()
方法来获取实际文件。
关于一个完全不同的主题,您应该在新代码中使用 Bind()
而不是 Connect()
。如果您正在学习使用后者的教程,那么这是一个很好的迹象,表明它 非常 已经过时了。您的代码仍然无法使用 Bind()
进行编译,但错误消息会稍微简单一些。