c++g++gtkmmgladegtkmm3

can't associate Gtk::DrawingArea with its form from the glade file on c++


I'm writing a test task on c++ for a team where everything is written in gtk. I used to write purely under Qt and never gtk/gtkmm. The program has widgets whose operation I seem to have figured out, but I can’t get DrawingArea to draw anything, or at least contact its protégé from the glade file (unlike other widgets). I understood this from this output in the console ** (main:11189): CRITICAL **: 22:58:23.000: Gtk::Builder::get_widget(): dynamic_cast<> failed. I realized that the problem occurs at this moment, inside the main window constructor builder->get_widget("scene_ui", scene); the class itself looks like this(all code is stolen)

#pragma once
 
#include <gtkmm/drawingarea.h>
 
class Scene : public Gtk::DrawingArea
{
public:
  Scene();
  virtual ~Scene();
 
protected:
  virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
};
#include "Scene.hpp"
#include <cairomm/context.h>
 
Scene::Scene()
{
}
 
Scene::~Scene()
{
}
 
bool Scene::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
  Gtk::Allocation allocation = get_allocation();
  const int width = allocation.get_width();
  const int height = allocation.get_height();
  const int lesser = MIN(width, height);
 
  int xc, yc;
  xc = width / 2;
  yc = height / 2;
 
  cr->set_line_width(lesser * 0.02);
 
  // в первую очередь рисуется простая открытая дуга
  cr->save();
  cr->arc(width / 3.0, height / 4.0, lesser / 4.0, -(M_PI / 5.0), M_PI);
  cr->close_path();  
  cr->set_source_rgb(0.0, 0.8, 0.0);
  cr->fill_preserve();
  cr->restore();  
  cr->stroke();
  cr->save();
  cr->arc(xc, yc, lesser / 4.0, 0.0, 2.0 * M_PI); 
  cr->set_source_rgba(0.0, 0.0, 0.8, 0.6); 
  cr->fill_preserve();
  cr->restore();
  cr->stroke();
  double ex, ey, ew, eh;
  ex = xc;
  ey = 3.0 * height / 4.0;
  ew = 3.0 * width / 4.0;
  eh = height / 3.0;
 
  cr->save();
 
  cr->translate(ex, ey);
  cr->scale(ew / 2.0, eh / 2.0);
 
  cr->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI);
 
  cr->set_source_rgba(0.8, 0.0, 0.0, 0.7);
  cr->fill_preserve();
  cr->restore();
  cr->stroke();
 
  return true;
}

I use gtkmm-3.0 with glade and g++ on Debian How can I get him to draw??? I tried copy code from docs, but there all from gtkmm4


Solution

  • the problem was that I had to use get_widget_derived instead of get_widget and on top of that change the constructor of the Scene class to

    Scene::Scene(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder):
            Gtk::DrawingArea(cobject)
    

    and also include the entire gtkmm.h because without it the constructor does not know about Gtk::Builder