GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 10:13 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: gtkmm+glade problem
PostPosted: Tue Nov 29, 2011 6:44 pm 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
Hello and thx in advance!

I'm new programming in GUI's and kind of modderate in c++ . I've been working in a application in opencv/c++ and now i want to link it with a very simple GUI.

At first, i create a GUI with glade following an example i got, and it works. Later i pack my opencv code in some .cpp classes and rewrote the Makefile including the new stuff and a link with pkg-config in wrote some code to use it. Then the problem occurs, building the code with the new Makefile seems to mess with glade somehow and have a Segmentation fault in the first loading of a glade :
Code:
#include <libglademm.h>
#include <gtkmm/main.h>
#include <libglademm/xml.h>
#include <gtkmm.h>

#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include "Evaluador.h"

#include "BaseWindow.h"
#include "Tab1.h"
#include <iostream>


int main (int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);
  Glib::RefPtr<Gnome::Glade::Xml> refXml = Gnome::Glade::Xml::create("MainWindow2.glade");

  BaseWindow *baseWindow;
  refXml->get_widget_derived("window1", baseWindow);
  Gtk::Notebook *notebook;
  refXml->get_widget("notebook1", notebook);


  Glib::RefPtr<Gnome::Glade::Xml> refXml_tab1 = Gnome::Glade::Xml::create("tab1.glade");
  Tab1 *widget1;
  refXml_tab1->get_widget_derived("hbox1", widget1);
  Glib::ustring label1("Ejecutar");
  notebook->append_page(*widget1, label1);

  kit.run(*baseWindow);

  return 0;
}

If i remove all the references to the opencv code and build with the new Makefile, still got the segmentation fault, but if a build it with the old Makefile the GUI works all fine

First Makefile:
Code:
all: example

example: cish.cc MainWindow2.glade BaseWindow.o Tab1.o TabHBox.o
   g++ -o cish cish.cc BaseWindow.o Tab1.o TabHBox.o `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

BaseWindow.o : BaseWindow.cc BaseWindow.h
   g++ -c BaseWindow.cc `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

Tab1.o : Tab1.cc Tab1.h TabHBox.h
   g++ -c Tab1.cc `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

TabHBox.o : TabHBox.cc TabHBox.h
   g++ -c TabHBox.cc `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

clean:
   rm -f cish *.o


Opencv included makefile
Code:
all: build

build: cish.cc MainWindow2.glade BaseWindow.o Tab1.o TabHBox.o Util.o ExtractorPuntos.o DecisorPuntos.o Segmentador.o Evaluador.o
   g++ -o cish cish.cc BaseWindow.o Tab1.o TabHBox.o Util.o ExtractorPuntos.o DecisorPuntos.o Segmentador.o Evaluador.o `pkg-config gtkmm-2.4 libglademm-2.4 opencv --cflags --libs`

Util.o: Util.cpp Util.h
   g++ -c Util.cpp `pkg-config opencv --cflags --libs`

ExtractorPuntos.o: ExtractorPuntos.cpp ExtractorPuntos.h
   g++ -c ExtractorPuntos.cpp `pkg-config opencv --cflags --libs`

DecisorPuntos.o: DecisorPuntos.cpp DecisorPuntos.h Util.h
   g++ -c DecisorPuntos.cpp `pkg-config opencv --cflags --libs`

Segmentador.o: Segmentador.cpp Segmentador.h Util.h
   g++ -c Segmentador.cpp `pkg-config opencv --cflags --libs`

Evaluador.o: Evaluador.cpp Evaluador.h Util.h ExtractorPuntos.h DecisorPuntos.h Segmentador.h
   g++ -c Evaluador.cpp `pkg-config opencv --cflags --libs`


