两个 Gtk::Grids 持有 Gtk::DrawingArea:尺码问题
Two Gtk::Grids holding Gtk::DrawingArea: sizing issue
我有一个自定义 GameBoard
,它继承自 Gtk::VBox
容器并堆叠两个子 Gtk::Grid
容器:
class GameBoard : public Gtk::VBox
{
public:
GameBoard();
virtual ~GameBoard();
private:
Gtk::Grid m_nextDiscArea;
Gtk::Grid m_gameBoardGrid;
};
GameBoard
构造函数是这样实现的:
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);
}
我的 部分涵盖了我面临的问题,但该解决方案不适用于这种情况。问题是两个grid的child widgets显示的时候大小不一样,6x7的grid的圆盘比较小,看起来有点奇怪:
我试过 set_v/hexpand
、set_v/halign
但似乎没有任何效果。如何让两个网格的子窗口小部件具有相同的大小?
为了完成,这里是Disc
class的界面。它只是使用 Cairo 绘制了一个带有彩色背景的圆盘:
class Disc : public Gtk::DrawingArea
{
public:
Disc();
Disc(double p_red, double p_green, double p_blue, double p_alpha);
virtual ~Disc();
protected:
// Signal handlers:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& p_context) override;
private:
double m_red {0.0};
double m_green {0.0};
double m_blue {0.0};
double m_alpha {0.0};
};
theGtkNerd 给我解决了这个问题。可以使用 Gtk::Paned
代替 Gtk::VBox
来打包两个 Gtk::Grid
。 GameBoard
class 则变为:
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);
}
这是结果的屏幕截图。调整大小使所有圆盘保持比例:
我不太确定为什么会按预期工作,到目前为止我在文档中没有发现任何相关内容,但确实如此。
我有一个自定义 GameBoard
,它继承自 Gtk::VBox
容器并堆叠两个子 Gtk::Grid
容器:
class GameBoard : public Gtk::VBox
{
public:
GameBoard();
virtual ~GameBoard();
private:
Gtk::Grid m_nextDiscArea;
Gtk::Grid m_gameBoardGrid;
};
GameBoard
构造函数是这样实现的:
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
但似乎没有任何效果。如何让两个网格的子窗口小部件具有相同的大小?
为了完成,这里是Disc
class的界面。它只是使用 Cairo 绘制了一个带有彩色背景的圆盘:
class Disc : public Gtk::DrawingArea
{
public:
Disc();
Disc(double p_red, double p_green, double p_blue, double p_alpha);
virtual ~Disc();
protected:
// Signal handlers:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& p_context) override;
private:
double m_red {0.0};
double m_green {0.0};
double m_blue {0.0};
double m_alpha {0.0};
};
theGtkNerd 给我解决了这个问题。可以使用 Gtk::Paned
代替 Gtk::VBox
来打包两个 Gtk::Grid
。 GameBoard
class 则变为:
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);
}
这是结果的屏幕截图。调整大小使所有圆盘保持比例:
我不太确定为什么会按预期工作,到目前为止我在文档中没有发现任何相关内容,但确实如此。