我创建了一个包含4个标签的应用程序。我想更改两个标签的值(“wynik”和“wynik1”)。当我按下键盘上的左键时,“wynik”标签的值应该增加,当我按下键减少时。当我按下右键“wynik1”标签的值应该增加,当我按下键向下减少。 我的代码:
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
gint count = 0;
char buf[0];
char buf1[0];
gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer wynik);
gboolean
on_key_press1 (GtkWidget *widget, GdkEventKey *event, gpointer wynik);
int main( int argc, char * argv[] ) {
GtkWidget * okno;
GtkWidget * kontener;
GtkWidget * etykieta;
GtkWidget * etykieta1;
GtkWidget * wynik;
GtkWidget * wynik1;
gtk_init( & argc, & argv );
okno = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_keep_above(GTK_WINDOW(okno), TRUE);
gtk_window_set_default_size( GTK_WINDOW( okno ), 300, 200 );
gtk_window_set_position( GTK_WINDOW( okno ), GTK_WIN_POS_CENTER );
kontener = gtk_fixed_new();
gtk_container_add( GTK_CONTAINER( okno ), kontener );
char * str = " <span fgcolor='#007560'><span font='20'><b>PIA</b></span></span>";
etykieta = gtk_label_new( NULL );
gtk_label_set_markup( GTK_LABEL( etykieta ), str );
gtk_fixed_put( GTK_FIXED( kontener ), etykieta, 0, 0 );
char * str1 = " <span fgcolor='#9a1262'><span font='20'><b>PER</b></span></span>";
etykieta1 = gtk_label_new( NULL );
gtk_label_set_markup( GTK_LABEL( etykieta1 ), str1 );
gtk_fixed_put( GTK_FIXED( kontener ), etykieta1, 65, 0 );
char * wyn = " <span fgcolor='#007560'><span font='26'><b>0</b></span></span>";
wynik = gtk_label_new( NULL );
gtk_label_set_markup( GTK_LABEL( wynik ), wyn );
gtk_fixed_put( GTK_FIXED( kontener ), wynik, 3, 30 );
char * wyn1 = " <span fgcolor='#9a1262'><span font='26'><b>0</b></span></span>";
wynik1 = gtk_label_new( NULL );
gtk_label_set_markup( GTK_LABEL( wynik1 ), wyn1 );
gtk_fixed_put( GTK_FIXED( kontener ), wynik1, 68, 30 );
g_signal_connect( G_OBJECT( okno ), "destroy", G_CALLBACK( gtk_main_quit ), NULL );
g_signal_connect (G_OBJECT (okno), "key_press_event", G_CALLBACK (on_key_press), wynik);
g_signal_connect (G_OBJECT (okno), "key_press_event", G_CALLBACK (on_key_press1), wynik1);
gtk_widget_show_all( okno );
gtk_main();
return 0;
}
gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer wynik)
{
switch (event->keyval)
{case GDK_S:
case GDK_Left:
count++;
std::ostringstream os;
os << "<span font='26'><b>" << count << "</b></span>";
gtk_label_set_markup( GTK_LABEL( wynik ), os.str().c_str() );
break;
case GDK_Down:
count--;
std::ostringstream os;
os << "<span font='26'><b>" << count << "</b></span>";
gtk_label_set_markup( GTK_LABEL( wynik ), os.str().c_str() );
break;
default:
return FALSE;
}
return FALSE;
}
gboolean
on_key_press1 (GtkWidget *widget, GdkEventKey *event, gpointer wynik1)
{
switch (event->keyval)
{case GDK_S: // add this line
case GDK_Right:
count++;
std::ostringstream os1;
os1 << "<span font='26'><b>" << count << "</b></span>";
gtk_label_set_markup( GTK_LABEL( wynik1 ), os1.str().c_str() );
break;
case GDK_Up:
count--;
std::ostringstream os1;
os1 << "<span font='26'><b>" << count << "</b></span>";
gtk_label_set_markup( GTK_LABEL( wynik1 ), os1.str().c_str() );
break;
default:
return FALSE;
}
return FALSE;
}
不幸的是我得到了错误:
> ||=== nowygt3, Debug ===| F:\c++\nowygt3\main.c||In function > 'on_key_press':| F:\c++\nowygt3\main.c|67|error: expected expression > before ':' token| F:\c++\nowygt3\main.c|68|error: 'os' undeclared > (first use in this function)| F:\c++\nowygt3\main.c|68|note: each > undeclared identifier is reported only once for each function it > appears in| F:\c++\nowygt3\main.c|73|error: duplicate label 'std'| > F:\c++\nowygt3\main.c|67|note: previous definition of 'std' was here| > F:\c++\nowygt3\main.c|73|error: expected expression before ':' token| > F:\c++\nowygt3\main.c|67|warning: label 'std' defined but not used > [-Wunused-label]| F:\c++\nowygt3\main.c||In function 'on_key_press1':| > F:\c++\nowygt3\main.c|91|error: expected expression before ':' token| > F:\c++\nowygt3\main.c|92|error: 'os1' undeclared (first use in this > function)| F:\c++\nowygt3\main.c|97|error: duplicate label 'std'| > F:\c++\nowygt3\main.c|91|note: previous definition of 'std' was here| > F:\c++\nowygt3\main.c|97|error: expected expression before ':' token| > F:\c++\nowygt3\main.c|91|warning: label 'std' defined but not used > [-Wunused-label]|
请帮帮我。我是初学者,但我自己尝试做所有事情,现在我没有想法。
答案 0 :(得分:0)
看起来在抱怨
std::ostringstream os1;
#include <iostream>
吗?
答案 1 :(得分:0)
在case
语句的特定switch
内创建具有自动存储持续时间的对象:
case GDK_S:
case GDK_Left:
std::ostringstream os;
...
break;
您应该定义范围,os
的生命周期将与之绑定:
case GDK_S:
case GDK_Left:
{
std::ostringstream os;
...
break;
}
除此之外,您还应该#include <sstream>
能够使用std::ostringstream
。