我已经创建了一个基于 FLTK 的 Fl_Input
类的定制对象,它绘制屏幕输入框并自动将其放置在下方上一个 已声明的框。这是通过使用“外部”(在“类实现外部”的意义上)计数器来实现的,该计数器用于计算框的 y 坐标。
这是该类的简化版本:
// Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
class Example : public Fl_Input {
public:
Example(const char * str); // constructor
private:
int ex_y(); // compute y-coordinate of current box based on the previous ones
};
#endif // EXAMPLE_H
下面是成员函数和构造函数:
// Example.cpp
#include "Example.h"
int inbox_y1; // y-coord of first box; an example of “external” constants
int inbox_num; // counter: number of input boxes declared; used to compute their y-coord
Example::Example(const char * str)
: Fl_Input(200, ex_y(), 200, 25, str)
{
++inbox_num;
}
int Example::ex_y()
{
return inbox_y1 + (25+25)*inbox_num;;
}
最后是主文件(我在其中将对象的声明包装在自定义函数中):
// main.cpp
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
#include <string>
#include <iostream>
#include "Example.h"
void place_inboxes()
{
int inbox_y1 = 25; // not used by the following objects: the first one is attached to the upper window
int inbox_num = 0; // the counter is not incremented by the following declarations
Example * ex1 = new Example("First Input Box");
std::cout << "First call: " << inbox_num << '\n';
Example * ex2 = new Example("Second Input Box");
std::cout << "Second call: " << inbox_num << '\n';
Example * ex3 = new Example("Third Input Box");
std::cout << "Third call: " << inbox_num << '\n';
}
int main()
{
Fl_Window * win = new Fl_Window(600, 600, "Test of 'Example' class");
{
place_inboxes();
}
win->end();
win->show();
return Fl::run();
}
此编译和链接成功,但(即使盒子确实堆叠)结果不是我预期的:
- 第一个框附加到窗口的上部,而它应该位于窗口下方,距离由(“外部”)常量
inbox_y1
定义。这使我认为Example
类型对象的声明没有考虑到这样的常量。 - 在每个新对象声明之后,计数器的增量值被打印出来,但实际上它不受影响。
问题: 我怎样才能让它正常工作? (我的印象是我在声明各种源文件中的常量时弄得一团糟......)
(可选)附带问题: 定义一个既构建对象又将其放置的类是否被认为是不好的做法?我的意思是,让一个对象一次做“太多”的事情是不好的做法吗? (我也想写一个放置对象的函数,但我什至不知道从哪里开始。)
最后说明: 我目前使用的代码实际上运行良好,但它只有一个一个 源文件(其中我称为“外部”的常量是全局常量)。将其分成多个源文件时出现问题。
最佳答案
方法一:在类中使用statics。这不是一个很好的方法,因为它只知道自己而不知道其他任何东西。问题是,如果您有第二个使用 Example 的窗口,它会从最后一个窗口停止的位置开始,因此您需要一个重置函数来将所有内容放回到开始处。对您的代码所做的更改是
// Example.h
class Example : public Fl_Input {
public:
Example(const char * str); // constructor
static int inbox_y1, inbox_num;
static void Reset();
...
// Example.cpp
int Example::inbox_y1, Example::inbox_num;
void Example::Reset()
{
inbox_y1 = 25;
inbox_num = 0;
}
...
// main.cpp
// call Example::Reset() before you call place_inboxes
方法二:询问家长。 parent 跟踪最后一个 child 在哪里。这稍微好一点,因为 parent 控制 child 的位置。 child 需要做的就是询问 parent 它在哪里。
// Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Window.H>
class ExParent: public Fl_Window
{
public:
ExParent(int x, int y, const char* name);
int child_x();
int child_y();
void next_child_y();
private:
int cy;
};
class Example : public Fl_Input {
public:
Example(ExParent* parent, const char * str); // constructor
};
#endif // EXAMPLE_H
// Example.cpp
#include "Example.h"
Example::Example(ExParent* parent, const char * str)
: Fl_Input(parent->child_x(), parent->child_y(), 200, 25, str)
{
}
ExParent::ExParent(int x, int y, const char* name)
: Fl_Window(x, y, name)
, cy(25)
{
}
int ExParent::child_x() { return 200; }
int ExParent::child_y()
{
return cy;
}
void ExParent::next_child_y()
{
cy += 50;
}
int main()
{
ExParent * win = new ExParent(600, 600, "Test of 'Example' class");
Example * ex1 = new Example(win, "First Input Box");
win->next_child_y();
Example * ex2 = new Example(win, "Second Input Box");
win->next_child_y();
Example * ex3 = new Example(win, "Third Input Box");
win->end();
win->show();
return Fl::run();
}
关于c++ - 如何自动堆叠 FLTK 的 Fl_Input-objects 一个在另一个下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786615/