#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QDebug>class WidgetPrivate : public QWidget{ Q_OBJECT public: WidgetPrivate(QWidget *parent = 0); void setSource(QString arg); signals: void created(); private: QString m_source; private slots: void cr();};template<typename Derived>class Widget : public WidgetPrivate{ public: Widget(Derived *d,QWidget *parent = 0); protected: void mousePressEvent(QMouseEvent *); virtual void init();};class FirstWidget : public Widget<FirstWidget>{ public: FirstWidget(QWidget *parent = 0); void init();};class LastWidget : public Widget<LastWidget>{ public: LastWidget(QWidget *parent = 0); void init();};#endif // WIDGET_H
#include "widget.h"WidgetPrivate::WidgetPrivate(QWidget *parent) :QWidget(parent){ connect(this,SIGNAL(created()),this,SLOT(cr())); emit created();}void WidgetPrivate::cr(){ qDebug() << "create widget";}void WidgetPrivate::setSource(QString arg){ m_source = arg;}template<typename Derived>Widget<Derived>::Widget(Derived *d,QWidget *parent) :WidgetPrivate(parent){ qDebug() << Q_FUNC_INFO;}template<typename Derived>void Widget<Derived>::mousePressEvent(QMouseEvent *){ qDebug() << Q_FUNC_INFO;}FirstWidget::FirstWidget(QWidget *parent) :Widget<FirstWidget>(this,parent){ qDebug() << Q_FUNC_INFO;}void FirstWidget::init(){}LastWidget::LastWidget(QWidget *parent) :Widget<LastWidget>(this,parent){ qDebug() << Q_FUNC_INFO;}void LastWidget::init(){ setWindowTitle("Last");}
#include <QtGui/QApplication>#include <QWidget>#include <QDebug>#include "widget.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); FirstWidget f; f.setSource("f"); f.show(); LastWidget l; l.setSource("l"); l.show(); return a.exec();}
debug/main.o:main.cpp:(.rdata$_ZTV6WidgetI10LastWidgetE[vtable for Widget<LastWidget>]+0xe8): undefined reference to `Widget<LastWidget>::init()'debug/main.o:main.cpp:(.rdata$_ZTV6WidgetI11FirstWidgetE[vtable for Widget<FirstWidget>]+0xe8): undefined reference to `Widget<FirstWidget>::init()'
template <typename Derived>BasicWindow<Derived>::BasicWindow(Derived *d,QWidget *parent) :BasicWindowPrivate(parent){ #ifndef Q_OS_MAC || Q_OS_MACX || Q_OS_MAC64 || Q_OS_DARWIN || Q_OS_DARWIN64 m_layout = new QVBoxLayout(this); m_layout->setMargin(0); m_layout->setSpacing(0); m_viewer = new QDeclarativeView(this); m_viewer->viewport()->installEventFilter(this); m_layout->addWidget(m_viewer); m_viewer->setResizeMode(QDeclarativeView::SizeRootObjectToView); m_viewer->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); m_viewer->rootContext()->setContextProperty("window",this); m_viewer->rootContext()->setContextProperty("core",Core::instance()); QObject::connect(m_viewer->rootContext()->engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit())); m_viewer->setMouseTracking(true); #else m_viewer = NULL; d->setRoundRect(false); d->init(); #endif}