Как исправить "Gtk-CRITICAL **: 00: 18: 45.249: gtk_widget_queue_draw_area: утверждение 'GTK_IS_WIDGET (widget)' не выполнено" - PullRequest
0 голосов
/ 23 ноября 2018

Я пишу программу на GTK3 C, используя Glade, которая будет рисовать прямоугольник при нажатии кнопки.Рисует прямоугольник, но с проблемами.

Я получаю это в терминале при вызове функции рисования прямоугольника:

(Logika.run:8416): Gtk-CRITICAL **: 00:30:59.112: gtk_widget_queue_draw_area: assertion 'GTK_IS_WIDGET (widget)' failed

Существует визуальное представление проблемы (я нажал только один раз) (извините за низкий fps There is visual representation of the problem (I only clicked once) (sorry for low fps) Как видите, он рисует прямоугольник, но с большой задержкой.

Я отследил проблему до "gtk_widget_queue_draw_area", однако понятия не имею, как это исправить. Когда яиспользовал этот код без Glade, он работал без проблем. Код полон других вещей (например, таймер), поэтому, пожалуйста, игнорируйте эти части. Пожалуйста, обратите внимание на "Cairo Draw материал". Есть код C:

#include <gtk/gtk.h>


#include <string.h>
#include <stdio.h>
#include <math.h>



//==============================================================Timer=stuff==================================================================================
unsigned char isTimerRunning = 0;
int tempx = 0;
int tempy = 0;




gint TimerCallback (gpointer data)
{



    if (isTimerRunning == 1)
    {

        g_print("Timer click\n");

        //timer test
        if (tempx > 1300)
        {
            tempx = 0;
            tempy = tempy + 5;

        }
        //draw_brush (DrawArea, tempx, tempy);
        //draw_rectangle (DrawArea, tempx, tempy, 5, 5, 0.5, 0.5, 0);
        tempx = tempx + 5;
        //tempy = tempy + 5;








        return 1;
    } else
    {
        return 0;
    }

}

void StartTimer ()
{
    if (isTimerRunning == 0)
    {
        isTimerRunning = 1;
        g_timeout_add (100, TimerCallback, NULL);  //17 = 60fps

    }

}

void StopTimer ()
{
    isTimerRunning = 0;
}




//=======================================================================


//====================Cairo=Draw=stuff============================
static cairo_surface_t *surface = NULL;
GtkWidget *MainDrawArea;

static void clear_surface (void)
{
  cairo_t *cr;

  cr = cairo_create (surface);

  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);

  cairo_destroy (cr);
}

/* Create a new surface of the appropriate size to store our scribbles */
gboolean configure_event_cb (GtkWidget *widget,GdkEventConfigure *event, gpointer data)
{
  if (surface)
    cairo_surface_destroy (surface);

  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                               CAIRO_CONTENT_COLOR,
                                               gtk_widget_get_allocated_width (widget),
                                               gtk_widget_get_allocated_height (widget));

  /* Initialize the surface to white */
  clear_surface ();

  /* We've handled the configure event, no need for further processing. */
  return TRUE;
}

/* Redraw the screen from the surface. Note that the ::draw
 * signal receives a ready-to-be-used cairo_t that is already
 * clipped to only draw the exposed areas of the widget
 */
gboolean draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
{
  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);

  return FALSE;
}

/* Draw a rectangle on the surface at the given position */
static void draw_brush (GtkWidget *widget, gdouble x, gdouble y)
{
  cairo_t *cr;

  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);

  cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  cairo_fill (cr);

  cairo_destroy (cr);

  /* Now invalidate the affected region of the drawing area. */
  gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
}



//========================




int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "Resources/GUI_design.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);



    //g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);




    gtk_widget_show(window);                
    gtk_main();

    return 0;
}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}


void TestButton_clicked()
{
    g_print("Test button is pressed\n");

    //draw_rectangle (DrawArea, 50, 50, 20, 20, 0.5, 0.5, 0);
    //draw_callback(DrawArea,cr2);
    draw_brush (MainDrawArea, 100, 100);





}

