Results 1 to 7 of 7

Thread: what shoud i write in main() ?

  1. #1
    Join Date
    Nov 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Red face what shoud i write in main() ?

    I read qt book an want to write the example but after writing i don't know what should i write in main() function .

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QThread>
    4. #include <QDataStream>
    5. #include<QDialog>
    6. #include <QPushButton>
    7. #include <QCloseEvent>
    8. #include <iostream>
    9.  
    10.  
    11. class Thread :public QThread
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Thread();
    17. void setMessage(const QString &message);
    18. void stop();
    19.  
    20. protected:
    21. void run();
    22. private:
    23. QString messageStr;
    24. volatile bool stopped;
    25.  
    26. };
    27.  
    28. Thread::Thread()
    29. {
    30. stopped=false;
    31. }
    32. void Thread::run()
    33. {
    34. while (!stopped)
    35. std::cerr<<qPrintable(messageStr);
    36. stopped=false;
    37. std::cerr<<std::endl;
    38. }
    39. void Thread::stop()
    40. {
    41. stopped=true;
    42. }
    43.  
    44. class ThreadDialog:public QDialog
    45. {
    46. Q_OBJECT
    47. public:
    48. ThreadDialog(QWidget *parent=0);
    49. protected:
    50. void closeEvent(QCloseEvent *event);
    51.  
    52. private slots:
    53. void startOrStopThreadA();
    54. void startOrStopThreadB();
    55.  
    56. private:
    57. Thread threadA;
    58. Thread threadB;
    59. QPushButton *threadAButton;
    60. QPushButton *threadBButton;
    61. QPushButton *quitButton;
    62. };
    63. ThreadDialog::ThreadDialog(QWidget *parent)
    64. :QDialog(parent)
    65. {
    66. threadA.setMessage("A");
    67. threadB.setMessage("B");
    68.  
    69. threadAButton=new QPushButton(tr("Start A"));
    70. threadBButton=new QPushButton(tr("Start B"));
    71. quitButton=new QPushButton(tr("Quit"));
    72. quitButton->setDefault(true);
    73. connect (threadAButton,SIGNAL(clicked()),this,SLOT(startOrStopThreadA()));
    74. connect (threadBButton,SIGNAL(clicked()),this,SLOT(startOrStopThreadB()));
    75.  
    76.  
    77. }
    78.  
    79. void ThreadDialog::startOrStopThreadA()
    80. {
    81. if(threadA.isRunning()){
    82. threadA.stop();
    83. threadAButton->setText(tr("Start A"));
    84. }else{
    85. threadA.start();
    86. threadAButton->setText(tr("Stop A"));
    87. }
    88. }
    89.  
    90. void ThreadDialog::startOrStopThreadB()
    91. {
    92. if (threadB.isRunning()){
    93. threadB.stop();
    94. threadBButton->setText(tr("Start B"));
    95. }else{
    96. threadB.start();
    97. threadBButton->setText(tr("Stop B"));
    98. }
    99. }
    100. void ThreadDialog::closeEvent(QCloseEvent *event)
    101. {
    102. threadA.stop();
    103. threadB.stop();
    104. threadA.wait();
    105. threadB.wait();
    106. event->accept();
    107. }
    108.  
    109.  
    110. int main(int argc,char *argv[]){
    111. // what shoul i write here for using this program?
    112. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 17th November 2010 at 15:37. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: what shoud i write in main() ?

    Am... you should not start with threaded programming in C++ before you know basic C/C++.
    Then you could start playing with Qt, but not threaded.
    THEN learn what threaded programming is.
    And only then you will be able to code Qt threaded apps.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: what shoud i write in main() ?

    i know , but i should write this program.

    i want to know how to use this class in main .

    please help me

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: what shoud i write in main() ?

    Look at Qt examples.

    Particularly, where is your QApplication variable?

  5. #5
    Join Date
    Nov 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: what shoud i write in main() ?

    i am reading c++GUI Programing with Qt4 . in chapter 14 i read this .
    what should i do?
    which variable?

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: what shoud i write in main() ?

    Quote Originally Posted by nima View Post
    i am reading c++GUI Programing with Qt4 . in chapter 14 i read this .
    what should i do?
    which variable?
    Did you work through all the example programs in chapters 1 through 13?

    Seriously: if you don't understand what the main function does, or how to declare variables, you're skipping over way, way too much. You need to take several steps back and learn the basics first.

  7. #7
    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: what shoud i write in main() ?

    Chapter 14 of the book assumes that you have read the explanation on the very first page of Chapter 1 and can work it out for yourself. Chapter 2 has an example almost perfectly suited. There are numerous other examples in the book before Ch 14, each with a similar pattern just to reinforce the lesson.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. Replies: 11
    Last Post: 11th August 2008, 09:14
  3. TCP Write Raw data
    By ^NyAw^ in forum General Programming
    Replies: 19
    Last Post: 23rd November 2007, 16:38
  4. XML -read-write
    By hgedek in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2007, 18:10
  5. Replies: 15
    Last Post: 23rd March 2007, 16:16

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
  •  
Qt is a trademark of The Qt Company.