无法使用来自 GtkSpinButton 小部件的用户输入修改全局整数变量
Can't modify global integer variable with user input from a GtkSpinButton widget
我使用 GTK 和 glade 编写了一个简单的程序,允许用户在 GtkSpinButton 中输入数字。一旦用户按下回车键(激活信号),全局变量应该更改为用户输入的任何内容。不幸的是,这不起作用,当我在主函数中打印出全局变量时,它只是吐出一个零。当我用一个字符串和一个 GtkEntry 框做同样的事情时,它工作得很好。这是源代码:
//gtkspinbox test program
// g++ -rdynamic -c lol.cpp $(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0)
// g++ lol.o -o lol $(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0)
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <math.h>
#include <ctype.h>
using namespace std;
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <limits>
#include <cmath>
#include <regex>
GtkWidget *lolwindow;
GtkWidget *lolfixed;
GtkWidget *lolentry;
GtkBuilder *builder;
int lol;
extern "C" void on_lolentry_activate(GtkEntry *e, gpointer user_data){
lol = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(lolentry));
gtk_widget_destroy(lolwindow);
}
int main(int argc, char **argv){
gtk_init(&argc, &argv);//initializes gtk
builder = gtk_builder_new_from_file ("lol.glade");//reads the xml file generated by glade
gtk_builder_connect_signals(builder, NULL);
lolwindow = GTK_WIDGET(gtk_builder_get_object(builder, "lolwindow"));
g_signal_connect(lolwindow, "destroy", G_CALLBACK(gtk_main_quit), NULL);
lolfixed = GTK_WIDGET(gtk_builder_get_object(builder, "lolfixed"));
lolentry = GTK_WIDGET(gtk_builder_get_object(builder, "lolentry"));
gtk_widget_show(lolwindow);
gtk_main();
cout << lol << "\n";
}
和空地文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkAdjustment" id="adjustment1">
<property name="lower">1</property>
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkWindow" id="lolwindow">
<property name="can_focus">False</property>
<property name="title" translatable="yes">enter a number lmao</property>
<child type="titlebar">
<placeholder/>
</child>
<child>
<object class="GtkFixed" id="lolfixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSpinButton" id="lolentry">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustment1</property>
<signal name="activate" handler="on_lolentry_activate" swapped="no"/>
</object>
<packing>
<property name="x">161</property>
<property name="y">95</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
感谢您的帮助!
显然,glade 中调整变量的最大值被设置为零,这就是全局变量被设置为零的原因。一旦我为最大值输入了不同的值,问题就消失了。
我使用 GTK 和 glade 编写了一个简单的程序,允许用户在 GtkSpinButton 中输入数字。一旦用户按下回车键(激活信号),全局变量应该更改为用户输入的任何内容。不幸的是,这不起作用,当我在主函数中打印出全局变量时,它只是吐出一个零。当我用一个字符串和一个 GtkEntry 框做同样的事情时,它工作得很好。这是源代码:
//gtkspinbox test program
// g++ -rdynamic -c lol.cpp $(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0)
// g++ lol.o -o lol $(pkg-config --cflags --libs gtk+-3.0 gmodule-2.0)
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <math.h>
#include <ctype.h>
using namespace std;
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <limits>
#include <cmath>
#include <regex>
GtkWidget *lolwindow;
GtkWidget *lolfixed;
GtkWidget *lolentry;
GtkBuilder *builder;
int lol;
extern "C" void on_lolentry_activate(GtkEntry *e, gpointer user_data){
lol = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(lolentry));
gtk_widget_destroy(lolwindow);
}
int main(int argc, char **argv){
gtk_init(&argc, &argv);//initializes gtk
builder = gtk_builder_new_from_file ("lol.glade");//reads the xml file generated by glade
gtk_builder_connect_signals(builder, NULL);
lolwindow = GTK_WIDGET(gtk_builder_get_object(builder, "lolwindow"));
g_signal_connect(lolwindow, "destroy", G_CALLBACK(gtk_main_quit), NULL);
lolfixed = GTK_WIDGET(gtk_builder_get_object(builder, "lolfixed"));
lolentry = GTK_WIDGET(gtk_builder_get_object(builder, "lolentry"));
gtk_widget_show(lolwindow);
gtk_main();
cout << lol << "\n";
}
和空地文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkAdjustment" id="adjustment1">
<property name="lower">1</property>
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkWindow" id="lolwindow">
<property name="can_focus">False</property>
<property name="title" translatable="yes">enter a number lmao</property>
<child type="titlebar">
<placeholder/>
</child>
<child>
<object class="GtkFixed" id="lolfixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSpinButton" id="lolentry">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="adjustment">adjustment1</property>
<signal name="activate" handler="on_lolentry_activate" swapped="no"/>
</object>
<packing>
<property name="x">161</property>
<property name="y">95</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
感谢您的帮助!
显然,glade 中调整变量的最大值被设置为零,这就是全局变量被设置为零的原因。一旦我为最大值输入了不同的值,问题就消失了。