1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <QtGui>
#include "treemodel.h"
TreeModel * model = 0;
QTreeView * view = 0;
class QObject;
class QEvent;
class Application : public QApplication
{
private:
int m_timerId;
public:
Application(int argc, char * argv[]);
bool notify(QObject * receiver, QEvent * event);
void timerId(int value);
void timerEvent(QTimerEvent * event);
};
Application::Application(int argc, char * argv[])
: QApplication(argc, argv)
{
connect(this, SIGNAL(destroyed()), this, SLOT(quit()));
}
bool Application::notify(QObject * receiver, QEvent * event)
{
if (receiver == this)
{
if (event->type() == QEvent::Quit)
{
if (model)
delete model;
if (view)
delete view;
}
if (event->type() == QEvent::Timer)
killTimer(m_timerId);
}
return QApplication::notify(receiver, event);
}
void Application::timerId(int value)
{
m_timerId = value;
}
void Application::timerEvent(QTimerEvent * event)
{
if (view)
{
if (model)
delete model;
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
model = new TreeModel(file.readAll());
file.close();
view->setModel(model);
view->show();
}
}
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(simpletreemodel);
Application app(argc, argv);
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
model = new TreeModel(file.readAll());
file.close();
view = new QTreeView();
view->setWindowTitle(QObject::tr("Simple Tree Model"));
app.startTimer(100);
view->setModel(model);
view->show();
return app.exec();
} |
Partager