c++ - 我怎样才能将文件拖放到哪个 gtkmm 图像小部件

标签 c++ drag-and-drop event-handling gtk gtkmm

....
std::vector<Gtk::TargetEntry> listTargets;
listTargets.push_back( Gtk::TargetEntry("STRING") );
listTargets.push_back( Gtk::TargetEntry("text/plain") );

image1->drag_dest_set(listTargets);
image1->signal_drag_data_received().connect(sigc::mem_fun(*this,
              &mainWindow::drop_event) );

image2->drag_dest_set(listTargets);
image2->signal_drag_data_received().connect(sigc::mem_fun(*this,
                  &mainWindow::drop_event) );
....

和我的 drop&drop 事件处理函数:

void mainWindow::drop_event(
        const Glib::RefPtr<Gdk::DragContext>& context, int, int,
        const Gtk::SelectionData& selection_data, guint, guint time)
{
    std::cout << selection_data.get_data_as_string()  << std::endl;
}

我可以使用此代码获取“拖动到图像小部件”的文件位置。输出是这样的:

file:////opt/google/chrome/google-chrome.desktop
file:////var/www/index.html
file:///opt/libreoffice4.1/LICENSE.html

没关系,我可以。但是,我怎样才能得到:文件放到哪个图像(image1 或 image2 小部件)是这样的:

dropped to **image1** : file:////opt/google/chrome/google-chrome.desktop
dropped to **image2** : file:////var/www/index.html
dropped to **image1** : file:///opt/libreoffice4.1/LICENSE.html

谢谢...

最佳答案

sigc allows you to bind extra arguments to your handlers.

处理程序变为:

void mainWindow::drop_event(
        const Glib::RefPtr<Gdk::DragContext>& context, int, int,
        const Gtk::SelectionData& selection_data, guint, guint time, 
        Glib::ustring whichImage)
{
    std::cout << "dropped to" << whichImage << ":" << selection_data.get_data_as_string()  << std::endl;
}

然后 connect 是:

image1->signal_drag_data_received().connect(sigc::bind<Glib::ustring>(sigc::mem_fun(*this,
              &mainWindow::drop_event), "image1" ));

image2->signal_drag_data_received().connect( sigc::bind<Glib::ustring>(sigc::mem_fun(*this,
                  &mainWindow::drop_event), "image2"));

关于c++ - 我怎样才能将文件拖放到哪个 gtkmm 图像小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18592729/

相关文章:

c# - 在 WPF 中拖放文件不起作用

jquery - 与 jQuery 同步 'radiogroups'

c# - 查找已将哪个事件处理程序分配给对象 WPF 上的事件

c++ - 矩阵中的运算符重载 : how to overload () and = at the same time

javascript - GWT-Drag and Drop for draggable Viewport

.net - ListView 的 DragEnter、DragOver、DragDrop 事件未引发 (AllowDrop=True)

python - wxPython:向多个小部件发送信号

c++ - 如何在编译时检查函数是否具有默认参数值?

c++ - 从派生类方法设置基对象

c++ - 在字符串中搜索字符