首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网格中的GTKMM按钮

网格中的GTKMM按钮
EN

Stack Overflow用户
提问于 2015-05-13 12:39:20
回答 1查看 1.6K关注 0票数 1

刚刚学习了GTKMM的概念,发现我很喜欢这个,而仅仅是C++,在终端上编程是相当无聊的。当我决定创建一个代码来创建一个8x8按钮的表格时,我遇到了一个问题,那就是如何将每个按钮设置到我想要的位置,因为我创建的代码水平方向上只有64个按钮。有人能帮我吗?谢谢

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

代码语言:javascript
复制
#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 (修改属性和方法的代码)

代码语言:javascript
复制
#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 (主函数)

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-13 14:11:16

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

因此,您可以在循环中给出行/列位置,或者将按钮9附加在按钮1下面,将按钮2附加到按钮1的右侧

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30205808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档