When trying to compile with Code::Blocks the example that comes up with GTK+:
#include stdlib.h
#include gtk/gtk.h
static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "Hello World");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);
button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
I receive the following errors:
ld.exe||cannot find -lgobject-2.0|
ld.exe||cannot find -lglib-2.0|
||=== Build finished: 2 errors, 0 warnings ===|
I have linked both to the project, but I can't find a way to make it work. I have tried both the bundled and separated packages from http://www.gtk.org/download-windows.html. I'm pretty sure it must be something simple/stupid but I couldn't find anything that could help me to solve this problem.
Have you added the GTK Directory containing the libglib-2.0.a, libgobject-2.0.a files to the Linker Search Directories.
This can be done from the following Path:
Settings | Compiler and debugger... | Search directories | Linker | Add.
The full command line for the compiler/linker can be viewed in the Build Log window. To enable this go to:
Settings | Compiler and debugger... | Other Settings | Compiler logging | Full command line
I didn't come across the missing entrance point issue so maybe your copy of GTK is corrupted. Download the All-in-one bundle from GTK+ 2.22
make sure you add all the Include Directories in Search directories | Compiler
As a side note you may need to compile your project using -mms-bitfields which can be added in:
Settings | Compiler and debugger... | Other options just paste the flag as is.
When code::blocks is running ld.exe instead of calling it with, for example: -l glib-2.0 it is calling it with -lglib-2.0
Adding a space before the lib's name in code::block's linker options should fix it.
Related
I just started to learn programming in C++ using eclipse cdt and gtk in Ubuntu.
The packages I was able to install and configure are:
Install gtk3.0
install pkg-config from the market place
Added ${PKCFLAGS} in Cross G++ Compiler
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
Added ${PKCFLAGS} in Cross G++ Linker
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
I tried to run a sample program:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Window :-)");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
But I am still getting an error "Unresolved inclusion: "
Run in a terminal
pkg-config --cflags gtk+-3.0
and put the output in the "compiler flags" field somewhere in eclipse's settings.
Run
pkg-config --libs gtk+-3.0
and put the output from this command in the "linker flags" field. Don't use cross compiler settings unless you are really cross compiling. This should compile as long as you code is right.
There were two problems,
I needed to add also gtk+2.0 in C/C++ Build => settings
I did not noticed but there are several issues with the code I attached it's
with some strange symbols, I changed it to this:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello world");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
After a couple of rounds of troubleshooting I was able to compile GTK source code in Visual Studio with no errors. I followed a procedure not too dissimilar to 'How to configure gtk on Visual studio 2010'. The code is as follows,
#include <gtk-2.0\gtk\gtk.h>
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main(int argc, char* argv[])
{
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_init(&argc, &argv);
gtk_widget_set_usize(window, 300, 200);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(window), "GTK+ with VS2010");
gtk_widget_show(window);
gtk_main();
return 0;
}
However on starting the code no window appears. Visual Studio simply indicates the solution is running but no window appears. Any ideas?
There's problem here:
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_init(&argc, &argv);
You should call gtk_init before creating any windows:
Call this function before using any other GTK+ functions in your GUI
applications. It will initialize everything needed to operate the
toolkit and parses some standard command line options.
Try:
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
I've coded a very simple GUI with GTK3, with three buttons:
Start: calls OpenCV VideoCapture
Stop: stops the VideoCapture
Quit: destroys the window
The problem arises when I hit "Stop", the OpenCV process blocks the GUI and the command is executed after many seconds (typically, a minute).
this is my main:
bool stop = false;
static void start_webcam (GtkWidget *widget, gpointer data)
{
cout<<"Webcam On"<<endl;
int err = 0;
**err = opencv_webcam(); // this function loads the OpenCV Webcam**
// troubles?
if (err != 0)
{
cout<<"Unable to load the webcam. Error Code # "<<err<<endl;
}
}
static void stop_webcam (GtkWidget *widget, gpointer data)
{
stop = true;
cout<<"Webcam Off"<<endl;
}
int main (int argc, char *argv[])
{
// Widgets
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
// GTK Init
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "WEBCAM CONTROL PANEL");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
/// START WEBCAM
button = gtk_button_new_with_label ("Start");
g_signal_connect (button, "clicked", G_CALLBACK (start_webcam), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
/// STOP WEBCAM
button = gtk_button_new_with_label ("Stop");
g_signal_connect (button, "clicked", G_CALLBACK (stop_webcam), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
/// QUIT
button = gtk_button_new_with_label ("Quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
/// GO!
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
I would like to set some high priority to the Quit button. I tried by setting the highest priority in my main by SetPriorityClass() in Windows, but I was unsuccessful.
Thank you very much!
EDIT
this is my openCV code:
extern bool stop;
int opencv_webcam()
{
/// Various stuff
cv::Mat frame;
cv::namedWindow("Webcam Session");
/// Webcam Opening
cv::VideoCapture clip(0);
while(stop != true)
{
// frames extraction
clip.grab();
clip.retrieve(frame);
// troubles?
if ((frame.empty()==1))
{
// errore!
return -1;
}
/// SHOW the frames
cv::imshow("Webcam Session", frame);
if (waitKey(30)>= 0)
{
break;
}
}
return 0;
}
GTK+ is NOT multithreaded. When one of your signal handlers is running, all other GUI processing stops. Your opencv_webcam() function is running on the GUI thread, and runs its main loop while processing the Start button signal. The reason why your Stop and Quit buttons aren't working is because they never get a chance to run (at least not until opencv_webcam() errors out).
I don't know what the appropriate solution is; I don't know enough about OpenCV to suggest a solution that lets you have stable timing while playing nice with GTK+. I know of two possibilities, though:
Doing all OpenCV processing in a gdk_threads_add_idle() callback (stable timing will be harder)
Running OpenCV on a different thread (you will need to communicate frame images across threads if you want to show them in your window)
You will need to search around and figure out what the best solution is. Try Googling for "gtk opencv" and seeing what you get.
Alternatively, if you're doing strict webcam work, you might want to look at libcheese, which is a GTK+ complement specifically intended for webcams.
As for button priorities, there's no signal prioritization that I know of. Given the diagnosis of your bug above, you should be able to see why prioritization wouldn't have helped anyway. In fact, I don't know of any GUI toolkit for any platform that has that ability. You should instead fix threading bugs that prevent the GUI from being snappy.
I'm rather new to programming and especially when it comes to how to including libraries and alike activities. I've been programming a bit using Python in the past and I've been using GTK to create windows, something I've intended to do when programming with c++ as well. To get things started, here's my code:
#include <stdlib.h>
#include <gtk/gtk.h>
static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "test");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);
button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
The code was taken from a "Hello world" example I found on the internet a while back.
I'm aware that this issue have already gotten an answer, but my situation is much more complex (from my perspective at least). First off, I've installed all packages required. I run Ubuntu 14.04 by the way.
When I compile the code using g++ main.cpp I get the following error:
main.cpp:2:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
There is a fix for this particular error, which is to extend the compile command like this: g++ main.cpp -I/usr/include/gtk-2.0. This however will provide another, similar error:
In file included from /usr/include/gtk-2.0/gdk/gdk.h:32:0,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from main.cpp:2:
/usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30:21: fatal error: gio/gio.h: No such file or directory
#include <gio/gio.h>
^
compilation terminated.
You can fix this as well by extending the command like this (all commands are fund on the internet and I don't quite understand it all): g++ -g -Wall -c -o program.o main.cpp -I/usr/include/gtk-2.0 $(pkg-config --libs --cflags glib-2.0). There will now be an error with cairo.h.
As you can see there are similar errors. I have no idea what's wrong but I must believe there is a relatively easy fix.
Also, I tried a fresh install of Ubuntu (just installed packages necessary) and the same errors occur.
The pkgconfig command gives you all of the necessary -I (include) and -l (linker) statements for the compiler, when including a certain package.
Take a look what is given by running:
pkgconfig --cflags --libs gtk+-2.0
I've tried compiling your code on my Ubuntu 14.04.1 and it went fine, when I used:
g++ main.cpp `pkg-config --cflags --libs gtk+-2.0`
Probably pkg-config --libs --cflags glib-2.0 wasn't enough to provide all of the necessary include paths and shared libraries.
It's works (Ubuntu):
export CPATH=$CPATH:/usr/include/gtk-2.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/usr/lib/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib/gtk-2.0/include/
export CPATH=$CPATH:/usr/include/atk-1.0/
With Fedora >=25:
export CPATH=$CPATH:/usr/include/gtk-3.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/lib64/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib64/gtk-3.0/3.0.0/
export CPATH=$CPATH:/usr/include/atk-1.0/
export CPATH=$CPATH:/usr/include/cairo/
into ~/.bashrc
or
into ~/.zshrc
Remember to run: source ~/.bashrc
When you don't have a file installed on Ubuntu, the place to go is http://packages.ubuntu.com and use the second search form to look for the filename.
In this case, the answer is (for trusty):
File Package
-----------------------------------------------------------------
/usr/include/gtk-2.0/gtk/gtk.h libgtk2.0-dev
/usr/include/gtk-3.0/gtk/gtk.h libgtk-3-dev
/usr/lib/R/site-library/RGtk2/include/RGtk2/gtk.h r-cran-rgtk2
You need to select which one of those is most suitable, and install it.
I'm guessing this one: libgtk2.0-dev
You can then repeat the process for the gio/gio.h header file.
I also immigranted to C++ from Python and I am just not used with how the C++ include way of working. Here's what I do to make life easier in C++. I make a Makefile with the following contents:
all:
g++-5 main.cpp -o main `pkg-config --libs --cflags gtkmm-3.0`
Instead of main.cpp, include all your *.cpp files and headers. (Also, please notice I use gtkmm-3.0) And in gtkmm-3.0, your Application with one window, one button and the button being linked to a function [which says: Hello World! of course] would look like this
#include <iostream>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
class HelloWorld : public Gtk::Window{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
// implementing the class HelloWorld
HelloWorld::HelloWorld(): m_button("Click me, Xweque"){
set_border_width(20); // Sets the border width of the window.
// Connecting the button's clicked signal to an arbitrary function
m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked));
add(m_button);
m_button.show();
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::on_button_clicked(){
std::cout << "Hello World" << std::endl;
}
int main(int argc, char** argv){
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
HelloWorld mywindow;
app->run(mywindow);
return 0;
}
I'm trying to get the input text from a text box in a callback function when the user changes something it it (on "changed").
The code goes as follow:
#include <stdio.h>
#include <gtk/gtk.h>
void enter_callback( GtkWidget *widget, GtkEditable *buffer)
{
printf("%s",gtk_editable_get_chars(buffer, 0, -1));
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *text;
GtkWidget *table;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
table = gtk_table_new (2, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
text=gtk_text_new(NULL, NULL);
gtk_text_set_editable(text, TRUE);
gtk_signal_connect(GTK_OBJECT(text), "changed", GTK_SIGNAL_FUNC(enter_callback), (GtkEditable*)text);
gtk_table_attach_defaults(GTK_TABLE(table), text, 0, 1, 0, 1);
gtk_container_border_width (GTK_CONTAINER (window), 40);
gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);
gtk_widget_show(text);
gtk_widget_show(window);
gtk_widget_show(table);
gtk_main();
return 0;
}
The code compiles just right, I'm compiling it on Code::Blocks on debug, checking output on the console by printf. The problem is I get <NULL> as a callback everytime I change something on the textbox. How can I get the correct output?
SOLUTION:
As noted by Washu, gtk_text is deprecated and gtk_text_view should be used instead.
According to the GTK documentation, GtkText is deprecated, buggy, and should not be used. You should instead be using the GtkTextView widget via gtk_text_view_new.
You can use GtkEntry widget too. And use gtk_entry_get_text () (which return const gchar * value) that to get text from GtkEntry, for instance.