[英]"Name: value" widget via GTK
如何通過 GTK 制作一個如下所示的小部件?
------------------
| Text1: | 1 |
|-----------+----|
| Text2: | 10 |
|-----------+----|
| | |
| | |
| | |
------------------
'Text1', 'Text2' - 常量字符串; '1', '10' - 動態改變的值。
我應該使用什么 Gtk::Widget's ?
使用標簽制作 Gtk::Grid,對齊它們,設置列間距和列同質。
#include <gtkmm.h>
class window : public Gtk::Window {
protected:
Gtk::Box box;
Gtk::Grid grid;
Gtk::Button button;
Gtk::Label name1, name2, value1, value2;
int n_click = 0;
public:
window();
~window() = default;
void on_click() {
value1.set_label("1");
value2.set_label(n_click % 2 ? "1" : "10");
n_click++;
queue_draw();
}
};
window::window() : button("Update"),
name1("Text:"),
name2("Text2:")
{
add(box);
box.pack_start(button);
button.signal_clicked().connect(sigc::mem_fun(*this, &window::on_click));
box.pack_end(grid, Gtk::PACK_SHRINK);
grid.attach(name1, 0, 0, 2, 1);
grid.attach(value1, 2, 0, 1, 1);
grid.attach(name2, 0, 1, 2, 1);
grid.attach(value2, 2, 1, 1, 1);
name1.set_halign(Gtk::ALIGN_START);
name2.set_halign(Gtk::ALIGN_START);
value1.set_halign(Gtk::ALIGN_START);
value2.set_halign(Gtk::ALIGN_START);
grid.set_column_spacing(10);
grid.set_column_homogeneous(true);
show_all();
}
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "");
window win;
return app->run(win);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.