Results 1 to 8 of 8

Thread: There is a problem with this code

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem with this code

    Hello
    Why not run the code below?
    thanks
    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class LightWidget : public QWidget
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(bool on READ isOn WRITE setOn)
    7. public:
    8. LightWidget(const QColor &color, QWidget *parent = 0)
    9. : QWidget(parent), m_color(color), m_on(false) {}
    10.  
    11. bool isOn() const
    12. { return m_on; }
    13. void setOn(bool on)
    14. {
    15. if (on == m_on)
    16. return;
    17. m_on = on;
    18. update();
    19. }
    20.  
    21. public slots:
    22. void turnOff() { setOn(false); }
    23. void turnOn() { setOn(true); }
    24.  
    25. protected:
    26. virtual void paintEvent(QPaintEvent *)
    27. {
    28. if (!m_on)
    29. return;
    30. QPainter painter(this);
    31. painter.setRenderHint(QPainter::Antialiasing);
    32. painter.setBrush(m_color);
    33. painter.drawEllipse(0, 0, width(), height());
    34. }
    35.  
    36. private:
    37. QColor m_color;
    38. bool m_on;
    39. };
    40.  
    41. class TrafficLightWidget : public QWidget
    42. {
    43. public:
    44. TrafficLightWidget(QWidget *parent = 0)
    45. : QWidget(parent)
    46. {
    47. QVBoxLayout *vbox = new QVBoxLayout(this);
    48. m_red = new LightWidget(Qt::red);
    49. vbox->addWidget(m_red);
    50. m_yellow = new LightWidget(Qt::yellow);
    51. vbox->addWidget(m_yellow);
    52. m_green = new LightWidget(Qt::green);
    53. vbox->addWidget(m_green);
    54. QPalette pal = palette();
    55. pal.setColor(QPalette::Background, Qt::black);
    56. setPalette(pal);
    57. setAutoFillBackground(true);
    58. }
    59.  
    60. LightWidget *redLight() const
    61. { return m_red; }
    62. LightWidget *yellowLight() const
    63. { return m_yellow; }
    64. LightWidget *greenLight() const
    65. { return m_green; }
    66.  
    67. private:
    68. LightWidget *m_red;
    69. LightWidget *m_yellow;
    70. LightWidget *m_green;
    71. };
    72.  
    73. QState *createLightState(LightWidget *light, int duration, QState *parent = 0)
    74. {
    75. QState *lightState = new QState(parent);
    76. QTimer *timer = new QTimer(lightState);
    77. timer->setInterval(duration);
    78. timer->setSingleShot(true);
    79. QState *timing = new QState(lightState);
    80. QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
    81. QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
    82. QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
    83. QFinalState *done = new QFinalState(lightState);
    84. timing->addTransition(timer, SIGNAL(timeout()), done);
    85. lightState->setInitialState(timing);
    86. return lightState;
    87. }
    88.  
    89. class TrafficLight : public QWidget
    90. {
    91. public:
    92. TrafficLight(QWidget *parent = 0)
    93. : QWidget(parent)
    94. {
    95. QVBoxLayout *vbox = new QVBoxLayout(this);
    96. TrafficLightWidget *widget = new TrafficLightWidget();
    97. vbox->addWidget(widget);
    98. vbox->setMargin(0);
    99.  
    100. QStateMachine *machine = new QStateMachine(this);
    101. QState *redGoingYellow = createLightState(widget->redLight(), 3000);
    102. redGoingYellow->setObjectName("redGoingYellow");
    103. QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000);
    104. yellowGoingGreen->setObjectName("yellowGoingGreen");
    105. redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen);
    106. QState *greenGoingYellow = createLightState(widget->greenLight(), 3000);
    107. greenGoingYellow->setObjectName("greenGoingYellow");
    108. yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow);
    109. QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000);
    110. yellowGoingRed->setObjectName("yellowGoingRed");
    111. greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed);
    112. yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow);
    113.  
    114. machine->addState(redGoingYellow);
    115. machine->addState(yellowGoingGreen);
    116. machine->addState(greenGoingYellow);
    117. machine->addState(yellowGoingRed);
    118. machine->setInitialState(redGoingYellow);
    119. machine->start();
    120. }
    121. };
    122.  
    123. int main(int argc, char **argv)
    124. {
    125. QApplication app(argc, argv);
    126.  
    127. TrafficLight widget;
    128. widget.resize(110, 300);
    129. widget.show();
    130.  
    131. return app.exec();
    132. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rezas1000; 30th August 2014 at 19:59.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: There is a problem with this code

    What does "not run" about it? You have not provided a description of the problem.

  3. The following user says thank you to ChrisW67 for this useful post:

    rezas1000 (30th August 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem with this code

    Photo of Code:

    Untitled.jpg

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: There is a problem with this code

    Did you run qmake after adding the Q_OBJECT macro to the class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    rezas1000 (31st August 2014)

  7. #5
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem with this code

    Quote Originally Posted by wysota View Post
    Did you run qmake after adding the Q_OBJECT macro to the class?
    Yes.it is.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: There is a problem with this code

    Is this a cpp or a header file?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    rezas1000 (31st August 2014)

  10. #7
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: There is a problem with this code

    thank you.
    it is a cpp file.

  11. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: There is a problem with this code

    Then you have to include the generate moc, something lke
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    at the end of the file and re-run qmake.

    Better would be to put alll the class declarations into a header file.

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    rezas1000 (31st August 2014)

Similar Threads

  1. There is a problem in the code
    By rezas1000 in forum Newbie
    Replies: 8
    Last Post: 25th August 2014, 14:01
  2. Problem with Qnetwork cookies and getting code after login
    By DomantasB in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2013, 10:21
  3. uic generate code problem.
    By jerry7 in forum Newbie
    Replies: 1
    Last Post: 21st March 2011, 07:59
  4. Problem with code (probably repaintinng label)
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 8th January 2011, 13:58
  5. Problem compiling Qt 4.7.0 code with d3d10.h
    By pogo11 in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2010, 22:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.