c++ - 如何使用 Gtkmm 打开新标签页?

标签 c++ gtkmm gtkmm3

我用库 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创建后。

*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;
}

关于c++ - 如何使用 Gtkmm 打开新标签页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684073/

相关文章:

c++ - 是否可以使用 gtkmm 定义 GTK+ 类型?

c++ - Gtk::StatusIcon 系统托盘对齐问题

c++ - 更改字节顺序的最快方法

c++ - Crypto++ 的 undefined reference ,尽管它已链接并在其他项目中工作

c++ - 带有 Gio 文件监视器的 gtkmm 应用程序

c++ - 跟踪/断点陷阱(核心转储)Gtkmm

c++ - 我怎样才能使用 gtk_window_set_transient_for()?

c++ - Gtk - 如何设置框大小?

c++ - 为什么 vector<T>::emplace_back 使 T 具有已删除的复制构造函数,但编译失败?

c++ - 为什么我的结构不能有 boost::variant 类型的成员,但可以有 vector<boost::variant> 类型的成员?