Robotics StackExchange | Archived questions

signal 'draw' is invalid for instance ... of type 'GtkDrawingArea'

I don't understand the reason for this error.

After I compiled my code and run it I got the message signal 'draw' is invalid for instance (...) of type 'GtkDrawingArea'

I may have errors on my source code, but first I want to understand the reason for this problem.

Thanks in advance

Here goes my package.xml and makelists.txt

<?xml version="1.0"?>
<package>
  <name>pubsub</name>
  <version>0.0.0</version>
  <description>The pubsub package</description>

  <maintainer email="viki@todo.todo">viki</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>cv_bridge</build_depend>
  <build_depend>image_transport</build_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>geometry_msgs</build_depend>



  <run_depend>roscpp</run_depend>
  <run_depend>std_msgs</run_depend>
  <run_depend>sensor_msgs</run_depend>
  <run_depend>cv_bridge</run_depend>
  <run_depend>image_transport</run_depend> 
  <run_depend>message_runtime</run_depend>
  <run_depend>geometry_msgs</run_depend>

  <export>    
  </export>
</package>

cmake_minimum_required(VERSION 2.8.3)
project(pubsub)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  cv_bridge
  actionlib
  actionlib_msgs
  image_transport
  sensor_msgs
  roslaunch
  message_generation
  geometry_msgs
)

find_package(Boost REQUIRED COMPONENTS system)
find_package(PkgConfig REQUIRED)
find_package(OpenCV)
find_package(GTK2 COMPONENTS gtk)

roslaunch_add_file_check(launch)

pkg_check_modules(gtk REQUIRED gtk+-2.0)
pkg_check_modules(OpenCV REQUIRED opencv)

add_message_files(
   FILES
   msg1.msg
   main_msg.msg
   tracks.msg
   orientations.msg
   RaposaWheels.msg
   main.msg
#   Message2.msg
 )

 add_service_files(
   FILES
   demo_srv.srv
#   Service2.srv
 )

 generate_messages(
   DEPENDENCIES
   std_msgs
   actionlib_msgs
   geometry_msgs
 )

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES myVideoImageSubscriber
  CATKIN_DEPENDS roscpp std_msgs geometry_msgs 
#  DEPENDS system_lib
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${Bost_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${OpenCV_CFLAGS}
  ${GTK2_INCLUDE_DIRS}
  ${GTK2_CFLAGS}
)

add_executable(sub7 src/subscriber7.cpp)

add_dependencies(sub7 msg_pkg_generate_messages_cpp pubsub cv_bridge roscpp std_msgs sensor_msgs)

target_link_libraries(sub7
   ${catkin_LIBRARIES}
   ${Boost_LIBRARIES}
   ${OpenCV_LIBRARIES}
   ${GTK2_LIBRARIES}
)