BaseWindow.o : BaseWindow.cc BaseWindow.h
   g++ -c BaseWindow.cc `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

Tab1.o : Tab1.cc Tab1.h TabHBox.h
   g++ -c Tab1.cc `pkg-config gtkmm-2.4 libglademm-2.4 opencv --cflags --libs `

TabHBox.o : TabHBox.cc TabHBox.h
   g++ -c TabHBox.cc `pkg-config gtkmm-2.4 libglademm-2.4 --cflags --libs`

clean:
   rm -f cish *.o


I'm pretty clueless and need some help because im running out of time. I can show the code you guys need but i dont want to flood. I would apreciate all the help


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Wed Nov 30, 2011 7:43 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 768
Location: UK
Hi,

I am not sure it it is the order you are linking the libraries or not, so I have rewritten your make file to do this and to make it clearer to read, by using variables.

See the tutorial at http://users.actcom.co.il/~choo/lupg/tutorials/writing-makefiles/writing-makefiles.html

Code:
LD = g++
LIBS = `pkg-config gtkmm-2.4 libglademm-2.4 opencv --libs`
CPPFLAGS = `pkgconfig gtkmm-2.4 libglademm-2.4 opencv --cflags`

OBJS = cish.o BaseWindow.o Tab1.o TabHBox.o Util.o ExtractorPuntos.o DecisorPuntos.o Segmentador.o Evaluador.o

all: build

build: $(OBJS)
   $(LD) $(LIBS) $(OBJS) -o cish

cish.o: cish.cc
   g++ $(CPPFLAGS) -c cish.o

Util.o: Util.cpp Util.h
   g++ $(CPPFLAGS) -c Util.cpp

ExtractorPuntos.o: ExtractorPuntos.cpp ExtractorPuntos.h
   g++ $(CPPFLAGS) -c ExtractorPuntos.cpp

DecisorPuntos.o: DecisorPuntos.cpp DecisorPuntos.h Util.h
   g++ $(CPPFLAGS) -c DecisorPuntos.cpp

Segmentador.o: Segmentador.cpp Segmentador.h Util.h
   g++ $(CPPFLAGS) -c Segmentador.cpp

Evaluador.o: Evaluador.cpp Evaluador.h Util.h ExtractorPuntos.h DecisorPuntos.h Segmentador.h
   g++ $(CPPFLAGS) -c Evaluador.cpp

BaseWindow.o : BaseWindow.cc BaseWindow.h
   g++ $(CPPFLAGS) -c BaseWindow.cc

Tab1.o : Tab1.cc Tab1.h TabHBox.h
   g++ $(CPPFLAGS) -c Tab1.cc

TabHBox.o : TabHBox.cc TabHBox.h
   g++ $(CPPFLAGS) -c TabHBox.cc

clean:
   rm -f cish $(OBJS)


By the way what version of GTKMM are you using, as libglade has been deprecated.

E.

_________________
E.


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Wed Nov 30, 2011 9:33 am 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
Thx errol! I Did a little fix to the Makefile you gave to me and the code compile. I try moving the opencv lib and cppflag in all the combinations possible but the segmentation fault continue :(

I'm pretty desperate, i would appreciate any lead/hint/clue

Thx in advance


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Wed Nov 30, 2011 5:34 pm 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
I was tired of looking for the problem and decide rewrite the whole thing with Gtk::Builder. Now it works!

But i got some little noob problems trying to get some arguments for my opencv class:

Miwindow.h
Code:
#ifndef MIWINDOW_H
#define MIWINDOW_H

#include <libglademm.h>
#include <gtkmm.h>

class Miwindow : public Gtk::Window {

public:


   Miwindow(BaseObjectType* cobject, Glib::RefPtr<Gtk::Builder> refBuilder);
   virtual ~Miwindow();
   
protected:

  // Xml pointer
      Glib::RefPtr<Gtk::Builder> m_refBuilder;
   
   // Widgets
   Gtk::ImageMenuItem *imagemenuitem2;
   Gtk::ImageMenuItem *imagemenuitem5;
   Gtk::ImageMenuItem *imagemenuitem6;
   Gtk::SpinButton *spinbutton1;
   Gtk::Button *button1;   

   // Signal handlers
   void on_button1_clicked();
   void on_imagemenuitem2_activate();
   void on_imagemenuitem5_activate();
   void on_imagemenuitem6_activate();


};

#endif


Miwindow.cc
Code:
//#include "cv.h"
//#include "highgui.h"
//#include "ml.h"
#include "Miwindow.h"
#include "Evaluador.h"

#include <iostream>


Miwindow::Miwindow(BaseObjectType* cobject,    Glib::RefPtr<Gtk::Builder> refBuilder):
  Gtk::Window(cobject),
  m_refBuilder(refBuilder) {

  m_refBuilder->get_widget("imagemenuitem5", imagemenuitem5);
 
  if (imagemenuitem5) {
    imagemenuitem5->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem5_activate) );
  }
 
  m_refBuilder->get_widget("imagemenuitem6", imagemenuitem6);
  if (imagemenuitem6) {
    imagemenuitem6->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem6_activate) );
  }
 
  m_refBuilder->get_widget("imagemenuitem2", imagemenuitem2);
 

 
  if (imagemenuitem2) {
   imagemenuitem2->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem2_activate) );     
  }
 
  m_refBuilder->get_widget("button1", button1);
  m_refBuilder->get_widget("spinbutton1", spinbutton1);
 
  if (button1 && spinbutton1){
    button1->signal_clicked().connect(sigc::mem_fun(*this, &Miwindow::on_button1_clicked) );
  }
 
}


Miwindow::~Miwindow() {
  delete imagemenuitem2;
  delete imagemenuitem5;
  delete imagemenuitem6;
  delete button1;
  delete spinbutton1;
}

void Miwindow::on_button1_clicked() { //run the opencv  method

   ***** int spinvalue = spinbutton1->get_value();
   ***** std::cout << "Parameter 1 = " << spinvalue << std::endl;[/color]

//    Evaluador evaluador;
//    eval resultado;
++++//    resultado = evaluador.evaluar(spinvalue,"/PathToOneImage");[/color]
//    std::cout << "ratio = " << resultado.ratio << std::endl;

}


void Miwindow::on_imagemenuitem2_activate() { //open and show the image to the opencv method
   Gtk::Window* visor_win = new Gtk::Window(Gtk::WINDOW_TOPLEVEL);
   visor_win->set_title ("visor de imágenes");

   Gtk::HBox* box = new Gtk::HBox();
   box->set_spacing(5);


   Gtk::Image* image = new Gtk::Image();

   box->pack_start (*image, true, true);

   visor_win->add(*box);

   Gtk::FileChooserDialog dialog("Selecciona imagen a procesar",
                                 Gtk::FILE_CHOOSER_ACTION_OPEN);
   dialog.add_button (Gtk::Stock::OPEN,
                      Gtk::RESPONSE_ACCEPT);
   dialog.add_button (Gtk::Stock::CANCEL,
                      Gtk::RESPONSE_CANCEL);

   switch (dialog.run())
   {
      case Gtk::RESPONSE_ACCEPT:
    {
      Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create_from_file(dialog.get_filename());
      pb = pb->scale_simple(800, 600,Gdk::INTERP_BILINEAR);
      image->set(pb);
       visor_win->show_all();


%%%%% std::cout << "pathImagen = " << dialog.get_filename() << std::endl;
//      std::cout << "w x h: " <<     pb->get_width() << " x " << pb->get_height() << std::endl;

       break;
    }
      default:
         break;
   }
   dialog.hide();
}

void Miwindow::on_imagemenuitem5_activate() { // Close item
  exit(1);
}

void Miwindow::on_imagemenuitem6_activate() { //about item
  Gtk::AboutDialog *aboutDialog = new Gtk::AboutDialog();
 
  aboutDialog->set_license(Glib::ustring("LGPL"));
//more aboutDialog stuff
}


I dont know why (*****) the spinbutton keep returning 0 no matter what you put in it. And i need (++++) opencv method a const char* or char* (%%%%)of the path about the opened image.

Thx in advance


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Wed Nov 30, 2011 7:26 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 768
Location: UK
Lets start with your header. You do not need to include <libglademm.h> as we are now using Gtk::Builder

I have rewritten your code and done a bit of reformatting to try and make it clearer to read. Note I have left the memory management bugs in (memory leaks), as this could be looked at by you in your own way or as another task later.

Code:
//#include "cv.h"
//#include "highgui.h"
//#include "ml.h"
#include "Miwindow.h"
#include "Evaluador.h"

#include <iostream>


Miwindow::Miwindow(BaseObjectType * cobject, Glib::RefPtr<Gtk::Builder> refBuilder)
    : Gtk::Window(cobject), m_refBuilder(refBuilder)
{
// Get pointers to our widgets
  m_refBuilder->get_widget("imagemenuitem2", imagemenuitem2);
  m_refBuilder->get_widget("imagemenuitem5", imagemenuitem5);
  m_refBuilder->get_widget("imagemenuitem6", imagemenuitem6);
  m_refBuilder->get_widget("button1", button1);
  m_refBuilder->get_widget("spinbutton1", spinbutton1);

// Connect signals
  if (imagemenuitem5) {
    imagemenuitem5->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem5_activate) );
  }

  if (imagemenuitem6) {
    imagemenuitem6->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem6_activate) );
  }
 
  if (imagemenuitem2) {
    imagemenuitem2->signal_activate().connect(sigc::mem_fun(*this, &Miwindow::on_imagemenuitem2_activate) );     
  }
 
  if (button1 && spinbutton1) {
    button1->signal_clicked().connect(sigc::mem_fun(*this, &Miwindow::on_button1_clicked) );
  }
}


Miwindow::~Miwindow()
{
}

void Miwindow::on_button1_clicked()
{ //run the opencv  method

   int spinvalue = spinbutton1->get_value_as_int();
   std::cout << "Parameter 1 = " << spinvalue << std::endl;

//    Evaluador evaluador;
//    eval resultado;
//    resultado = evaluador.evaluar(spinvalue,"/PathToOneImage");[/color]
//    std::cout << "ratio = " << resultado.ratio << std::endl;

}


void Miwindow::on_imagemenuitem2_activate()
{ //open and show the image to the opencv method
   Gtk::Window* visor_win = new Gtk::Window(Gtk::WINDOW_TOPLEVEL);
   visor_win->set_title("visor de imágenes");

   Gtk::HBox* box = new Gtk::HBox();
   box->set_spacing(5);


   Gtk::Image* image = new Gtk::Image();

   box->pack_start (*image, true, true);

   visor_win->add(*box);

   Gtk::FileChooserDialog dialog("Selecciona imagen a procesar",
                                 Gtk::FILE_CHOOSER_ACTION_OPEN);
   dialog.add_button (Gtk::Stock::OPEN,
                      Gtk::RESPONSE_ACCEPT);
   dialog.add_button (Gtk::Stock::CANCEL,
                      Gtk::RESPONSE_CANCEL);

   switch (dialog.run())
   {
      case Gtk::RESPONSE_ACCEPT:
      {
        Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create_from_file(dialog.get_filename());
        pb = pb->scale_simple(800, 600, Gdk::INTERP_BILINEAR);
        image->set(pb);
        visor_win->show_all();

        std::cout << "pathImagen = " << dialog.get_filename() << std::endl;
        std::cout << "pathImagen (as a C string) = " << dialog.get_filename().c_str() << std::endl;

//      std::cout << "w x h: " <<     pb->get_width() << " x " << pb->get_height() << std::endl;

        break;
      }
      default:
        break;
   }
}

void Miwindow::on_imagemenuitem5_activate()
{ // Close item
  Gtk::Main::Quit();
}

void Miwindow::on_imagemenuitem6_activate()
{ //about item
  Gtk::AboutDialog *aboutDialog = new Gtk::AboutDialog();

  aboutDialog->set_license(Glib::ustring("LGPL"));
//more aboutDialog stuff
}


- In the destructor ~Miwindow() there is no need to delete the widgets as they are already managed in the top level window, and this would cause a double delete. See the documentation for GtkBuilder.

- Should not call exit() in a GUI application, as this is not always a clean way to quit. You may want other processing to be done before the application finally ends.

- There is no need to hide the FileChoosser dialog as it will be destroyed once out of scope.

- Changed the way the value is obtained for the Gtk::SpingButton. But will need to know more details on how the SpingButton is defined in the Glade file to fully give an answer.

- To obtain a C string from a std::string or Glib::ustring use c_str() on the object to return the C string.

_________________
E.


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Wed Nov 30, 2011 8:39 pm 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
Thx errol! i almost got it, but i am soooo tired. Tomorrow i ill end it ;)


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Thu Dec 01, 2011 10:35 am 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
I solve the first problem (the spinbutton one) setting the values in code as follow:
Code:
  m_refBuilder->get_widget("spinbutton1", spinbutton1);
 
  double f = 50;
  double t = 1000;
  spinbutton1->set_range(f,t);
  spinbutton1->set_increments(f,f);

  if (button1 && spinbutton1){
    button1->signal_clicked().connect(sigc::mem_fun(*this, &Miwindow::on_button1_clicked));
  }
 
}


For the second problem ... the dialog.get_filename().c_str() return a const char *, I need a char * in my on_button1_clicked(). I tried some possibilities, all envolving creating a new class variable.

I tried for this variable a char * path, char path[] ... and tried to convert the const char * in that, but for most i got a memory crash and for others i was not obtaining the value in on_button1_clicked().

This last one i don't know what is wrong with it:

Miwindow.h
Code:
#ifndef MIWINDOW_H
#define MIWINDOW_H

#include <gtkmm.h>

class Miwindow : public Gtk::Window {

public:


   Miwindow(BaseObjectType* cobject, Glib::RefPtr<Gtk::Builder> refBuilder);
   virtual ~Miwindow();
   
protected:

        //same stuff

   char * path;

        //same stuff

};

#endif


Miwindow.cc
Code:
//same includes

Miwindow::Miwindow(BaseObjectType * cobject, Glib::RefPtr<Gtk::Builder> refBuilder)
    : Gtk::Window(cobject), m_refBuilder(refBuilder)
{
//same stuff


Miwindow::~Miwindow() {
}

void Miwindow::on_button1_clicked() {

      int spinvalue = spinbutton1->get_value_as_int();
    std::cout << "Parameter 1 = " << spinvalue << std::endl;
      std::cout << path << std::endl;
}


void Miwindow::on_imagemenuitem2_activate()
{
  //same stuff

  path = const_cast<char*> ( dialog.get_filename().c_str() );
      std::cout << "pathImagen = " << path << std::endl;

  //same stuff
}
void Miwindow::on_imagemenuitem5_activate() {
//same stuff
}

void Miwindow::on_imagemenuitem6_activate() {
  //same stuff
}


When i run those parts, i got the following:

1 pathImagen = /home/myhome/pathToImage/image.jpg
2 Parameter 1 = 50
3 �6� �

The 1, is the cout just after the const_cast<char*>, the 2 is the spinbutton working and the 3 is some random garbage :S

Anyone know what i'm doing wrong or know the way to do what i want? any lead/hint/help will be appreciated.

Thx in advance for all!


Top
 Profile  
 
 Post subject: Re: gtkmm+glade problem
PostPosted: Thu Dec 01, 2011 3:59 pm 
Offline
Familiar Face

Joined: Tue Nov 29, 2011 6:19 pm
Posts: 9
I answer it myself

the problem was that the memory that the pointer was pointing is free when the function ends, so i use a std::string as the variable in the header and then use path = std::string(dialog.get_filename().c_str()) and path.c_str() to access where i want
Solved ;)

Thx to all the leads and hints!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group