我用库 gtkmm 创建了一个笔记本,但我无法打开一些新标签。这是我的代码:
共有三个文件:
主要.cpp
#include <gtkmm/main.h>
#include "win.hpp"
int main(int argc, char *argv[]){
Gtk::Main app(argc, argv);
Win win;
Gtk::Main::run(win);
return EXIT_SUCCESS;
}
win.hpp :
#ifndef DEF_WIN
#define DEF_WIN
#include <gtkmm.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <sstream>
class Win : public Gtk::Window{
public:
Win();
private:
Gtk::Notebook m_notebook;
Gtk::Grid m_grid1;
Gtk::Grid m_grid2;
};
#endif
这是最后一个文件:
#include "win.hpp"
Win::Win(){
maximize();
set_title("Test");
Gtk::VBox *boxV = Gtk::manage(new Gtk::VBox(false,0));
add(*boxV);
Gtk::MenuBar *barreMenu = Gtk::manage(new Gtk::MenuBar);
boxV->pack_start(*barreMenu, Gtk::PACK_SHRINK);
Gtk::MenuItem *menuItemFile = Gtk::manage(new Gtk::MenuItem("_File",true));
barreMenu->append(*menuItemFile);
Gtk::Menu *menuFile = Gtk::manage(new Gtk::Menu);
menuItemFile->set_submenu(*menuFile);
Gtk::ImageMenuItem *menuNew = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::NEW));
menuFile->append(*menuNew);
menuNew->signal_activate().connect([this]() {
m_notebook.append_page(m_grid1,"Hello");
});
m_notebook.popup_enable();
Gtk::Grid* grid1 = Gtk::manage(new Gtk::Grid());
grid1->set_border_width(0);
grid1->set_row_spacing(0);
Gtk::Label *title = Gtk::manage(new Gtk::Label());
title->set_markup("<b><span size='xx-large'>Welcome !");
title->set_hexpand(true);
grid1->attach(*title,0,0,10,1);
Gtk::Button *commencer = Gtk::manage(new Gtk::Button("Start"));
grid1->attach(*commencer,4,7,2,3);
commencer->set_hexpand(true);
commencer->signal_clicked().connect([this]() {
m_notebook.append_page(m_grid2,"Hey");
});
m_notebook.append_page(*grid1, "New worksheet");
boxV->pack_start(m_notebook);
show_all();
}
代码编译没有任何问题。但是当我执行代码并单击“开始”或“新建”时,我没有看到新的选项卡,我不知道为什么,因为我在信号中输入了这个:
m_notebook.append_page(m_grid1,"Hello");
还有这个:
m_notebook.append_page(m_grid2,"Hey");
最佳答案
每个小部件都需要是 shown创建后。
*grid1
与 window.show_all()
一起显示,但在执行该方法时 m_grid1
和 m_grid2
没有父级并且不在 window
的层次结构中。
#include <gtkmm.h>
#include <iostream>
int main()
{
auto Application = Gtk::Application::create();
Gtk::Window window;
window.maximize();
window.set_title("Test");
Gtk::Grid m_grid1, m_grid2;
Gtk::VBox *boxV = Gtk::manage(new Gtk::VBox(false,0));
window.add(*boxV);
Gtk::MenuBar *barreMenu = Gtk::manage(new Gtk::MenuBar);
boxV->pack_start(*barreMenu, Gtk::PACK_SHRINK);
Gtk::MenuItem *menuItemFile = Gtk::manage(new Gtk::MenuItem("_File",true));
barreMenu->append(*menuItemFile);
Gtk::Menu *menuFile = Gtk::manage(new Gtk::Menu);
menuItemFile->set_submenu(*menuFile);
Gtk::ImageMenuItem *menuNew = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::NEW));
menuFile->append(*menuNew);
Gtk::Notebook m_notebook;
menuNew->signal_activate().connect([&]() {
m_notebook.append_page(m_grid1,"Hello");
m_grid1.show();
});
m_notebook.popup_enable();
Gtk::Grid* grid1 = Gtk::manage(new Gtk::Grid());
grid1->set_border_width(0);
grid1->set_row_spacing(0);
Gtk::Label *title = Gtk::manage(new Gtk::Label());
title->set_markup("<b><span size='xx-large'>Welcome !</span></b>");
title->set_hexpand(true);
grid1->attach(*title,0,0,10,1);
Gtk::Button *commencer = Gtk::manage(new Gtk::Button("Start"));
grid1->attach(*commencer,4,7,2,3);
commencer->set_hexpand(true);
commencer->signal_clicked().connect([&]{
m_notebook.append_page(m_grid2,"Hey");
m_grid2.show();
});
m_notebook.append_page(*grid1, "New worksheet");
boxV->pack_start(m_notebook);
window.show_all();
Application->run(window);
return 0;
}
此外,我怀疑您希望在每次单击按钮时都创建新的网格。现在您的代码多次添加相同的网格。
#include <gtkmm.h>
#include <iostream>
int main()
{
auto Application = Gtk::Application::create();
Gtk::Window window;
window.maximize();
window.set_title("Test");
Gtk::Grid m_grid1, m_grid2;
Gtk::Notebook m_notebook;
auto addGrid = [&]{
Gtk::Grid* grid1 = Gtk::manage(new Gtk::Grid());
Gtk::Label *title = Gtk::manage(new Gtk::Label("Welcome 2"));
grid1->attach(*title,0,0,10,1);
m_notebook.append_page(*grid1,"Hello");
grid1->show();
};
Gtk::VBox *boxV = Gtk::manage(new Gtk::VBox(false,0));
window.add(*boxV);
Gtk::MenuBar *barreMenu = Gtk::manage(new Gtk::MenuBar);
boxV->pack_start(*barreMenu, Gtk::PACK_SHRINK);
Gtk::MenuItem *menuItemFile = Gtk::manage(new Gtk::MenuItem("_File",true));
barreMenu->append(*menuItemFile);
Gtk::Menu *menuFile = Gtk::manage(new Gtk::Menu);
menuItemFile->set_submenu(*menuFile);
Gtk::ImageMenuItem *menuNew = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::NEW));
menuFile->append(*menuNew);
menuNew->signal_activate().connect(addGrid);
m_notebook.popup_enable();
m_grid1.set_border_width(0);
m_grid1.set_row_spacing(0);
Gtk::Label *title = Gtk::manage(new Gtk::Label());
title->set_markup("<b><span size='xx-large'>Welcome !</span></b>");
title->set_hexpand(true);
m_grid1.attach(*title,0,0,10,1);
Gtk::Button *commencer = Gtk::manage(new Gtk::Button("Start"));
m_grid1.attach(*commencer,4,7,2,3);
commencer->set_hexpand(true);
commencer->signal_clicked().connect(addGrid);
m_notebook.append_page(m_grid1, "New worksheet");
boxV->pack_start(m_notebook);
window.show_all();
Application->run(window);
return 0;
}
关于c++ - 如何使用 Gtkmm 打开新标签页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684073/