install(TARGETS sub7
   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

I have been playing with the source code, but nothing, here is the code.

#include "ros/ros.h"
#include "std_msgs/Int16.h"
#include "std_msgs/Float64.h"
#include "std_msgs/Float32.h"
#include "pubsub/RaposaWheels.h"
#include "pubsub/main.h"
#include <geometry_msgs/Quaternion.h>
#include <std_msgs/Header.h>
#include <iostream>
#include <sstream>
#include <cairo.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define  pi  3.14159
#define draw_Wigth   180
#define draw_height  180

typedef struct orien_ang{
    float x;
    float y;
}ang;

GtkWidget* GlobalTable;
GtkWidget* darea1;
GtkWidget* darea2;
double body_roll;

ang* Gorien;

static void do_drawing(GtkWidget *widget, cairo_t *cr, gpointer user_data);
static void do_drawing2(GtkWidget *widget, cairo_t *cr, gpointer user_data);

static void do_drawing(GtkWidget *widget, cairo_t *cr, gpointer user_data)
{

    double int_x, int_y;
    double radius;
    double inset;
    double aux_ang1, aux_ang2, aux_ang3, aux_ang4;
    int i;



    int_x = draw_Wigth/2;
    int_y = draw_height/2;

    radius = draw_Wigth/3 - 5;

    cairo_set_source_rgb(cr, 0, 0, 0); //Black
    cairo_stroke(cr);

    i = 0;
    while (i < 36){
        cairo_save(cr);
        inset = (1.0/18)*radius;
        cairo_set_line_width(cr, 0.5 * cairo_get_line_width(cr));
        cairo_move_to(cr, int_x + (radius - inset)* cos(i * pi /18.0), int_y + (radius - inset)* sin(i * pi /18.0));
        cairo_line_to(cr, int_x + radius* cos(i * pi/18.0), int_y + radius* sin(i * pi/18.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    i = 1;
    while ( i < 9)
    {
        cairo_save(cr);

        inset = 0.15 * radius;
        cairo_move_to(cr, int_x + (radius - inset) * cos(i * pi/4.0), int_y + (radius-inset)*sin(i * pi / 4.0));
        cairo_line_to(cr, int_x + radius * cos(i * pi/4.0), int_y + radius*sin(i * pi / 4.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr,0, 1, 0); //Green
    cairo_move_to(cr, int_x + radius*0.6*( -1* cos((*Gorien).x)), int_y + radius*0.6*sin((*Gorien).x));
    cairo_line_to(cr, int_x + radius*0.6*cos((*Gorien).x), int_y + radius*0.6*(-1*sin((*Gorien).x)));
    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

}

static void do_drawing2(GtkWidget *widget, cairo_t *cr, gpointer user_data)
{
        char angle[10];

        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_set_font_size(cr, 14);
        cairo_move_to(cr,20,20);
        cairo_show_text(cr,"heading");
        snprintf(angle, 20, "%4.2f%s", (*Gorien).x*180/pi, "º");
        cairo_move_to(cr,20,60);
        cairo_show_text(cr,angle);

}

void number_callback(const pubsub::main::ConstPtr& msg)
{

    body_roll = msg->body_roll;
    ros::spinOnce();
    gtk_main_iteration_do(0);   


}

int main(int argc, char **argv)

{

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  Gorien=(ang *)malloc(sizeof(ang));

  (*Gorien).x = 1;
  (*Gorien).y = 12;

  darea1 = gtk_drawing_area_new();
  darea2 = gtk_drawing_area_new();
  GlobalTable = gtk_table_new (1, 2, TRUE);

  gtk_widget_set_size_request(darea1, draw_Wigth, draw_height);
  gtk_widget_set_size_request(darea2, draw_Wigth, draw_height);

  gtk_table_attach_defaults (GTK_TABLE (GlobalTable), darea1, 0, 1, 0, 1);
  gtk_table_attach_defaults (GTK_TABLE (GlobalTable), darea2, 0, 1, 1, 2);
  gtk_container_add(GTK_CONTAINER(window), GlobalTable);

  g_signal_connect(G_OBJECT(darea1), "draw", 
    G_CALLBACK(do_drawing), NULL); 
  g_signal_connect(G_OBJECT(darea2), "draw", 
    G_CALLBACK(do_drawing2), NULL); 

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 360, 180); 
  gtk_window_set_title(GTK_WINDOW(window), "GTK window");

  gtk_widget_show_all(window);

  gtk_main();


    ros::init(argc, argv,"sub7");
    ros::NodeHandle node_obj;
    ros::Rate loop_rate(100);       // Rate class instance. 
    ros::Subscriber number_subscriber = node_obj.subscribe("/republished/raposang/telemetry",10,number_callback);
    ros::spin();
    return 0;


}

Asked by Rodrigo Lourenço on 2018-07-16 13:27:28 UTC

Comments

I removed (most) of the comments from your code listing to make it easier to read.

Asked by jayess on 2018-07-16 13:53:52 UTC

Answers

Problem solve.

The main issue here is mixing GTK2.0 and GTK3.0, a few changes had to be made on this code.

1º Here we read "draw" we must have "expose-event"

2º the arguments of do_drawing not have cairo_t *cr but GdKEventExpose *event

3º create in the do_drawing the next 2 lines:

cairo_t *cr;
cr = gdk_cairo_create(widget->window);

These solved the entire problem.

Thanks.

Asked by Rodrigo Lourenço on 2018-07-17 13:40:48 UTC

Comments