2016-04-23 28 views
4

我无法正确获取GTK3中的TreeView。在GTK3树视图中包装文本

我把它用这种方式来包装:

Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2); 
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell()) 
     ->property_wrap_mode().set_value(Pango::WRAP_WORD_CHAR); 
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell()) 
     ->property_wrap_width().set_value(200); 

这工作,文本被包裹,但是当我调整窗口的大小,使之更大,有很多的上述难看的白色空间和在长文本的单元格下面。看来,GTK基于包装宽度保留了单元格的高度。这对我来说毫无意义。

我试图让周围设置需要signal_check_resize与计算这样需要宽度:

 Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2); 
     auto width = this->get_allocated_width() 
      - mTreeView.get_column(0)->get_width() 
      - mTreeView.get_column(1)->get_width(); 
     static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell()) 
      ->property_wrap_width().set_value(width-100); 
     this->forceRecreateModel = true; //Needed to work 

但是这让我只能使窗口更大。它在调整大小后不能缩小。

问题是,这是如何正确完成的?

我在Arch linux上使用gtk3.20.3-1和gtkmm3.20.1-1。

编辑:在标题中的固定错字...

+1

哦,我的......我从来不想触摸任何人的感受。 :) – kracejic

回答

4

最后我发现如何做到这一点。

在窗口的设置中(对于我的窗口派生类的构造函数),需要将列设置为AUTOSIZE以允许缩小宽度。

//Last Column setup 
{ 
    mTreeView.append_column("Translation", mColumns.mEnglish); 
    Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2); 
    pColumn->set_sizing(Gtk::TreeViewColumnSizing::TREE_VIEW_COLUMN_AUTOSIZE); 
    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell()) 
     ->property_wrap_mode().set_value(Pango::WRAP_WORD_CHAR); 
} 

此外,还需要在每个调整大小上设置正确的包裹宽度。如果没有这个,那么行的高度就和当前设置的wrap_width所需要的一样大,而不考虑当前的宽度(导致顶部有很大的填充,当更多地拉伸并且禁止使窗口更小时)。

此代码也在构造函数中。

this->signal_check_resize().connect([this]() 
{ 
    //calculate remaining size 
    Gtk::TreeViewColumn* pColumn = mTreeView.get_column(2); 
    auto width = this->get_allocated_width() 
     - mTreeView.get_column(0)->get_width() 
     - mTreeView.get_column(1)->get_width()-30; 

    //minimum reasonable size for column 
    if(width < 150) 
     width = 150; 

    static_cast<Gtk::CellRendererText *>(pColumn->get_first_cell()) 
     ->property_wrap_width().set_value(width); 

    //debounce 
    static auto oldsize = 0; 
    { 
     oldsize = width; 

     //trigger redraw of mTreeView (by clearing and refilling Model, 
     //it is done in 100ms pulse) 
     this->mRedrawNeeded = true; 
    } 
}); 

也许值得注意的是,我已将mTreeView封装在Gtk :: ScrolledWindow中。所以这是一个在柱子设置之前出现的块。 :)

//in class is: Gtk::ScrolledWindow mScrollForResults; 

//scrolling area 
mGrid.attach(mScrollForResults, 0,2,10,1); 
mScrollForResults.set_hexpand(); 
mScrollForResults.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, 
          Gtk::PolicyType::POLICY_ALWAYS); 
mScrollForResults.set_margin_top(10); 
mScrollForResults.set_min_content_width(400); 
mScrollForResults.set_min_content_height(200); 
mScrollForResults.add(mTreeView); 

//results treeView 
mRefListStore = Gtk::ListStore::create(mColumns); 
mTreeView.set_model(mRefListStore); 
mTreeView.set_hexpand(); 
mTreeView.set_vexpand();