vendredi 28 juin 2019

C++ g_signal_connect realize gtkmm, return warnings

The problem I'm having is with the g_signal_connect, I'm trying to hide the mouse cursor, but I need the window instance to be able to do that (I think). But the prolem here is when I use the function g_signal_connect to do a call back when window realize. I have a main class which implements Window. In case someone knows how to hide the cursor without using this method I would appreciate.

The warning messages:

(main:20441): GLib-GObject-WARNING **: instance of invalid non-instantiatable type '(null)'

(main:20441): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed


The function with g_signal_connect:

g_signal_connect(G_OBJECT(this), "realize", G_CALLBACK(MainWindow::WindowRealize), NULL);

My MainWindow.cc class:

#include "MainWindow.h"


MainWindow::MainWindow() {
  set_title("Canvas exemple");
  fullscreen();

  Gdk::RGBA *gray_bg = new Gdk::RGBA("#333232");
  Gdk::RGBA *light_pink_bg = new Gdk::RGBA("#CEB7B3");
  Gdk::RGBA *black_bg = new Gdk::RGBA("#000000");


  // Here is the problem -------------
  g_signal_connect(G_OBJECT(this), "realize", G_CALLBACK(MainWindow::WindowRealize), NULL);
  // Cria o painel
  this->painel = new Gtk::Box(Gtk::ORIENTATION_VERTICAL);

  // Cria a area de desenho do canvas
  this->canvas = new Canvas();
  this->canvas->set_size_request(500, 500);

  // Meu primeiro conainer onde ficara os botoes
  this->m_Container = new Gtk::Grid();
  this->m_Container->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
  this->m_Container->override_background_color(*gray_bg);

  // Meu segundo container onde ficara o canvas
  this->second_Container = new Gtk::Grid();
  this->second_Container->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
  this->second_Container->override_background_color(*light_pink_bg);

  // Cria o botao e o adiciona no primeiro container
  this->m_New_Line = new Gtk::Button();
  this->m_New_Line->set_label("On/Off");
  this->m_New_Line->signal_pressed().connect( sigc::mem_fun(
    *this, &MainWindow::newPoint));
  this->m_New_Line->signal_released().connect( sigc::mem_fun(
    *this, &MainWindow::cancelButtonFlag));

  this->m_Container->add(*this->m_New_Line);

  // Adiciona o canvas no segundo container
  this->second_Container->add(*this->canvas);

  // Configura tudo no painel
  this->painel->pack_start(*this->m_Container, Gtk::PACK_SHRINK);
  this->painel->pack_start(*this->second_Container, Gtk::PACK_SHRINK);

  // Exibe o painel
  add(*this->painel);

  show_all_children();
}

void MainWindow::newPoint() {
  std::cout << "Botao pressionado" << std::endl;
  this->buttonFlag = 1;

  doMyDraw = new std::thread([this] {
    this->keepDraw();
  });
}

void MainWindow::keepDraw() {
  while (this->buttonFlag == 1) {
    this->canvas->newPoint();
     std::this_thread::sleep_for(std::chrono::milliseconds(250));
  }
}


void MainWindow::cancelButtonFlag() {
  std::cout << "Botao liberado" << std::endl;
  this->buttonFlag = 0;
  if (doMyDraw) {
    if (doMyDraw->joinable()) {
      doMyDraw->join();
    }
  }
}

MainWindow::~MainWindow() { }

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <gtkmm/window.h>
#include <gtkmm/grid.h>
#include <gtkmm/button.h>
#include <gtkmm/box.h>
#include <gdkmm/rgba.h>

#include <iostream>
#include <mutex>
#include <thread>
#include <unistd.h>

#include "Canvas.h"

class MainWindow : public Gtk::Window {
public:
  MainWindow();
  virtual ~MainWindow();

protected: /**Buttons handlers */
  int buttonFlag = 0;
  virtual void newPoint();
  void keepDraw();
  inline void changecursor() {
    // assert(G_WINDOW != NULL);
    // gdk_window_set_cursor(G_WINDOW, G_CURSOR);
    std::cout << "Aqui 2" << std::endl;
  }

  inline static void WindowRealize(GtkWidget *window, gpointer data) {
    // G_CURSOR_HAND = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_HAND2);
    // G_WINDOW = gtk_widget_get_window(window);
    std::cout << "Aqui" << std::endl;
  }
  virtual void cancelButtonFlag();

private:
  Gtk::Grid *m_Container, *second_Container;
  Gtk::Button *m_New_Line, *m_Save;
  Gtk::Box *painel;
  Canvas *canvas;
  std::thread* doMyDraw;
};

#endif

Aucun commentaire:

Enregistrer un commentaire