0

On the code bellow I am setting ScrolledWindows on a grid container and then setting their properties in a loop, because I want to put TextViews on the grid. I do not want to use TreeView because it will be ackward to put long or moderate texts there, which is what I am intending. Unfortunately the function get_child_at(int col,int row), for getting the attached widgets returns Gtk::Widget and for the properties I need I must do a cast for Gtk::ScrolledWindow; which I am not being able to do.

(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);
}

the last error I got was

/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*

I considered the gtkmm documentation on using casting with Glib::RefPtr

  • additionaly if I use `current_grid_child_scrolledWin = std::static_pointer_cast(*something);`, I get `.../src/test-gui.cc:94:92: error: no matching function for call to 'static_pointer_cast(Glib::RefPtr&)'`, `.../g++-v9/bits/shared_ptr_base.h:1532:5: note: candidate: 'template std::__shared_ptr<_Tp1, _Lp> std::static_pointer_cast(const std::__shared_ptr<_Tp2, _Lp>&)'` and `.../src/test-gui.cc:94:92: note: 'Glib::RefPtr' is not derived from const std::__shared_ptr<_Tp2, _Lp>` – jeferson lemos Feb 11 '21 at 20:50

1 Answers1

0

As we can see from the Gtk::Grid documentation, the return type of get_child_at is a raw pointer to a widget:

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

and not a smart pointer (in your case a Glib::Refptr). This is because the caller of get_child_at is not responsible to manage the lifetime of the object pointed to by the returned pointer. You simply get a handle, work on it and then leave it alone. Someone else is responsible to call delete on it. Here is how I would do it in your case:

#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.

Notice that nowhere is this code I am using a smart pointer. This is because when I new my Gtk::ScrolledWindow, I use Gtk::manage*. This function marks the object (here the Gtk::ScrolledWindow) as owned by its parent container widget (Here the Gtk::Grid), so you don't need to delete it manually. I becomes the container's responsibility to call delete on its children when it goes away.

*If you use a newer Gtkmm version, you will have to use make_managed<T> instead. See here.

BobMorane
  • 3,870
  • 3
  • 20
  • 42