如何在 gtkmm 上将 Gtk::Widget 转换为 GtK::ScrolledWindow?

Posted

技术标签:

【中文标题】如何在 gtkmm 上将 Gtk::Widget 转换为 GtK::ScrolledWindow?【英文标题】:How can I cast Gtk::Widget to GtK::ScrolledWindow on gtkmm? 【发布时间】:2021-02-11 20:12:34 【问题描述】:

在下面的代码中,我在网格容器上设置 ScrolledWindows,然后在循环中设置它们的属性,因为我想将 TextViews 放在网格上。我不想使用 TreeView,因为在此处放置长文本或适度文本会很糟糕,这正是我的意图。不幸的是,用于获取附加小部件的函数 get_child_at(int col,int row) 返回 Gtk::Widget 并且对于我需要的属性,我必须为 Gtk::ScrolledWindow 进行强制转换;这是我做不到的。

(some code)
descriptors->attach(* (new Gtk::ScrolledWindow ()), 0,0);
descriptors->attach(* (new Gtk::ScrolledWindow ()), 1,0);
descriptors->attach(* (new Gtk::ScrolledWindow ()), 2,0);

(some other code)

for(int i = 0; i < 3; i++)

    //Glib::RefPtr<Gtk::ScrolledWindow> * disposable_pointer = new Glib::RefPtr<Gtk::ScrolledWindow>();
    //disposable_pointer = descriptors->get_child_at (i,0);
    //Glib::RefPtr<Gtk::ScrolledWindow> disposable_pointer = Glib::RefPtr<Gtk::Widget>::cast_dynamic(*descriptors->get_child_at (i,0));
    //Glib::RefPtr<Gtk::ScrolledWindow>Glib::RefPtr<Gtk::ScrolledWindow>::cast_dynamic
    //current_grid_child_scrolledWin = Glib::RefPtr<Gtk::ScrolledWindow>cast_dynamic(descriptors->get_child_at (i,0));
    //current_grid_child_scrolledWin = disposable_pointer->get();
    
    Glib::RefPtr<Gtk::Widget> * something = new Glib::RefPtr<Gtk::Widget>(descriptors->get_child_at (i,0));
    
    Glib::RefPtr<Gtk::ScrolledWindow> disposable_pointer = *something;

    current_grid_child_scrolledWin->set_vadjustment (Glib::RefPtr<Gtk::Adjustment>());
    current_grid_child_scrolledWin->set_hadjustment (Glib::RefPtr<Gtk::Adjustment>());
    current_grid_child_scrolledWin->set_policy (Gtk::PolicyType::POLICY_AUTOMATIC, Gtk::PolicyType::POLICY_AUTOMATIC);
    current_grid_child_scrolledWin->set_vexpand (true);
    current_grid_child_scrolledWin->set_hexpand (true);
    current_grid_child_scrolledWin->set_margin_end (10);
    current_grid_child_scrolledWin->set_margin_bottom (10);
    current_grid_child_scrolledWin->set_visible (true);

我得到的最后一个错误是

/usr/include/glibmm-2.4/glibmm/refptr.h:309:31: error: invalid conversion from 'Gtk::Widget*' to 'Gtk::ScrolledWindow*' [-fpermissive]
309 |   pCppObject_(src.operator->())
|                               ^
|                               |
|                               Gtk::Widget*

我考虑过the gtkmm documentation on using casting with Glib::RefPtr

【问题讨论】:

另外,如果我使用current_grid_child_scrolledWin = std::static_pointer_cast&lt;Gtk::ScrolledWindow&gt;(*something);,我会得到.../src/test-gui.cc:94:92: error: no matching function for call to 'static_pointer_cast&lt;Gtk::ScrolledWindow&gt;(Glib::RefPtr&lt;Gtk::Widget&gt;&amp;)'.../g++-v9/bits/shared_ptr_base.h:1532:5: note: candidate: 'template&lt;class _Tp, class _Tp1, __gnu_cxx::_Lock_policy _Lp&gt; std::__shared_ptr&lt;_Tp1, _Lp&gt; std::static_pointer_cast(const std::__shared_ptr&lt;_Tp2, _Lp&gt;&amp;)'.../src/test-gui.cc:94:92: note: 'Glib::RefPtr&lt;Gtk::Widget&gt;' is not derived from const std::__shared_ptr&lt;_Tp2, _Lp&gt; 【参考方案1】:

Gtk::Grid documentation 可以看出,get_child_at 的返回类型是一个指向小部件的原始指针:

Widget* Gtk::Grid::get_child_at(int column,
                                int row 
                               ) 

而不是智能指针(在您的情况下为Glib::Refptr)。这是因为get_child_at 的调用者不负责管理返回指针指向的对象的生命周期。您只需找到一个句柄,对其进行处理,然后将其搁置一边。其他人负责致电delete。在你的情况下,我会这样做:

#include <gtkmm.h>
#include <iostream>

int main(int argc, char *argv[])

    // Initialize Gtkmm:
    auto app = Gtk::Application::create(argc, argv, "so.question.q66162108");

    // Create a grid and attach a scrolled window:
    Gtk::Grid container;
    Gtk::ScrolledWindow scrolledWindow;

    // Add the scrolled window to the grid. Make sure the container is responsible
    // of deleting it! Otherwise, remeber to call delete on it:
    container.attach(*Gtk::manage(new Gtk::ScrolledWindow()), 0, 0, 1, 1);

    // Here, you get a raw pointer to a widget:
    Gtk::Widget* pWidget = container.get_child_at(0, 0);

    // We cast it to its most derived type:
    Gtk::ScrolledWindow* pScrolledWindow = dynamic_cast<Gtk::ScrolledWindow*>(pWidget);

    // If the cast fail (i.e. not a ScrolledWindow at (0, 0), then the
    // casted pointer will be set to nullptr:
    if(pScrolledWindow)
    
        std::cout << "Casting worked" << std::endl;

        // Handle properties here...
        pScrolledWindow->set_visible (true);

        // Set other properties here...
    

    return 0;
 // At this point the container is destroyed and since Gtk::manage
  // was used, the scrolled window will be automatically deleted.

请注意,这段代码我没有使用智能指针。这是因为当我new 我的Gtk::ScrolledWindow 时,我使用Gtk::manage*。此函数将对象(此处为 Gtk::ScrolledWindow)标记为其父容器小部件(此处为 Gtk::Grid)所拥有,因此您无需手动删除它。当容器消失时,我成为容器的责任在其子代上调用delete

*如果您使用较新的 Gtkmm 版本,则必须改用make_managed&lt;T&gt;。见here。

【讨论】:

以上是关于如何在 gtkmm 上将 Gtk::Widget 转换为 GtK::ScrolledWindow?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 GTK3 中禁用按钮

在 Gtk::Grid 中移动 Gtk::Widget 的位置

gtk_container_add:断言“GTK_IS_WIDGET(小部件)”失败

GTK3:gtk_window_resize() 与 gtk_widget_set_size_request()

将 Gtk.Widget 打印到打印机的最佳方法是啥?

鼠标按下时 Gtk::Widget on_enter_notify?