如何自动将 FLTK 的Fl_Input对象一个堆叠在另一个下面
How to automatically stack FLTK's Fl_Input-objects one below the other
我基于 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
型对象的声明没有考虑到这种常量。 - 在每个新对象声明之后,都会打印计数器的递增值,但实际上它不受影响。
问题:如何使其正常工作?(我的印象是我在声明各种源文件中的常量时弄得一团糟......
(可选)附带问题:定义一个既构建对象又放置对象的类是否被认为是不好的做法?我的意思是,让一个对象一次做"太多"的事情是一种不好的做法吗?(我也想写一个放置对象的函数,但我什至不知道从哪里开始。
结语:我目前使用的代码实际上运行良好,但它只有一个源文件(其中我称之为"外部"的常量是全局常量)。将其划分为多个源文件时出现问题。
方法 1:在类中使用静态。 这不是一个非常好的方法,因为它只知道自己,而不了解其他任何事情。 问题是,如果您有第二个使用 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
方法二:问家长。 父母跟踪最后一个孩子在哪里。 这稍微好一点,因为父母控制了孩子的位置。 孩子需要做的就是问父母它在哪里。
// 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++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 如何在C++中将函数发送到另一个进程
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口