Results 1 to 3 of 3

Thread: using two threads

  1. #1
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default using two threads

    Hey guys,
    I have a gui that outputs a sine wave and square wave with user input frequency and amplitude. My code to produce both the square and sine wave is tested and works successfully.
    My problem is: I have the sine wave working. I used threads to start and eliminate(from click of pushButtons) the generation of the sine wave. I am trying to do the same for the square wave, but when I click the pushButton, it generates the sine wave again.
    I am not sure where I am going wrong. Any help would be appreciated. Code snippets below:

    wave.cpp:
    Qt Code:
    1. wave::wave(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::wave)
    4. {
    5. ui->setupUi(this);
    6. setup();
    7. thread = new MyThread();
    8. thread2 = new MyThread();
    9.  
    10.  
    11. }
    12. wave::~wave()
    13. {
    14. delete ui;
    15. delete thread;
    16. delete thread2;
    17. }
    18.  
    19. void wave::on_pushButton_clicked()
    20. {
    21. //Generate
    22. freq = ui->frequency->text().toDouble();
    23. ampl = ui->amplitude->text().toDouble();
    24. thread->start();
    25. ui->pushButton->setEnabled(false);
    26. ui->pushButton->setStyleSheet("background-color: gray");
    27. ui->pushButton_2->setStyleSheet("background-color: rgb(255, 192, 192);"
    28. "color: red;");
    29. ui->pushButton_2->setEnabled(true);
    30. }
    31.  
    32. void wave::on_pushButton_2_clicked()
    33. {
    34. //Terminate
    35. thread->terminate();
    36. ui->pushButton_2->setEnabled(false);
    37. ui->pushButton_2->setStyleSheet("background-color: gray");
    38. ui->pushButton->setStyleSheet("background-color: rgb(192, 255, 208);"
    39. "color: green;");
    40. ui->pushButton->setEnabled(true);
    41.  
    42. }
    43.  
    44.  
    45. void wave::on_pushButton_3_clicked()
    46. {
    47. freq = ui->frequency->text().toDouble();
    48. ampl = ui->amplitude->text().toDouble();
    49. thread2->start();
    50. ui->pushButton_3->setEnabled(false);
    51. ui->pushButton_3->setStyleSheet("background-color: gray");
    52. ui->pushButton_4->setStyleSheet("background-color: rgb(255, 192, 192);"
    53. "color: red;");
    54. ui->pushButton_4->setEnabled(true);
    55. }
    56.  
    57. void wave::on_pushButton_4_clicked()
    58. {
    59. thread2->terminate();
    60. ui->pushButton_4->setEnabled(false);
    61. ui->pushButton_4->setStyleSheet("background-color: gray");
    62. ui->pushButton_3->setStyleSheet("background-color: rgb(192, 255, 208);"
    63. "color: green;");
    64. ui->pushButton_3->setEnabled(true);
    65. }
    To copy to clipboard, switch view to plain text mode 

    wave.h:
    Qt Code:
    1. #ifndef WAVE_H
    2. #define WAVE_H
    3. #include "ui_wave.h"
    4. #include <alsa/asoundlib.h>
    5. #include <QMainWindow>
    6. #include <QObject>
    7. #include "mythread.h"
    8.  
    9. namespace Ui {
    10. class wave;
    11. }
    12.  
    13. class wave : public QMainWindow
    14. {
    15. Q_OBJECT
    16. MyThread *thread;
    17. MyThread *thread2;
    18.  
    19. public:
    20. explicit wave(QWidget *parent = 0);
    21. ~wave();
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25. void on_pushButton_2_clicked();
    26.  
    27. void on_pushButton_3_clicked();
    28.  
    29. void on_pushButton_4_clicked();
    30.  
    31. private:
    32. Ui::wave *ui;
    33.  
    34. };
    35.  
    36. void onWriteLoopSine();
    37. void onWriteLoopSquare();
    38.  
    39. #endif // WAVE_H
    To copy to clipboard, switch view to plain text mode 


    mythread.h:
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5.  
    6. class MyThread : public QThread
    7. {
    8. Q_OBJECT
    9.  
    10.  
    11. protected:
    12. void run();
    13.  
    14.  
    15. public:
    16. explicit MyThread(QObject *parent = 0);
    17.  
    18. };
    19.  
    20.  
    21.  
    22. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp:
    Qt Code:
    1. #include "wave.h"
    2. #include "mythread.h"
    3.  
    4.  
    5. MyThread::MyThread(QObject *parent) :
    6. QThread(parent)
    7. {
    8. }
    9.  
    10.  
    11. void MyThread::run()
    12. {
    13. onWriteLoopSine();
    14. onWriteLoopSquare();
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: using two threads

    What's the contents of onWriteLoopSine() and onWriteLoopSquare() and why are you calling them one after the other? I thought you had two separate threads generating one wave each.
    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.


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

    duma (18th August 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: using two threads

    Quote Originally Posted by wysota View Post
    What's the contents of onWriteLoopSine() and onWriteLoopSquare() and why are you calling them one after the other? I thought you had two separate threads generating one wave each.
    Yes, thanks. I used booleans to call them seperately.

Similar Threads

  1. Non GUI threads
    By raghavendraningoji in forum Qt Programming
    Replies: 7
    Last Post: 22nd July 2011, 14:43
  2. Qt Threads vs Native Threads performance
    By StackOverflow in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2010, 12:14
  3. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  4. Qt threads
    By ^Nisok^ in forum Newbie
    Replies: 12
    Last Post: 22nd April 2009, 14:35
  5. Threads...
    By Abc in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2008, 17:35

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.