我已经试了几天了,只是想将一个Base64字符串解码并添加到Gtk::TextView中。以下是代码:
txtbuffer_ = Gtk::TextBuffer::create();
txtview_.set_buffer(txtbuffer_);
const Glib::ustring str = Glib::Base64::decode("YmJi3A==");
txtbuffer_->set_text(str);
当我运行程序时,出现了以下错误:
Gtk-CRITICAL **: gtk_text_buffer_emit_insert: assertion 'g_utf8_validate (text, len, NULL)' failed
这个错误只会在使用Unicode字符时出现。当文本为ASCII时,一切都运行良好。 我尝试了三个不同的base64解码器,使用了std::string和Glib::ustring与所有不同的解码器进行了尝试。我还尝试使用函数
Glib::locale_to_utf8()
,但是这给我带来了错误terminate called after throwing an instance of 'Glib::ConvertError'
。同时我也使用了Glib::convert
,但是出现了相同的错误。
我知道Gtk::TextView可以显示Unicode,因为如果我将文本设置为含有Unicode的字符串,它就能够正确地显示该文本。我读到Gtk::TextView以UTF-8格式显示文本,所以我认为我的问题在于解码后的字符串没有编码成UTF-8,但我并不确定。因此我的问题是如何让Gtk::TextView显示解码后的base64?
补充说明:我正在使用Gtkmm的3.8版本
已测试使用3.12版本,出现相同错误信息
最小程序如下:
//test.h#ifndef TEST_H_
#define TEST_H_
#include <gtkmm.h>
class MainWindow : public Gtk::Window
{
public:
MainWindow();
virtual ~MainWindow();
protected:
Gtk::Box box_main;
Gtk::TextView txtview_;
Glib::RefPtr<Gtk::TextBuffer> txtbuffer_;
};
#endif /* TEST_H_ */
//test.cpp
#include "test.h"
MainWindow::MainWindow()
{
Gtk::Window::add(box_main);
box_main.pack_start(txtview_);
txtbuffer_ = Gtk::TextBuffer::create();
txtview_.set_buffer(txtbuffer_);
const Glib::ustring str = Glib::Base64::decode("YmJi3A==");
txtbuffer_->set_text(str);
Gtk::Window::show_all_children();
}
MainWindow::~MainWindow()
{
}
//main.cpp
#include "test.h"
int main(int argc, char* argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "test.program");
MainWindow mw;
return app->run(mw);
}