3

我一直在搜索很多,但我仍然找不到一个很好的例子来说明如何在同一个应用程序中使用GTK. 我的程序在里面,C++但我不介意举个例子C来帮助我理解这个原理。

因此,基本思想是创建我自己的派生对象,Gtk::Window而不是Gtk::Dialog. Dialog有一个运行方法可以完美地打开一个模式弹出窗口,但是对于我想要做的事情来说它不够灵活。有谁知道当我单击程序中的按钮时我会如何生成一个新窗口?

例如:

void MainWindow::on_button_clicked()
{

    NewWindow window;
    //Some code to display that window and stay in a loop until told to return
}

NewWindow 的来源Gtk::Window是这样的:

class NewWindow : public Gtk::Window
{

   //Normal stuff goes here

}

任何事情都会有所帮助......我在这里真的很困惑!

4

2 回答 2

5

Another way to have a new window is to create a pointer to a Gtk window variable(Gtk::Window* about_window_;) then set the Gtk window variable to a new instance of the other window (about_window_ = new Window;), after that show the new window (about_window_->show();). Below is a full example of this:

class AboutWindow : public Gtk::Window
{
public:
    AboutWindow();
    ~AboutWindow();

protected:
    Gtk::Label lbl_;
};

AboutWindow::AboutWindow()
{
    this->set_default_size(100, 100);
    this->set_title("About");

    lbl_.set_label("About label");
    this->add(lbl_);

    this->show_all_children();
}

AboutWindow::~AboutWindow()
{

}

class MainWindow : public Gtk::Window
{
public:
    MainWindow();
    virtual ~MainWindow();

protected:
    void onButtonClicked();
    void aboutWinClose();

    Gtk::Button button_;
    Gtk::Label lbl_;
    Gtk::Box box_;
    Gtk::AboutWindow* aboutw_;
};

MainWindow::MainWindow()
{
    this->set_default_size(100, 100);
    box_.set_orientation(Gtk::ORIENTATION_VERTICAL);
    this->add(box_);
    box_.pack_start(lbl_);

    lbl_.set_label("a test");

    button_.set_label("Open About Window");
    box_.pack_end(button_);

    button_.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onButtonClicked));

    aboutw_ = 0;

    this->show_all_children();
}

MainWindow::~MainWindow()
{

}

void MainWindow::onButtonClicked()
{
    if(aboutw_ != 0)
        return;

    aboutw_ = new AboutWindow;
    aboutw_->signal_hide().connect(sigc::mem_fun(*this, &MainWindow::aboutWinClose));
    aboutw_->show();
}

void MainWindow::aboutWinClose()
{
    aboutw_ = 0;
}

Added for reference.

于 2014-01-08T23:58:27.130 回答
2

If you don't want the new window to be modal, then simply create it, show() it, and return from your main window's method without entering a loop.

于 2013-03-17T12:29:10.240 回答