如何使用Gtkmm打开新标签?

时间:2017-01-16 19:55:31

标签: c++ gtkmm gtkmm3

我创建了一个带有gtkmm库的笔记本,但我无法打开一些新标签。这是我的代码:

有三个文件:

的main.cpp

#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

win.hpp:

#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");

还有这个:

ad.show()

1 个答案:

答案 0 :(得分:0)

创建后,每个小部件都需要shown

*grid1window.show_all()一起显示,但在执行该方法时,m_grid1m_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;
}