[英]Window title truncation when using Window::set_title with gtkmm
我試圖重命名應用程序主窗口的標題,但嘗試這樣做時,該名稱將被截斷。 我試圖查看這是否是轉換問題,但我確實找不到發生這種情況的原因。 試試這個小程序。 點擊取消,在標題欄中看到默認的應用程序名稱,但是如果選擇一個文件,它將顯示文件的第一行作為標題,但是將其截斷...字符串的末尾總是3個字符,並添加三個點“ ...”。
我究竟做錯了什么?? 還是我的gtkmm版本有問題? 我使用gtkmm-2.4預先感謝。
#include <iostream>
#include <gtkmm.h>
using namespace std;
using namespace Gtk;
using namespace Glib;
class AppWindow : public Window {
public:
AppWindow();
protected:
void onMenuFileOpen();
private:
ustring app_name;
void OpenScript(const ustring sScriptFile);
};
AppWindow::AppWindow() {
app_name = "default app_name, very long name, with !!^spectal caractères à afficher, and there is no name truncation";
//set_title(app_name);
set_default_size(600, 600);
onMenuFileOpen();
}
void AppWindow::onMenuFileOpen() {
FileChooserDialog dialog("Choose a file", FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(*this);
//Add response buttons the the dialog:
dialog.add_button(Stock::CANCEL, RESPONSE_CANCEL);
dialog.add_button(Stock::OPEN, RESPONSE_OK);
//Plain text filter
FileFilter filter_text;
filter_text.set_name("plain text");
filter_text.add_mime_type("text/plain");
dialog.add_filter(filter_text);
//Show the dialog and wait for a user response:
if(dialog.run() == RESPONSE_OK) {
OpenScript(dialog.get_filename());
}
//HERE, I RENAME THE WINDOW
set_title(app_name);
cout << app_name << endl;
}
void AppWindow::OpenScript(const ustring sScriptFile) {
RefPtr<IOChannel> file = IOChannel::create_from_file(sScriptFile,"r");
IOStatus status;
ustring one_line;
if(file->get_flags() & IO_FLAG_IS_READABLE) {
status = file->read_line(one_line);
app_name=one_line;
}
file->close();
}
int main(int argc, char *argv[]) {
Main kit(argc, argv);
AppWindow window;
//Shows the window and returns when it is closed.
Main::run(window);
return 0;
}
在這里工作正常。
好的,我終於找到了解決方案。 如果有人遇到同樣的問題,我在這里寫下。 我不知道這是一個錯誤還是什么,但是GTK似乎將'\\ n'之前的最后三個字符替換為'...'。 換句話說,用於重命名窗口的字符串不能包含任何“ \\ n”,否則set_title將不會顯示全名(它將在“ \\ n”之前停止三個字符)。
因此,就我而言,由於我使用了“ getline()”,因此我只是從字符串末尾刪除了“ \\ n”。
app_name.erase(app_name.end()-1);
請注意,我必須使用'std :: string'而不是'Gtk :: ustring',因為它不能處理'end()'函數上的'operator-'。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.