如何解决进度条 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!
输出:
我冒昧地简化了一下。最重要的是,我查看了您的内存管理模型,并通过使用 delete
完全删除了 Glib::RefPtr
的使用,它可以为您做到这一点。
您可以使用:
g++ -rdynamic main.cpp -o hello `pkg-config gtkmm-3.0 --libs --cflags`
注意:我是用 Gtkmm 3.22 写的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。