如何将输出显示为文本而不是数字?

How to show Output as a text instead of number?

我使用此代码在按钮中打印数字 0 到 11:

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);

我在 opencv 中使用 applyColorMap(unsharp, dstt, type_color) 并且我有 12 种颜色。这些类型的颜色显示为数字(0 到 11)。我想在我的按钮中将输出显示为 "text" 而不是 "number"。我可以使用 "color" 按钮更改类型颜色。

我只想更改,例如,“0”更改为 "AUTUMN",“1”更改为 "BONE",....如果您使用显示“0”的 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), g_strdup_printf("AUTUMN", 0)); " 到 "AUTUMN",但我希望所有这些都出现 ,只有一个 gtk_combo_box_text_append_text

我想在我的按钮中将输出显示为 "text" 而不是 "number"。

"AUTUMN" instead of "0"

"BONE"   instead of "1"

"JET"    instead of "2"

.

.

.

.

"PARULA" instead of "11"

对于如何解决这个任务,您有什么建议?或者我可以在互联网上的哪些资源上找到帮助?

这是我的 C++ 代码的一部分:

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");


 .
 .
 .
 .

 }

例如,我将循环编辑为:

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]));

但它不能正常工作。

已更新如何解码组合框中的值

您不需要 g_strdup_printfgtk_combo_box_text_append_text 需要一个 const gchar *。这意味着它不会修改您传递的字符串,它会自己创建一个副本。

在文件的顶部声明:

static const char *color_names[] = { "AUTUMN", "BONE", "JET" };

然后,在您要填充组合框的地方,替换:

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]));

与:

for (int i = 0; i < G_N_ELEMENTS(texts); i++)
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(colorize_combo), texts[i]);

最后,在你阅读组合框选择的文本的地方,替换:

int type_color = atoi(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(colorize_combo)));

与:

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);