两个Gtk :: Grids持有Gtk :: DrawingArea:大小调整问题

时间:2018-01-28 14:36:56

标签: c++ gtk gtkmm

我有一个自定义<list_name> = [ <expression> for <targets> in <iterable> if <test_expression> for <targets2> in <iterable2> ] ,它继承自GameBoard容器并堆叠两个子Gtk::VBox容器:

Gtk::Grid

class GameBoard : public Gtk::VBox { public: GameBoard(); virtual ~GameBoard(); private: Gtk::Grid m_nextDiscArea; Gtk::Grid m_gameBoardGrid; }; 构造函数以这种方式实现:

GameBoard

我所面临的问题部分涵盖在last post中,但该解决方案不适用于此案例。问题是,当显示两个网格中的子窗口小部件时,它们具有不同的大小,而6x7网格具有较小的光盘,这看起来有点奇怪:

enter image description here

我尝试使用GameBoard::GameBoard() { const int nbRows{6}; const int nbColumns{7}; m_nextDiscArea.set_row_homogeneous(true); m_nextDiscArea.set_column_homogeneous(true); for(int col{0}; col < nbColumns; ++col) { Disc* noDisc{new Disc}; m_nextDiscArea.attach(*noDisc, col, 0, 1, 1); } m_gameBoardGrid.set_row_homogeneous(true); m_gameBoardGrid.set_column_homogeneous(true); for(int row{0}; row < nbRows; ++row) { for(int col{0}; col < nbColumns; ++col) { Disc* noDisc{new Disc}; m_gameBoardGrid.attach(*noDisc, col, row, 1, 1); } } pack_start(m_nextDiscArea); pack_start(m_gameBoardGrid); } set_v/hexpand,但似乎没有任何效果。如何为两个网格的子窗口小部件设置相同的大小?

完成后,这是set_v/halign类的界面。它只是使用Cairo绘制带有彩色背景的光盘:

Disc

1 个答案:

答案 0 :(得分:0)

theGtkNerd 给了我这个问题的解决方案。可以使用Gtk::Paned,而不是使用Gtk::VBox打包两个Gtk::Grid。然后GameBoard类成为:

class GameBoard : public Gtk::Paned
{

public:

    GameBoard();
    virtual ~GameBoard();

private:

    Gtk::Grid m_nextDiscArea;
    Gtk::Grid m_gameBoardGrid;

};

构造函数可以实现为:

GameBoard::GameBoard()
{
    // The two Paned areas are vertically aligned:
    set_orientation(Gtk::Orientation::ORIENTATION_VERTICAL);

    const int nbRows{6};
    const int nbColumns{7};

    m_nextDiscArea.set_row_homogeneous(true);
    m_nextDiscArea.set_column_homogeneous(true);

    for(int col{0}; col < nbColumns; ++col)
    {
        Disc* noDisc{new Disc};
        noDisc->set_size_request(40, 40); // Give minimal size.
        m_nextDiscArea.attach(*noDisc, col, 0, 1, 1);
    }

    m_gameBoardGrid.set_row_homogeneous(true);
    m_gameBoardGrid.set_column_homogeneous(true);

    for(int row{0}; row < nbRows; ++row)
    {
        for(int col{0}; col < nbColumns; ++col)
        {
            Disc* noDisc{new Disc};
            noDisc->set_size_request(40, 40); // Give minimal size.
            m_gameBoardGrid.attach(*noDisc, col, row, 1, 1);
        }
    }

    // Layout setup: 'true' for m_nextDiscArea to be expanded   
    // 'false' to make sure it is not shrinkable (we can make
    // it hidden using the paned). Same for m_gameBoardGrid.
    pack1(m_nextDiscArea, true, false);
    pack2(m_gameBoardGrid, true, false);
}

以下是结果的屏幕截图。调整大小可以保持所有光盘的比例:

Grid with Gtk::Paned

我不确定为什么这会按预期工作,到目前为止我在文档中找不到任何内容,但确实如此。