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

时间:2016-06-16 17:57:35

标签: gtk gtk3 gtkmm gtk2

我在按钮中使用此代码作为打印编号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)。我想在我的按钮中将输出显示为“文本”而不​​是“数字”。我可以用“颜色”按钮更改类型颜色。

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

我想在我的按钮中将输出显示为“文本”而不​​是“数字”。

"AUTUMN" instead of "0"

"BONE"   instead of "1"

"JET"    instead of "2"

.

.

.

.

"PARULA" instead of "11"

enter image description here

您建议如何解决此任务的想法?或者我可以在互联网上的哪些资源上找到帮助?

这是我的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]));

但它无法正常工作。

1 个答案:

答案 0 :(得分:1)

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

您不需要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);