this code show me on xubuntu 21.04 form with size (400px width + 400px height) and with ONE button.
How can I add to this code two buttons?
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data) {
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_present(GTK_WINDOW(window));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
// Create a new button
GtkWidget *button = gtk_button_new_with_label ("press 123");
gtk_window_set_child (GTK_WINDOW (window), button);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_close), window);
}
int main(int argc, char *argv[]) {
g_autoptr(GtkApplication) app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
In GTK4, I have found that a window (GtkWindow or GtkApplicationWindow) can only have one child. So to include multiple widgets within a window (such as a label and three buttons) one usually has to first create a grid object (GtkGrid), place the widgets within the grid at specified rows and columns, and then set the grid as the child of the window. Using your sample code above, I revised the code to look like the following:
#include <gtk/gtk.h>
static void activate(GtkApplication *app, void *user_data)
{
GtkWidget *window = gtk_application_window_new(app);
GtkWidget *grid = gtk_grid_new();
GtkWidget *label = gtk_label_new("Hello World");
//gtk_window_set_child(GTK_WINDOW(window), gtk_label_new("Hello World!"));
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
gtk_grid_set_column_spacing(GTK_GRID(grid),10);
gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
// Create a new button
GtkWidget *button1 = gtk_button_new_with_label ("Press 1");
GtkWidget *button2 = gtk_button_new_with_label ("Press 2");
GtkWidget *button3 = gtk_button_new_with_label ("Press 3");
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 3, 1);
gtk_grid_attach(GTK_GRID(grid), button1, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button2, 1, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), button3, 2, 1, 1, 1);
gtk_window_set_child (GTK_WINDOW (window), grid);
// When the button is clicked, close the window passed as an argument
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_window_close),
window);
g_signal_connect_swapped (button3, "clicked", G_CALLBACK (gtk_window_close),
window);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char *argv[])
{
g_autoptr(GtkApplication) app = gtk_application_new(NULL,
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
This results in establishing a window with the two additional buttons you wanted. I did not know what type of functions those buttons should trigger, so for my revisions to your sample code, I just hooked the two additional buttons to the same closure signal. Following is a sample of the window when I run the program.
I hope that helps you out.
Regards.
Additional Notes:
Regarding the request as how to add "label2" and then have that label's text updated to "Network Connections", the following additions to your sample program could provide a way to do this.
First, a callback function for updating the new label's text would be added to the program (usually at the beginning of the program).
void on_button1_clicked (GtkLabel *lbl)
{
gtk_label_set_text(lbl, "Network Connections");
}
Then, within the activation function, the new label would be defined.
GtkWidget *label2 = gtk_label_new("");
Next, the new label widget would be added to the grid (in this example, it was added next to the "label" widget in the first row).
gtk_grid_attach(GTK_GRID(grid), label2, 1, 0, 2, 1);
Finally, since the request in the comment was to have the label's text updated to "Network Connections", the signal connection for the first button would be revised to call the new "on_button1_clicked" callback function and passing the "label2" widget instead of the "window" widget.
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (on_button1_clicked), label2);
The result should net the desired behavior.
Hopefully, that addresses your comment.
Regards.
Related
int main(int argc, char **argv)
{
GtkWidget *pWindow;
GtkWidget *pVBox;
GtkWidget *pEntry;
GtkWidget *pButton;
GtkWidget *pLabel;
GtkWidget *text_view;
GtkWidget *scrollbar;
gtk_init(&argc, &argv);
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pWindow), "My IRC");
gtk_window_set_default_size(GTK_WINDOW(pWindow), 800, 600);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
pVBox = gtk_vbox_new(TRUE, 0);
pEntry = gtk_entry_new();
pLabel = gtk_label_new(NULL);
text_view = gtk_text_new(NULL, NULL);
scrollbar = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
gtk_box_pack_start(GTK_BOX(pVBox), scrollbar, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(pVBox), pEntry, TRUE, FALSE, 0);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), text_view);
g_signal_connect(G_OBJECT(pEntry), "activate", G_CALLBACK(on_activate_entry), (GtkWidget*) text_view);
gtk_widget_show_all(pWindow);
gtk_main();
return EXIT_SUCCESS;
}
I want to make the text_view box bigger than the other one. I couldn't find any solutions in the GTK documentation.
PS: It's GTK 2.0.
The two boolean arguments of gtk_box_pack_start() are expand and fill. When expand is true, the widget gets extra space after allocating other widgets. When fill is true, the widget is resized to fill that space. So what you want to do instead is
// expand AND fill - fills all available space
gtk_box_pack_start(GTK_BOX(pVBox), scrollbar, TRUE, TRUE, 0);
// NO expand AND NO fill - only uses what it needs
gtk_box_pack_start(GTK_BOX(pVBox), pEntry, FALSE, FALSE, 0);
Here's a page with more information. Note that you can use the [hv](expand|align) properties with boxes as well as grids (and use gtk_container_add(), which acts like gtk_box_pack_start()).
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 am a newbie at GTK+. I want to create a GUI with an Image with 2 buttons and a label below it.
The image has to be loaded via a file dialog which is opened when I click a button called "Load", but the image is not being shown on the Window. Below is my code:
#include <gtk/gtk.h>
#include <gtkmm.h>
#include <glib.h>
#include <cstring>
#include <cstdlib>
#include "cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
GtkWidget *window, *image;
GtkWidget *vbox, *valign, *hbox, *halign;
GtkWidget *expression_label;
char* get_file()
{
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open File", (GtkWindow *) window, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
char *filename;
filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) );
gtk_widget_destroy(dialog);
return filename;
}
}
void load_file(GtkWidget *widget, gpointer data)
{
g_print("Loading file\n");
char *file = get_file();
Mat img = imread(file);
//imshow("Opencv image", img);
image = gtk_image_new_from_file(file);
gtk_widget_queue_draw(image);
// gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
gtk_label_set_text(GTK_LABEL(expression_label), "Image Loaded");
g_print("File Loaded\n");
g_print("%s\n", file);
}
void get_expression(GtkWidget *widget, gpointer data)
{
gtk_label_set_text(GTK_LABEL(expression_label), "Expression Detected");
}
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "FEAR");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 1100, 1100);
gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
vbox = gtk_vbox_new(FALSE, 5);
hbox = gtk_hbox_new(TRUE, 3);
GtkWidget *load_button = gtk_button_new_with_label("Load");
GtkWidget *run_button = gtk_button_new_with_label("Run");
gtk_widget_set_size_request(load_button, 70, 30);
gtk_widget_set_size_request(run_button, 70, 30);
gtk_container_add(GTK_CONTAINER(hbox), load_button);
gtk_container_add(GTK_CONTAINER(hbox), run_button);
expression_label = gtk_label_new("Expression");
//image = gtk_image_new();
//Display placeholder image
image = gtk_image_new_from_file("../sample.jpg");
gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), expression_label, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
int load_handler_id = g_signal_connect(G_OBJECT(load_button), "clicked", G_CALLBACK(load_file), NULL);
g_signal_connect(G_OBJECT(run_button), "clicked", G_CALLBACK(get_expression), NULL);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I basically created a main window, then added a vbox to it. In the vbox, I have added the image widget, a hbox box that holds by buttons and a label widget to give me relevant information.
When I run the program, it displays the placeholder image properly, but when I select "Load" and choose the new image to be displayed, the label changes and I get the correct output on the command line, but the image widget does not display the new image that was selected.
Can someone please help me and let me know what am I doing wrong?
You're getting a new GtkImage widget after loading, and that widget is never added to a window so (of course) it's not being displayed.
You're just overwriting a global widget pointer with the new widget, but that doesn't magically make the new widget "replace" the old in the widget hierarchy. That you store the pointer to the new widget in the variable that used to hold the old widget doesn't matter from GTK+'s perspective.
Replace your call to gtk_image_new_from_file() with a call to gtk_image_set_from_file() on your existing widget (in image) to replace the content of the existing widget, which is packed and so on, with the image from the file.
Also, you're leaking the filename, you need to add free() after using it.
UPDATE: You mention in a comment that you've made the filename a global, to make it available in more places. If so, you still need to free() the old filename before getting a new one, else your code will be leaking memory.
problem: I have a Textview with different Texttags in different selected text areas. At the end of editing, i would like to render this multi tagged textbuffer/textview into drawingarea.But I am not getting how can I render multi tagged textview using Pango/cairo layout ?. It would be great if I get any Gtk+/Gtkmm code that shows text drawing.
EX: Lets say My text is "AAABBB" and in this AAA has bold tag set and BBB is non bold and italic set..now how can i render this kind of text ?
Assuming you're looking for a c++ example, check if code below would work for you:
#include <gtk/gtk.h>
static gboolean on_expose_event_0(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
GdkScreen *screen = gdk_drawable_get_screen(widget->window);
PangoRenderer *renderer = gdk_pango_renderer_get_default (screen);
GdkGC *gc = gdk_gc_new(widget->window);
gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), widget->window);
gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), gc);
PangoContext *context = gdk_pango_context_get_for_screen (screen);
PangoLayout *layout = pango_layout_new (context);
pango_layout_set_markup(layout, "<b>AAA</b><i>bbb</i>", -1);
PangoFontDescription *font = pango_font_description_from_string("Times 20");
pango_layout_set_font_description(layout, font);
pango_font_description_free(font);
pango_renderer_draw_layout (renderer, layout, 1, 1);
gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), NULL);
gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), NULL);
g_object_unref(layout);
g_object_unref(context);
g_object_unref(gc);
return FALSE;
}
int main( int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);
g_signal_connect(window, "expose-event", G_CALLBACK(on_expose_event_0), NULL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "pango test");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
gtk_widget_set_app_paintable(window, TRUE);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
There is also an example on using pango with cairo here: Using Pango with Cairo
hope this helps, regards
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.