Advertisement
Guest User

Untitled

a guest
Mar 26th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. hello.c -------------------------------------------------------------------------
  2.  
  3. #include <iostream>
  4.  
  5. #include <Wt/WBreak>
  6. #include <Wt/WContainerWidget>
  7. #include <Wt/WLineEdit>
  8. #include <Wt/WPushButton>
  9. #include <Wt/WText>
  10.  
  11. #include "HelloApplication.h"
  12. #include "QtObject.h"
  13.  
  14. // Needed when using WQApplication with Qt eventloop = true
  15. #include <QCoreApplication>
  16.  
  17. using namespace Wt;
  18.  
  19. HelloApplication::HelloApplication(const WEnvironment& env)
  20.   : WQApplication(env, true)
  21. {
  22.   /*
  23.    * Note: do not create any Qt objects from here. Initialize your
  24.    * application from within the virtual create() method.
  25.    */
  26. }
  27.  
  28. void HelloApplication::create()
  29. {
  30.   setTitle("Hello world");
  31.  
  32.   root()->addWidget(new WText("Your name, please ? "));
  33.   nameEdit_ = new WLineEdit(root());
  34.   nameEdit_->setFocus();
  35.  
  36.   WPushButton *b = new WPushButton("Greet me.", root());
  37.   b->setMargin(5, Left);
  38.  
  39.   root()->addWidget(new WBreak());
  40.  
  41.   greeting_ = new WText(root());
  42.  
  43.   b->clicked().connect(this, &HelloApplication::propagateGreet);
  44.   nameEdit_->enterPressed().connect(this, &HelloApplication::propagateGreet);
  45.  
  46.   qtSender_ = new QtObject(this);
  47.   qtReceiver_ = new QtObject(this);
  48.  
  49.   QObject::connect(qtSender_, SIGNAL(greet(const QString&)),
  50.            qtReceiver_, SLOT(doGreet(const QString&)));
  51. }
  52.  
  53. void HelloApplication::destroy()
  54. {
  55.   /*
  56.    * Note: Delete any Qt object from here.
  57.    */
  58.   delete qtSender_;
  59.   delete qtReceiver_;
  60. }
  61.  
  62. void HelloApplication::propagateGreet()
  63. {
  64.   qtSender_->passGreet(toQString(nameEdit_->text()));
  65. }
  66.  
  67. void HelloApplication::doGreet(const QString& qname)
  68. {
  69.   greeting_->setText("Hello there, " + toWString(qname));
  70. }
  71.  
  72. WApplication *createApplication(const WEnvironment& env)
  73. {
  74.   return new HelloApplication(env);
  75. }
  76.  
  77. int main(int argc, char **argv)
  78. {
  79.   // Needed for Qt's eventloop threads to work
  80.   QCoreApplication app(argc, argv);
  81.  
  82.   return WRun(argc, argv, &createApplication);
  83. }
  84.  
  85. helloapplication.h -------------------------------------------------------------------------
  86.  
  87. #ifndef HELLO_APPLICATION_H_
  88. #define HELLO_APPLICATION_H_
  89.  
  90. #include "WQApplication"
  91.  
  92. class QtObject;
  93. class QString;
  94.  
  95. using namespace Wt;
  96.  
  97. namespace Wt {
  98.   class WLineEdit;
  99.   class WText;
  100. }
  101.  
  102. /*! \class HelloApplication
  103.  *  \brief The 'hello' application modified to use QtCore
  104.  *
  105.  * A sample application that uses objects from the QtCore library.
  106.  */
  107. class HelloApplication : public WQApplication
  108. {
  109. public:
  110.   HelloApplication(const WEnvironment& env);
  111.  
  112.   void doGreet(const QString&);
  113.  
  114.   virtual void create();
  115.   virtual void destroy();
  116.  
  117. private:
  118.   WLineEdit *nameEdit_;
  119.   WText     *greeting_;
  120.  
  121.   QtObject  *qtSender_, *qtReceiver_;
  122.  
  123.   void propagateGreet();
  124. };
  125.  
  126. #endif // HELLO_APPLICATION_H_
  127.  
  128. qtobject.h -------------------------------------------------------------------------
  129.  
  130. #ifndef QTOBJECT_H_
  131. #define QTOBJECT_H_
  132.  
  133. #include <QThread>
  134.  
  135. class HelloApplication;
  136.  
  137. class QtObject : public QObject
  138. {
  139.   Q_OBJECT
  140.  
  141. public:
  142.   QtObject(HelloApplication *wt_, QObject *parent = 0);
  143.  
  144.   void passGreet(const QString&);
  145.  
  146. Q_SIGNALS:
  147.   void greet(const QString&);
  148.  
  149. public Q_SLOTS:
  150.   void doGreet(const QString&);
  151.   void doShit();
  152.  
  153. private:
  154.   HelloApplication *wt_;
  155. };
  156.  
  157. #endif // QTOBJECT_H_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement