GTKMM按钮在网格中

时间:2015-05-13 04:39:20

标签: c++ gtkmm

刚刚学会了GTKMM的概念,并发现我很喜欢这个,而不仅仅是C ++,这在终端编程时非常无聊。当我决定创建一个会生成8 x 8按钮的代码时,我遇到了一个问题,就是如何将每个按钮设置到我想要的位置,因为我创建的代码只是水平地创建了64个按钮。有人能帮我一下吗?感谢

examplewindow.h(代码类属性和方法)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

private:
  // Signal handlers:
  void on_button_numbered(const Glib::ustring& data);

  // Child widgets:
  Gtk::Grid m_grid;
  Gtk::Button button[8][8];
};

#endif /* GTKMM_EXAMPLEWINDOW_H */

examplewindow.cc(修改属性和方法的代码)

#include <iostream>
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
{
  set_title("Mr. Sandman");
  set_border_width(12);
  int i, j;
  add(m_grid);
  for (int i = 0; i < 8; i++)
  {
      for (int j = 0; j < 8; j++)
      {
          m_grid.add(button[i][j]);
      }
  }

  for (int i = 0; i < 8; i++)
  {
      for (j = 0; j < 8; j++)
      {
          button[i][j].signal_clicked().connect(
    sigc::bind<Glib::ustring>( sigc::mem_fun(*this,
      &ExampleWindow::on_button_numbered), "button 1") );
      }
  }
  m_grid.show_all();
}

ExampleWindow::~ExampleWindow()
{
}


void
ExampleWindow::on_button_numbered(const Glib::ustring& data)
{
  std::cout << data << " was pressed" << std::endl;
}

main.cc(主要功能)

#include "examplewindow.h"
#include <gtkmm/application.h>

int main(int argc, char *argv[])
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  ExampleWindow window;

  // Shows the window and returns when it is closed.
  return app->run(window);
}

1 个答案:

答案 0 :(得分:1)

而不是使用m_Grid.add()尝试使用attach()attach_next_to()

所以你可以在你的循环中给出行/列位置,或者在按钮1下面添加按钮9,在按钮1的右边添加按钮2

https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Grid.html#a9c425e95660daff60a77fc0cafc18115