Original issue is gtk4: missbehavior of the Gtk::Label tooltip (#6674) · Issues · GNOME / gtk · GitLab
The problem here is: When application have a lot of labels there are separate threads which can change tooltips for any label. In UI for those unchanged Labels - tooltips are flickering or even disappeared
Code in gtk3
#include <thread>
#include <iomanip>
void setTlpMarkup(GtkWidget *wg, char* markUp) {
if (wg) {
while(true) {
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
auto str = std::string(markUp) + ' ' + oss.str();
gtk_widget_set_tooltip_markup(wg, str.c_str());
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
}
static GtkWidget *lLabel, *rLabel;
std::thread lT, rT;
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *mainBox, *lBox, *rBox;
mainBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
lBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
rBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_start(GTK_BOX(mainBox), lBox, true, true, 0);
gtk_box_pack_end(GTK_BOX(mainBox), rBox, true, true, 0);
// Labels
lLabel = gtk_label_new("Left Label");
rLabel = gtk_label_new("Right Label");
gtk_box_pack_start(GTK_BOX(lBox), lLabel, true, true, 0);
gtk_box_pack_start(GTK_BOX(rBox), rLabel, true, true, 0);
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_container_add (GTK_CONTAINER (window), mainBox);
gtk_window_present (GTK_WINDOW (window));
gtk_widget_set_tooltip_markup(lLabel, (char*)"Left tooltip");
// lT = std::thread(setTlpMarkup, lLabel, (char*)"Left tooltip");
rT = std::thread(setTlpMarkup, rLabel, (char*)"Right tooltip");
gtk_widget_show_all (window);
}
int main(int argc, char* argv[]) {
GtkApplication *app;
int status{0};
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Code in gtk4
#include <gtk/gtk.h>
#include <thread>
#include <iomanip>
void setTlpMarkup(GtkWidget *wg, char* markUp) {
if (wg) {
while(true) {
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
auto str = std::string(markUp) + ' ' + oss.str();
gtk_widget_set_tooltip_markup(wg, str.c_str());
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
}
static GtkWidget *lLabel, *rLabel;
std::thread lT, rT;
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *mainBox, *lBox, *rBox;
mainBox = gtk_center_box_new();
lBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
rBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_center_box_set_start_widget(GTK_CENTER_BOX(mainBox), lBox);
gtk_center_box_set_end_widget(GTK_CENTER_BOX(mainBox), rBox);
// Labels
lLabel = gtk_label_new("Left Label");
rLabel = gtk_label_new("Right Label");
gtk_box_append(GTK_BOX(lBox), lLabel);
gtk_box_append(GTK_BOX(rBox), rLabel);
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_set_child (GTK_WINDOW (window), mainBox);
gtk_window_present (GTK_WINDOW (window));
gtk_widget_set_tooltip_markup(lLabel, (char*)"Left tooltip");
// lT = std::thread(setTlpMarkup, lLabel, (char*)"Left tooltip");
rT = std::thread(setTlpMarkup, rLabel, (char*)"Right tooltip");
}
int main(int argc, char* argv[]) {
GtkApplication *app;
int status{0};
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}