GTK+/ C++ gtk_label/ gtk_textview 文本大小调整
GTK+/ C++ gtk_label/ gtk_textview text sizing
在左侧的附加图像中 window 我有 Ubuntu 带有文件内容的控制台,在右侧 window 我有相同文件的内容我的 GTK+ 程序。问题是我希望我的 GTK+ 程序准确地显示文件,因为它显示在我的 Ubuntu 控制台上。
我已经尝试通过将单行添加到单个 gtk_label 并将该标签添加到垂直框来显示它,但结果是一样的。
问题
有什么方法可以使我的 gtk 程序与 Ubuntu 控制台中的 sizing/look 相同?
代码如下:
void Zarzadzanie::Wykonaj(GtkWidget* widget, GtkWidget* data)
{
string buffer;
string n="";
GtkWidget *dialog, *label, *vbox, *window2,*viewer;
//Creating my dialog
dialog=gtk_dialog_new_with_buttons("Polaczenia sieciowe",GTK_WINDOW(gtk_widget_get_toplevel(data)), GTK_DIALOG_MODAL, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,GTK_STOCK_REFRESH,
GTK_RESPONSE_OK, NULL );
vbox=gtk_vbox_new(0,0); //creating new vbox
window2=gtk_scrolled_window_new(0,0); //creating new scrolled window
gtk_button_set_label(GTK_BUTTON(gtk_dialog_get_widget_for_response(GTK_DIALOG(dialog),GTK_RESPONSE_CLOSE)),"Zamknij Okno"); //setting label in dialog
gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(window2),400); //setting min height in scrollable window
gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(window2),700); //setting min width in scrollable window
viewer=gtk_text_view_new(); //creating new textview
gtk_text_view_set_editable(GTK_TEXT_VIEW (viewer), FALSE); //setting textview to not editable
gtk_container_add(GTK_CONTAINER(window2), viewer); //adding textview to scrollable window
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),window2,0,0,0); //adding scrollable window to dialog
ifstream plik("Pomniejsze/netstat.txt"); //reading from file
if(plik==NULL) //checking if file is not empty
{
//i will add something later
}
else //if file is not empty
{
while(std::getline(plik, buffer)) //i read from file line by line
{
n+=buffer; //i add to "n" 1 line from file
n+="\n"; //i add "new line" to "n"
}
char* z=new char[n.length()+1]; //i create new temporary char*
strcpy(z, n.c_str()); //i copy content of "n" to temporary char*
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(viewer)),z,-1); //i set text of textview to text from temp char*
g_print(z); //i print out temporary char* which can be seen on attached image on left window
delete [] z; //i delete temporary char*
}
g_signal_connect(dialog, "delete-event",G_CALLBACK(gtk_widget_destroy), NULL); // i add delete event to "x" button
gtk_widget_show_all(dialog); //i show my dialog
gint resp=gtk_dialog_run(GTK_DIALOG(dialog)); //i run my dialog
if(resp==GTK_RESPONSE_CLOSE) //i check if user clicked button
{
gtk_widget_destroy(dialog); //if button is clicked i destroy dialog
}
else if(resp==GTK_RESPONSE_OK)
{
//something will be added later
}
}
您使用的是可变宽度字体。您真正想要的是 monospaced font,它是固定宽度的,因此所有字符将从一行排到另一行。
您可以使用 gtk_text_view_set_monospace()
来仅使用默认的等宽字体。此函数仅在 GTK+ 版本 3.16 之后可用。
Here's a list of common monospaced fonts. You can set the font to one of these using a PangoFontDescription
and gtk_widget_override_font()
。但是,由于这已被弃用并已被 set_monospace()
函数取代,因此您不需要手动强制使用某种等宽字体。
在左侧的附加图像中 window 我有 Ubuntu 带有文件内容的控制台,在右侧 window 我有相同文件的内容我的 GTK+ 程序。问题是我希望我的 GTK+ 程序准确地显示文件,因为它显示在我的 Ubuntu 控制台上。
我已经尝试通过将单行添加到单个 gtk_label 并将该标签添加到垂直框来显示它,但结果是一样的。
问题
有什么方法可以使我的 gtk 程序与 Ubuntu 控制台中的 sizing/look 相同?
代码如下:
void Zarzadzanie::Wykonaj(GtkWidget* widget, GtkWidget* data)
{
string buffer;
string n="";
GtkWidget *dialog, *label, *vbox, *window2,*viewer;
//Creating my dialog
dialog=gtk_dialog_new_with_buttons("Polaczenia sieciowe",GTK_WINDOW(gtk_widget_get_toplevel(data)), GTK_DIALOG_MODAL, GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,GTK_STOCK_REFRESH,
GTK_RESPONSE_OK, NULL );
vbox=gtk_vbox_new(0,0); //creating new vbox
window2=gtk_scrolled_window_new(0,0); //creating new scrolled window
gtk_button_set_label(GTK_BUTTON(gtk_dialog_get_widget_for_response(GTK_DIALOG(dialog),GTK_RESPONSE_CLOSE)),"Zamknij Okno"); //setting label in dialog
gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(window2),400); //setting min height in scrollable window
gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(window2),700); //setting min width in scrollable window
viewer=gtk_text_view_new(); //creating new textview
gtk_text_view_set_editable(GTK_TEXT_VIEW (viewer), FALSE); //setting textview to not editable
gtk_container_add(GTK_CONTAINER(window2), viewer); //adding textview to scrollable window
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),window2,0,0,0); //adding scrollable window to dialog
ifstream plik("Pomniejsze/netstat.txt"); //reading from file
if(plik==NULL) //checking if file is not empty
{
//i will add something later
}
else //if file is not empty
{
while(std::getline(plik, buffer)) //i read from file line by line
{
n+=buffer; //i add to "n" 1 line from file
n+="\n"; //i add "new line" to "n"
}
char* z=new char[n.length()+1]; //i create new temporary char*
strcpy(z, n.c_str()); //i copy content of "n" to temporary char*
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(viewer)),z,-1); //i set text of textview to text from temp char*
g_print(z); //i print out temporary char* which can be seen on attached image on left window
delete [] z; //i delete temporary char*
}
g_signal_connect(dialog, "delete-event",G_CALLBACK(gtk_widget_destroy), NULL); // i add delete event to "x" button
gtk_widget_show_all(dialog); //i show my dialog
gint resp=gtk_dialog_run(GTK_DIALOG(dialog)); //i run my dialog
if(resp==GTK_RESPONSE_CLOSE) //i check if user clicked button
{
gtk_widget_destroy(dialog); //if button is clicked i destroy dialog
}
else if(resp==GTK_RESPONSE_OK)
{
//something will be added later
}
}
您使用的是可变宽度字体。您真正想要的是 monospaced font,它是固定宽度的,因此所有字符将从一行排到另一行。
您可以使用 gtk_text_view_set_monospace()
来仅使用默认的等宽字体。此函数仅在 GTK+ 版本 3.16 之后可用。
Here's a list of common monospaced fonts. You can set the font to one of these using a PangoFontDescription
and gtk_widget_override_font()
。但是,由于这已被弃用并已被 set_monospace()
函数取代,因此您不需要手动强制使用某种等宽字体。