-----------qt-----------C++的模板的使用, ---定义一个模块类--------

来源:互联网 发布:网络安全设备有哪些 编辑:程序博客网 时间:2024/06/11 00:40



转自:http://qt-project.org/forums/viewthread/14782

If we’re throwing around solutions anyway: you could even create a class template that adds this function to any widget you want, including all stock widgets. :-)

  1. template <class T>
  2. public IDClass<T>: public T {
  3. public:
  4.    IDClass(int id, QWidget* parent = 0):
  5.       m_id(id),
  6.       T(parent)
  7.    {}
  8.  
  9.    void setId(int newId) {m_id = newId};
  10.    int id() const {return m_id;}
  11.  
  12. private:
  13.    int m_id;
  14. }
  15.  
  16. typedef IDClass<QLineEdit> IdLineEdit;
  17. typedef IDClass<QPushButton> IdPushButton;
  18. //etc.
0 0
原创粉丝点击