[英]Progress Bar gtkmm Glade
使用按鈕並處理其事件很容易,但我無法與進度條交互。 如何從 Glade 文件中獲取Gtk::ProgressBar
並設置其分數?
我使用 Gtkmm 3.24,我想在單擊按鈕時更新進度條。 這是我的林間空地文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkApplicationWindow" id="MainWindow">
<property name="can-focus">False</property>
<property name="title" translatable="yes">My Main Window</property>
<property name="default-width">200</property>
<property name="default-height">150</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="LittleButton">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="MyProgressBar">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
我的main.cpp
文件
#include <memory>
#include <gtkmm.h>
class MainWindow : public Gtk::ApplicationWindow
{
public:
MainWindow(BaseObjectType* obj, Glib::RefPtr<Gtk::Builder> const& builder)
: Gtk::ApplicationWindow(obj)
, builder{builder}
{
}
virtual ~MainWindow() = default;
private:
Glib::RefPtr<Gtk::Builder> builder;
};
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create(argc, argv, "de.engelmarkus.example");
auto builder = Gtk::Builder::create_from_file("MyGladeGUI.glade");
MainWindow* mwindow = nullptr;
builder->get_widget_derived("MainWindow", mwindow);
auto littleButton = builder->get_object("LittleButton");
auto r = app->run(*mwindow);
delete mwindow;
return r;
}
我從命令行構建我的應用程序:
g++ -rdynamic main.cpp -o hello $(pkg-config gtkmm-3.0 --libs --cflags)
這是執行您想要的代碼的代碼:
#include <memory>
#include <gtkmm.h>
constexpr int ERROR = 1;
int main(int argc, char* argv[])
{
auto app = Gtk::Application::create(argc, argv, "de.engelmarkus.example");
auto builder = Gtk::Builder::create_from_file("MyGladeGUI.glade");
// Get the window. First a `Glib::Object` handle to it and then cast it
// to the `Gtk::ApplicationWindow` type:
Glib::RefPtr<Glib::Object> mainWindowAsObject = builder->get_object("MainWindow");
Glib::RefPtr<Gtk::ApplicationWindow> mainWindow = Glib::RefPtr<Gtk::ApplicationWindow>::cast_dynamic(mainWindowAsObject);
if(!mainWindow)
{
return ERROR;
}
// At this point, the main window is valid, and as bonus, wrapped in a smart
// pointer, which means you do not have to manage its memory yourself. Now get
// the button. First get a `Glib::Object` handle to it and then cast it to the
// `Gtk::Button` type:
Glib::RefPtr<Glib::Object> buttonAsObject = builder->get_object("LittleButton");
Glib::RefPtr<Gtk::Button> button = Glib::RefPtr<Gtk::Button>::cast_dynamic(buttonAsObject);
if(!button)
{
return ERROR;
}
// At this point, the button is valid. We have to get the progress bar. You get
// the idea now ;):
Glib::RefPtr<Glib::Object> pBarAsObject = builder->get_object("MyProgressBar");
Glib::RefPtr<Gtk::ProgressBar> pBar = Glib::RefPtr<Gtk::ProgressBar>::cast_dynamic(pBarAsObject);
if(!pBar)
{
return ERROR;
}
// At this point, both the button and the progress bar have been extracted
// from the builder and are valid. We can now connect the button's 'clicked'
// signal handler:
button->signal_clicked().connect([&pBar](){
const double currentFraction = pBar->get_fraction();
pBar->set_fraction(currentFraction + 0.10);
});
// The syntax is ugly the basically:
// 1. Call `get()` to retreive the stored pointer
// 2. Call operator* on this pointer to get a reference
return app->run(*(mainWindow.get()));
} // mainWindow automatically deleted here by its RefPtr container!
Output:
我冒昧地把它簡化了一點。 最重要的是,我查看了您的 memory 管理 model 並完全刪除了delete
的使用,通過使用Glib::RefPtr
可以為您做到這一點。
您可以使用以下方式構建:
g++ -rdynamic main.cpp -o hello `pkg-config gtkmm-3.0 --libs --cflags`
注意:我使用 Gtkmm 3.22 編寫了這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.