E
E
Eugene2020-10-15 18:56:13
GTK+
Eugene, 2020-10-15 18:56:13

Why is glade not being loaded into my class?

Guys, I ask you not to hurt me, I'm learning the pros and faced such a disaster. I create a window with markup from a glade file like this:

#include <gtkmm.h>
#include <iostream>
#include "./win.cpp"

int main (int argc, char *argv[]){
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
  auto refBuilder = Gtk::Builder::create();
  Gtk::Window* TestWin;
  //MainWindow* TestWin;

  try{
      refBuilder->add_from_file("./test.glade");
  }
  catch(const Glib::FileError& ex){
      std::cerr << "!! FileError: " << ex.what() << std::endl;
  }
  catch(const Glib::MarkupError& ex){
      std::cerr << "!! MarkupError: " << ex.what() << std::endl;
  }
  catch(const Gtk::BuilderError& ex){
      std::cerr << "!! BuilderError: " << ex.what() << std::endl;
  }

  refBuilder->get_widget("window_id", TestWin);
  if(TestWin)
    return app->run(*TestWin);

  else{
    std::cout << "Ошибка загрузки виджета." << std::endl;
    return 1;
  }
}


And oddly enough, it compiles the code and even works, loads the markup without any problems and displays the desired window.
The problem appears when I try to inherit my class and already pass the markup file to it. The class itself so far is practically no different, created solely for educational purposes:
class MainWindow : public Gtk::Window{
public:
    virtual ~MainWindow(){
        std::cout << "Окно закрыто." << std::endl;
    }

protected:
    void on_button_clicked(){ //Обработчик сигналов от кнопки
        std::cout << "Hello World" << std::endl;
    }
};


And now if in the main() function we comment out the line "Gtk::Window* TestWin;" and uncomment "MainWindow* TestWin;", in an attempt to create a window from its class, the compiler issues the following:
** (program:22060): CRITICAL **: 18:50:50.811: Gtk::Builder::get_widget(): dynamic_cast<> failed.
Widget loading error.

He sincerely tried to google, but something didn’t work at all to understand what he needed from me ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-10-15
@jenya92

In case someone runs into the same problem, I read the official manuals for a long time and was able to understand what the jamb is. The constructor must be inherited too. Approximately like this:

MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade) : Gtk::Window(cobject);

After that, everything started! :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question