在底部的正常操作区域有一个带有ok和cancel的首选项对话框。我也有一个浏览按钮,我不想把它放在对话框的底部。
所以我有我的浏览按钮在顶部,但我不能设置它的响应ID。按钮属性中的响应ID字段显示为灰色。我试着把我所有的对话框小部件移到操作区,但这也没有用。
如何使我的浏览按钮返回响应ID而不将其移动到对话框的底部?
编辑:据我所知,gtk对话框不是为按钮设计的,除非它们在底部。我决定放弃对话框的想法,转而使用另一个窗口。这样我就可以使用现有的代码来挂钩按钮事件……我希望。
在编译以下测试应用程序时遇到麻烦。知道是什么问题吗?
控制台输出:
g++ main.cpp examplewindow.cpp subwindow.cpp -o testsubwindow.exe -I . %GTKMM_INCLUDES% %GTKMM_LIBS%
AppDataLocalTempccekbJuk.o:examplewindow.cpp:(.text+0x1b0d):
undefined reference to `ExampleSubWindow::~ExampleSubWindow()'
AppDataLocalTempccekbJuk.o:examplewindow.cpp:(.text+0x1b1e):
undefined reference to `ExampleSubWindow::~ExampleSubWindow()'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
LocalTempccekbJuk.o: bad reloc address 0xf in section `.text$_ZN
4sigc8internal8slot_repC2EPFPvS2_ES4_S4_[__ZN4sigc8internal8slot_repC2EPFPvS2_ES4_S4_]'
collect2.exe: error: ld returned 1 exit status
main.cpp:
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
examplewindow.h:
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
void on_button_clicked();
void on_button2_clicked();
void on_about_dialog_response(int response_id);
//Child widgets:
Gtk::Box m_VBox;
Gtk::Label m_Label;
Gtk::ButtonBox m_ButtonBox;
Gtk::ButtonBox m_ButtonBox2;
Gtk::Button m_Button;
Gtk::Button m_Button2;
Gtk::Window m_SubWindow;
Gtk::AboutDialog m_Dialog;
};
class ExampleSubWindow : public Gtk::Window
{
public:
ExampleSubWindow();
virtual ~ExampleSubWindow();
protected:
void on_button3_clicked();
Gtk::Box s_VBox;
Gtk::ButtonBox s_ButtonBox3;
Gtk::Button s_Button3;
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cpp:
#include "examplewindow.h"
#include <iostream>
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::ORIENTATION_VERTICAL),
m_Label("The AboutDialog is non-modal. "
"You can select parts of this text while the AboutDialog is shown."),
m_ButtonBox(Gtk::ORIENTATION_VERTICAL),
m_ButtonBox2(Gtk::ORIENTATION_VERTICAL),
m_Button("Show AboutDialog"),
m_Button2("Show SubWindow")
{
set_title("Gtk::AboutDialog example");
add(m_VBox);
m_VBox.pack_start(m_Label);
m_Label.set_line_wrap(true);
m_Label.set_selectable(true);
m_VBox.pack_start(m_ButtonBox);
m_ButtonBox.pack_start(m_Button);
m_Button.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_clicked) );
m_VBox.pack_start(m_ButtonBox2);
m_ButtonBox2.pack_start(m_Button2);
m_Button2.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_button2_clicked));
m_Dialog.set_transient_for(*this);
m_Dialog.set_program_name("Example application");
m_Dialog.set_version("1.0.0");
m_Dialog.set_copyright("Murray Cumming");
m_Dialog.set_comments("This is just an example application.");
m_Dialog.set_license("LGPL");
m_Dialog.set_website("http://www.gtkmm.org");
m_Dialog.set_website_label("gtkmm website");
std::vector<Glib::ustring> list_authors;
list_authors.push_back("Murray Cumming");
list_authors.push_back("Somebody Else");
list_authors.push_back("AN Other");
m_Dialog.set_authors(list_authors);
m_Dialog.signal_response().connect(
sigc::mem_fun(*this, &ExampleWindow::on_about_dialog_response) );
show_all_children();
// The widget must be realized and mapped before grab_focus() is called.
// That's why it's called after show_all_children().
m_Button.grab_focus();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_about_dialog_response(int response_id)
{
std::cout << response_id
<< ", close=" << Gtk::RESPONSE_CLOSE
<< ", cancel=" << Gtk::RESPONSE_CANCEL
<< ", delete_event=" << Gtk::RESPONSE_DELETE_EVENT
<< std::endl;
if((response_id == Gtk::RESPONSE_CLOSE) ||
(response_id == Gtk::RESPONSE_CANCEL) )
{
m_Dialog.hide();
}
}
void ExampleWindow::on_button_clicked()
{
m_Dialog.show();
//Bring it to the front, in case it was already shown:
m_Dialog.present();
}
void ExampleWindow::on_button2_clicked()
{
ExampleSubWindow subWindow;
subWindow.show();
}
subwindow.cpp
#include "examplewindow.h"
#include <iostream>
ExampleSubWindow::ExampleSubWindow()
: s_VBox(Gtk::ORIENTATION_VERTICAL),
s_ButtonBox3(Gtk::ORIENTATION_VERTICAL),
s_Button3("Test activation")
{
add(s_VBox);
s_VBox.pack_start(s_ButtonBox3);
s_ButtonBox3.pack_start(s_Button3);
s_Button3.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleSubWindow::on_button3_clicked) );
}
void ExampleSubWindow::on_button3_clicked()
{
std::cout << "Working yet?" << std::endl;
}
编辑:没关系,我知道我的小项目出了什么问题。仍然不知道这个例子有什么问题,但对我来说最主要的是子窗口需要用"new"创建,否则它将被直接删除。
ExampleSubWindow *subWindow = new ExampleSubWindow();
没关系,我知道我的小项目出了什么问题。仍然不知道这个例子有什么问题,但对我来说最主要的是子窗口需要用"new"创建,否则它将被直接删除。
ExampleSubWindow *subWindow = new ExampleSubWindow();
我认为这是Glade中的一个bug。Glade中的Gtk::Dialog有一个子Gtk::Box,而这个子Gtk::ButtonBox又有一个子Gtk::ButtonBox。如果您将Gtk::Button's添加到ButtonBox中,它们将允许启用和设置响应ID。如果您以任何其他方式向方框添加Gtk::按钮(直接或间接地,例如,向方框添加Gtk::Grid,然后向该网格添加按钮),则Glade将不允许您启用响应ID或设置值。
但是,如果在XML中这样做,它将按预期工作。例如,我向ButtonBox添加了一个Button,并设置了它的Response ID。我还在对话框中添加了一个网格,然后在该网格中添加了按钮(如上所述,不能启用响应ID)。我保存了.glade文件,然后用文本编辑器编辑它。我进入Dialog的"action-widgets"属性,并添加了"action-widget"子元素,代表我添加到Grid中的按钮。这个对话框工作得很好:(1)单击ButtonBox中的按钮按预期工作,(2)单击网格中的按钮也按预期工作(对话框发出"响应"信号,我在.glade文件中手动设置的响应ID从Dialog.run()返回)。