**编辑:我试过的是下面的内容(编译但是扼杀不起作用)**
class NibblerWindow : public Gtk::Window
{
private:
Gtk::Grid grid;
Gtk::Image tmp;
Gtk::Image gridElem[5];
public:
bool initImgArray();
NibblerWindow();
~NibblerWindow(){}
};
bool MyWindow::initImgArray()
{
int i = 0;
while (i != 5)
{
this->gridElem[i].set("someimage.gif");
++i;
}
return true;
}
MyWindow::MyWindow()
{
this->set_title("Work in progress");
this->set_default_size(512, 512);
this->add(grid);
this->grid.add(gridElem[0]);
this->grid.add(gridElem[1]);
this->grid.show();
this->grid.show_all_children();
}
大家好,
我基本上是在尝试构建一个游戏网格(如草稿或检查器),每个子窗口小部件都包含相同的图像模式。我找到的唯一功能方法是单独创建每个子窗口小部件,并在我的类的构造函数列表中构建它们,如下所示:
class MainWindow {
private:
Gtk::Grid gameGrid;
Gtk::Image gridElement1;
Gtk::Image gridElement2;
Gtk::Image gridElement3;
Gtk::Image gridElement4;
/* and so on ... */
/* with some member functions */
public:
MainWindow();
~MainWindow();
};
MainWindow::MainWindow() :
gridElement1("background.png"),
gridElement2("background.png"), gridElement3("background.png"),
gridElement4("background.png") /* et caetera ...*/
{
this->add(grid);
this->add(gridElement1);
this->gameGrid.attach_next_to(gridElement1, gridElement2, Gtk::POS_LEFT, 40, 41);
this->gameGrid.attach_next_to(gridElement2, gridElement3, Gtk::POS_LEFT, 40, 41);
this->gameGrid.attach_next_to(gridElement2, gridElement2, Gtk::POS_LEFT, 40, 41);
/* et caetera */
}
我觉得不适合,特别是对于这种情况,以一种静态方式实例化我的对象......我想用某种循环初始化它们。 我已经多次尝试将它们放在向量或数组中,但它表明它不起作用...考虑到我们不应该操纵<的直接指针strong> Gtk :: Objects ,但 Gtk :: RefPtr 代替。
有人知道另一种方式来声明我的Image对象吗?