void Debug_startTimer_activate()
{
    g_print("Timer start\n");
    StartTimer ();
}

void Debug_stopTimer_activate()
{
    g_print("Timer stop\n");
    StopTimer ();
}





gboolean Test_event ()
{
    g_print("Test Event\n");
    //printf ("print f test", 20);
    //sprintf("print f test ", "%d", event->x);
    //Test_event


}

//Reģistrē peles klikšķi
gboolean button_press_event_cb (GtkWidget *widget, GdkEventButton *event, gpointer data)
//gboolean button_press_event_cb (GdkEventButton *event)
{

    printf("[Click] Mouse X: %f\n", event->x);
    printf("[Click] Mouse Y: %f\n", event->y);

    return TRUE;



}

//Reģistrē peles kustību
gboolean motion_notify_event_cb (GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
    printf("[Move] Mouse X: %f\n", event->x);
    printf("[Move] Mouse Y: %f\n", event->y);

    return TRUE;
}

Код дизайна поляны:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkImage" id="NOT_gate_image">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="pixbuf">Standard_gates_images/Not_gate.png</property>
  </object>
  <object class="GtkPopover" id="StandartGatesChooseButton_popover1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox" id="StandartGatesChooseButton_popover1_box">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Not vārti</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="image">NOT_gate_image</property>
            <property name="always_show_image">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">OR vārti</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">AND vārti</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkImage">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="stock">gtk-missing-image</property>
  </object>
  <object class="GtkImage">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="stock">gtk-missing-image</property>
  </object>
  <object class="GtkWindow" id="window_main">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Logika komponentu simulators</property>
    <property name="default_width">1920</property>
    <property name="default_height">1080</property>
    <property name="icon">icon.png</property>
    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox" id="MainBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="MenuBar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Datne</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">imagemenuitem</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="image">__glade_unnamed_27</property>
                        <property name="use_stock">False</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-open</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-save-as</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">R_ediģēt</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-cut</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-copy</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-paste</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Skats</property>
                <property name="use_underline">True</property>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Palīdzība</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="Debug">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Debug</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">Start timer</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_stock">False</property>
                        <signal name="activate" handler="Debug_startTimer_activate" swapped="no"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem">
                        <property name="label">Stop timer</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_stock">False</property>
                        <signal name="activate" handler="Debug_stopTimer_activate" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkPaned" id="MainPaned">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="orientation">vertical</property>
            <property name="position">900</property>
            <property name="position_set">True</property>
            <child>
              <object class="GtkEventBox" id="GtkDrawingArea_EventBox">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <signal name="button-press-event" handler="button_press_event_cb" swapped="no"/>
                <signal name="motion-notify-event" handler="motion_notify_event_cb" swapped="no"/>
                <child>
                  <object class="GtkDrawingArea" id="MainDrawArea">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <signal name="configure-event" handler="configure_event_cb" swapped="no"/>
                    <signal name="draw" handler="draw_cb" swapped="no"/>
                  </object>
                </child>
              </object>
              <packing>
                <property name="resize">True</property>
                <property name="shrink">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkBox" id="ControlBox">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="spacing">5</property>
                <property name="homogeneous">True</property>
                <child>
                  <object class="GtkMenuButton" id="StandartGatesChooseButton">
                    <property name="label" translatable="yes">Standarta vārti</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <property name="direction">up</property>
                    <property name="popover">StandartGatesChooseButton_popover1</property>
                    <child>
                      <placeholder/>
                    </child>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkButton" id="TestButton">
                    <property name="label" translatable="yes">button</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <signal name="clicked" handler="TestButton_clicked" swapped="no"/>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
              <packing>
                <property name="resize">False</property>
                <property name="shrink">False</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkImage">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="stock">gtk-missing-image</property>
  </object>
</interface>
...