Благодаря ответу на этот другой вопрос мне удалось создать пустое окно, следуя желаемому подходу (заголовочный файл myWindow.h для объявления класса mainWindow, другой файл myWindow.cpp для определения класса, main.cpp и файл builder.ui с виджетами).
Моя цель — заполнить окно виджетами, возможно, начав с простого кнопка. Но из-за плохого знания C++ я не могу.
После некоторых исследований я попытался включить код из подобных примеров, в частности отсюда, и это то, что у меня пока получилось. :
Код: Выделить всё
myWindow.hКод: Выделить всё
#pragma once
#include
#include
#include
#include
class MainWindow : public Gtk::Window
{
public:
MainWindow(BaseObjectType* cobject, const Glib::RefPtr& p_builder);
protected:
Glib::RefPtr m_builder;
};
Код: Выделить всё
myWindow.cppКод: Выделить всё
#include "myWindow.h"
MainWindow::MainWindow(BaseObjectType* cobject, const Glib::RefPtr& p_builder)
: Gtk::Window(cobject)
{
this->m_builder = p_builder;
auto pButton = p_builder->get_widget("button1");
}
Код: Выделить всё
main.cppКод: Выделить всё
#include
#include
#include
#include
#include "myWindow.h"
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv);
auto builder = Gtk::Builder::create_from_file("builder.ui");
MainWindow* window = nullptr;
builder->get_widget_derived("mainWindow", window);
try
{
// You then show the window by using you handle (i.e window).
app->run(*window);
}
catch(const Gtk::BuilderError& bdre)
{
// specific handling for BuilderError
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78999215/how-to-add-widgets-e-g-a-button-to-a-window-in-a-c-application-using-gtkmm[/url]
Мобильная версия