通过基类自动执行窗口编程
Automate window programming through a base class
我已经为一个应用程序编写了几个窗口,这些窗口都继承了Gtkmm::Window
。在这一点上,我想自动化该过程。现在,以下结构很突出:
class MyWindow : public Gtk::Window
{
public:
MyWindow();
virtual ~MyWindow();
//...
private:
void registerLayouts(); // Adds layouts to the window.
void registerWidgets(); // Adds widgets to the layouts.
//...
};
和构造函数:
MyWindow::MyWindow()
{
registerLayouts(); // Cannot be virtual: in constructor.
registerWidgets(); // Cannot be virtual: in constructor.
//...
}
所以问题是每次必须对新窗口进行编程时,所有这些都必须手动完成(即复制/粘贴(,因为registerLayouts()
和registerWidgets()
是在构造时调用的,因此无法virtual
。
理想情况下,我将有一个可以继承的基类,它将使我可以选择重写这两个方法并处理其余的工作:它将在适当的位置调用这两个方法。
问题是,我还没有找到这个合适的位置。我看过不同的信号处理程序,但似乎没有。
你知道我怎么做吗?
MFC 具有执行类似于我需要的操作的CDialog::OnInitDialog()
。
您可以将工作委托给单独的类:
class MyWindow : public Gtk::Window
{
//public: *** EDIT ***
protected:
template <typename LayoutManager>
MyWindow(LayoutManager const& lm)
{
lm.registerLayouts(this);
lm.registerWidgets(this);
}
};
class SubWindow : public MyWindow
{
class LM { /* ... */ };
public:
SubWindow() : MyWindow(LM()) { }
};
(编辑:改进的模式隐藏了子类的布局管理器...
或者,整个类作为模板(可能优于上述(:
template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
public:
MyWindow()
{
LayoutManager lm(*this);
lm.registerLayouts();
lm.registerWidgets();
}
};
class SpecificLayoutManager { /* ... */ };
using SpecificWindow = MyWindow<SpecificLayoutManager>;
如果你也需要布局管理器来清理(我自己不熟悉GTK...
template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
LayoutManager lm;
public:
MyWindow() : lm(*this)
{
lm.registerLayouts();
lm.registerWidgets();
}
virtual ~MyWindow()
{
// still access to lm...
}
};
重要的旁注:在所有变体中,我们还没有一个完全构造的派生类——因此在布局管理器中强制转换为后者是不合法的(尝试了奇怪的重复模板模式,但出于完全相同的原因放弃了这个想法:也需要在 base 的构造函数中强制转换为派生(。
编辑以响应评论:有关如何管理子类的其他成员的示例(使用上面的第三个变体,模板类一和布局管理器成员;lm
成员现在需要protected
(:
class SubWindowLayoutManager
{
template <typename>
friend class MyWindow;
friend class SubWindow;
int someMember;
void registerLayouts() { }
void registerWidgets() { }
};
class SubWindow : public MyWindow<SubWindowLayoutManager>
{
void doSomething()
{
lm.someMember = 77;
}
};
此外,完全没有模板的新变体:
class MyWindow : public Gtk::Window
{
protected:
class LayoutManager
{
public:
virtual void registerLayouts(MyWindow* parent) = 0;
virtual void registerWidgets(MyWindow* parent) = 0;
};
std::unique_ptr<LayoutManager> lm;
MyWindow(std::unique_ptr<LayoutManager> lm)
: lm(std::move(lm))
{
this->lm->registerLayouts(this);
this->lm->registerWidgets(this);
}
};
class SubWindow : public MyWindow
{
class LM : public LayoutManager
{
public:
void registerLayouts(MyWindow* parent) override { }
void registerWidgets(MyWindow* parent) override { }
int someMember;
};
// convenience access function:
inline LM& lm()
{
return *static_cast<LM*>(MyWindow::lm.get());
}
public:
SubWindow() : MyWindow(std::make_unique<LM>()) { }
void doSomething()
{
//static_cast<LM*>(lm.get())->someMember = 77;
lm().someMember = 77;
}
};
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 通过基类自动执行窗口编程
- 我在哪里编写在退出 C++ 窗口窗体应用程序之前执行的代码部分
- 在C++中,系统如何将这些对象中的每一个与执行程序的窗口相关联?
- 发送到另一个窗口的鼠标移动消息不执行任何操作
- 在窗口执行目标 TLS 回调之前注入 dll
- 将程序(.EXE)作为线程 - 窗口C 执行
- 是否可以在不重新打开重新打开 win32 窗口的情况下执行需要提升的任务
- 在窗口中执行 Linux c++ 程序
- 如何在QT中关闭子窗口后执行功能
- 有什么方法可以判断分叉/执行Qt应用程序的主窗口何时显示?
- 如何在OSX中执行时强制Qt应用程序成为活动/前台窗口
- 如何在替换调用方并在 xp/vista/7 上运行的窗口中从我的 VS2008/C++ 应用程序执行程序
- 如何在执行回调时隐藏顶级GTK窗口
- c++ Shell在已打开的窗口中执行"open"
- WSARecv在套接字编程窗口中执行大约需要3秒
- c++可执行文件打开命令窗口
- c++ Win32多个窗口执行相同的操作
- 在窗口处理mfc时执行程序
- 如何执行批处理文件从C/ c++ w/o控制台窗口