I use this code for print number 0 to 11 in a button:
colorize_combo = gtk_combo_box_text_new_with_entry();
for (int i = 0; i <= 2; i += 1)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%d", i));
gtk_combo_box_set_active(GTK_COMBO_BOX(colorize_combo), 11);
gtk_table_attach_defaults(GTK_TABLE(table), colorize_combo, 0, 1, 17, 18);
I use applyColorMap(unsharp, dstt, type_color)
in opencv and I have 12 types color. these types color show up as numbers (0 to 11) . I want to show output as a "text" instead of "number" in my button. I can change types color with "color" button.
I just want to change for example, "0" to "AUTUMN", "1" to "BONE" , .... If you use gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("AUTUMN", 0));
that show up "0" to "AUTUMN" but I want all of them show up just with one gtk_combo_box_text_append_text
.
I want to show output as a "text" instead of "number" in my button.
"AUTUMN" instead of "0"
"BONE" instead of "1"
"JET" instead of "2"
.
.
.
.
"PARULA" instead of "11"
What ideas on how to solve this task would you suggest? Or on what resource on the internet can I find help?
this is a part of my c++ code:
void incdec2(GtkWidget *widget, const gchar *mode)
{
else if (!g_strcmp0(mode, "colorized"))
{
if (gtk_image_get_pixbuf(GTK_IMAGE(img4)) == NULL)
return;
int type_color = atoi(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo)));
vector< Vec4i > hierarchy;
int largest_contour_index = 0;
int largest_area = 0;
vector< vector <Point> > contours1;
Mat alpha(src1.size(), CV_8UC1, Scalar(0));
normalize(alpha, alpha, 0, 250, NORM_MINMAX, CV_8UC3);
findContours(thresh, contours1, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
for (int i = 0; i< contours1.size(); i++) // iterate through each contour.
{
double a = contourArea(contours1[i], false); // Find the area of contour
if (a>largest_area){
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
}
}
drawContours(alpha, contours1, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy);
applyColorMap(unsharp, dstt, type_color);
split(dstt, rgb);
Mat rgbe[4] = { rgb[0], rgb[1], rgb[2], alpha };
merge(rgbe, 4, im_color);
imwrite("Colorized Image.png", im_color);
gtk_image_set_from_file(GTK_IMAGE(img4), "Colorized Image.png");
}
}
int main(int argc, char *argv[])
{
.
.
.
.
.
colorize_combo = gtk_combo_box_text_new_with_entry();
for (int i = 0; i <= 11; i += 1)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%d", i));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("AUTUMN", 0));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("BONE", 1));
// gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("JET", 2));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("WINTER", 3));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("RAINBOW", 4));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("OCEAN", 5));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("SUMMER", 6));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("SPRING", 7));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("COOL", 8));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("HSV", 9));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("PINK", 10));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("HOT", 11));
//gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("PARULA", 12));
gtk_combo_box_set_active(GTK_COMBO_BOX(colorize_combo), 11);
gtk_table_attach_defaults(GTK_TABLE(table), colorize_combo, 0, 1, 17, 18);
but13 = bold_img_button("Color", "E:/Works for Gov Project/DOC/GUI/logogui1/colorize243.png");
gdk_color_parse("#50a0ff", &color);
gtk_widget_modify_bg(but13, GTK_STATE_NORMAL, &color);
gtk_table_attach_defaults(GTK_TABLE(table), but13, 1, 2, 17, 18);
g_signal_connect(G_OBJECT(but13), "clicked", G_CALLBACK(incdec2), "colorized");
.
.
.
.
}
For example, I edit my loop as:
string texts[] = { "AUTUMN", "BONE", "JET" };
int size = sizeof(texts) / sizeof(string);
for (int i = 0; i < size; i++)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%s", texts[i]));
but it doesn't work correctly.
UPDATED with how to decode the value in the combo box
You don't need g_strdup_printf
: gtk_combo_box_text_append_text
takes a const gchar *
. This means it won't modify the string you pass, it will create a copy by itself.
At the top of your file, declare:
static const char *color_names[] = { "AUTUMN", "BONE", "JET" };
Then, where you're filling your combo box, replace:
string texts[] = { "AUTUMN", "BONE", "JET" };
int size = sizeof(texts) / sizeof(string);
for (int i = 0; i < size; i++)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("%s", texts[i]));
with:
for (int i = 0; i < G_N_ELEMENTS(texts); i++)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), texts[i]);
Finally, where you read the text selected by the combo box, replace:
int type_color = atoi(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo)));
with:
int type_color = -1;
gchar * selected_color_name = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo));
for (int i = 0; i < G_N_ELEMENTS(color_names); i++)
if (g_strcmp0(selected_color_name, color_names[i]) == 0)
type_color = i;
// gtk_combo_box_text_get_active_text return a string that must be freed as stated in the doc
g_free(selected_color_name);