将 Window::set_title 与 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;
}
I am trying to rename the title of my application's main window, but when trying so, the name get truncated. I tried to see if it was a conversion problem but I realy can't find why this happen. Try this little program.
Hit cancel to see the default application name in the title bar, but if you choose a file, it should display the first line of the file as title but instead truncated it... The trucation is always 3 caracters before the end of the string, and three dots "..." is added.
What am I doing wrong?? or is it a bug with my gtkmm version or something? I use gtkmm-2.4
Thanks in advance.
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里工作得很好。
Works fine here.
好吧,我终于自己找到了解决方案。我写在这里以防其他人遇到同样的问题。我不知道这是一个错误还是什么,但似乎 GTK 将 '\n' 之前的最后三个字符替换为 '...'。换句话说,用于重命名窗口的字符串不能包含任何“\n”,否则 set_title 将不会显示完整名称(它将在“\n”之前停止三个字符)。
因此,就我而言,由于我使用了“getline()”,所以我只是从字符串末尾删除了“\n”。
Note that I had to use a 'std::string' instead of a 'Gtk::ustring' since it doesn't handle the 'operator-' on 'end()' function.
OK I finally found the solution myself. I write it here in case someone else gets the same issue. I don't know if it is a bug or what, but seems like GTK substitutes the three last characters before a '\n' to '...'. In other words, the string used to rename the window must not contain any '\n' or else set_title will not show the full name (it will stop three characters before '\n').
Therefore, In my case, since I used 'getline()', I simply removed the '\n' from the end of the string.
Note that I had to use a 'std::string' instead of a 'Gtk::ustring' since it doesn't handle the 'operator-' on 'end()' function.