将 Window::set_title 与 gtkmm 结合使用时窗口标题被截断

发布于 2024-10-18 13:48:04 字数 2023 浏览 2 评论 0原文

我正在尝试重命名应用程序主窗口的标题,但是在尝试这样做时,名称会被截断。我试图看看这是否是一个转换问题,但我真的找不到为什么会发生这种情况。试试这个小程序。 点击取消即可在标题栏中看到默认的应用程序名称,但如果您选择一个文件,它应该将文件的第一行显示为标题,但会截断它...截断始终是字符串末尾之前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-25 13:48:04

在这里工作得很好。

  • 也许你的文件不是UTF-8编码?
  • 如果标题比标题栏中的空格长,则标题被截断是正常的吗?

Works fine here.

  • maybe your file is not in UTF-8 encoding?
  • it's normal for the title to be truncated if it's longer than the space in the titlebar?
习ぎ惯性依靠 2024-10-25 13:48:04

好吧,我终于自己找到了解决方案。我写在这里以防其他人遇到同样的问题。我不知道这是一个错误还是什么,但似乎 GTK 将 '\n' 之前的最后三个字符替换为 '...'。换句话说,用于重命名窗口的字符串不能包含任何“\n”,否则 set_title 将不会显示完整名称(它将在“\n”之前停止三个字符)。

因此,就我而言,由于我使用了“getline()”,所以我只是从字符串末尾删除了“\n”。

app_name.erase(app_name.end()-1);

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.

app_name.erase(app_name.end()-1);

Note that I had to use a 'std::string' instead of a 'Gtk::ustring' since it doesn't handle the 'operator-' on 'end()' function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文