Results 1 to 4 of 4

Thread: signals not work

  1. #1
    Join Date
    Jan 2025
    Posts
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default signals not work

    Hi, I'm trying to create a signal, but I can't see the code. I'm trying to prevent reading and writing to a file at the same time with QSetting. Is there perhaps another way? Thanks.

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. #include "parametres.h"
    7. #include "threadsondes.h"
    8. #include "gainable.h"
    9. #include "affichecons.h"
    10.  
    11. class MainWindow: public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = nullptr);
    17.  
    18. signals:
    19. void lectureEnCours(bool m_lecture);
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. :QWidget (parent)
    5. {
    6. connect(this, &MainWindow::lectureEnCours, &m_mainwindowThreadSondes, &ThreadSondes::onLitEnCours);
    7.  
    8. m_timer1 = new QTimer(this);
    9. m_timer1 ->start(m_timerTemps);
    10. connect(m_timer1, &QTimer::timeout, this, &MainWindow::lectureTemperatures);
    11. }
    12.  
    13. void MainWindow::lectureTemperatures()
    14. {
    15. qDebug() << "lectureTemperature() MainWindow";
    16.  
    17. emit lectureEnCours(true);
    18. }
    To copy to clipboard, switch view to plain text mode 

    threadsondes.h

    Qt Code:
    1. #ifndef THREADSONDES_H
    2. #define THREADSONDES_H
    3.  
    4. #include <QThread>
    5.  
    6. #include <QObject>
    7.  
    8. #include "sondes.h"
    9.  
    10. class ThreadSondes: public QThread
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. ThreadSondes();
    16.  
    17. bool lecture = false;
    18.  
    19. void lectureTemperaturesEnCours();
    20.  
    21. public slots:
    22. void onLitEnCours(bool m_lecture);
    23.  
    24. signals:
    25.  
    26. private:
    27. DS18b20 m_threadSondesDS18b20;
    28.  
    29. void run();
    30.  
    31. };
    32.  
    33. threadsondes.cpp
    34.  
    35. [CODE]
    36. #include "threadsondes.h"
    37.  
    38. #include <QDebug>
    39.  
    40. ThreadSondes::ThreadSondes()
    41. {
    42. qDebug() << "ThreadSondes";
    43. }
    44.  
    45. void ThreadSondes::onLitEnCours(bool m_lecture)
    46. {
    47. lecture = m_lecture;
    48. }
    49.  
    50. void ThreadSondes::lectureTemperaturesEnCours()
    51. {
    52. if (lecture == true) {
    53. qDebug() << "lecture = " << lecture;
    54. } else {
    55. qDebug() << "lecture = " << lecture;
    56. }
    57. }
    58.  
    59. void ThreadSondes::run()
    60. {
    61. while (1) {
    62.  
    63. qDebug() << "run ThreadSondes()";
    64.  
    65. lectureTemperaturesEnCours();
    66.  
    67. /*m_threadSondesDS18b20.lectureSondeExt(); // on ecrite dans parametres en meme temps
    68. m_threadSondesDS18b20.lectureSondeUnitExt();
    69. m_threadSondesDS18b20.lectureSondeEcExt();
    70. m_threadSondesDS18b20.lectureSondeUnitInt();
    71. m_threadSondesDS18b20.lectureSondeEcInt();
    72.  
    73. sleep(5);*/
    74. }
    75. }
    To copy to clipboard, switch view to plain text mode 
    [/CODE]

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,311
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: signals not work

    I'm trying to prevent reading and writing to a file at the same time with QSetting. Is there perhaps another way?

    You should use a QMutex for this.

    Qt Code:
    1. class SomeQtWidget : public QWidget
    2. {
    3. Q_OBJECT;
    4.  
    5. public:
    6.  
    7. // ...
    8.  
    9. void WriteSettings()
    10. {
    11. mutex.lock();
    12.  
    13. // write QSettings
    14.  
    15. mutex.unlock();
    16. }
    17.  
    18. void ReadSettings()
    19. {
    20. mutex.lock()
    21.  
    22. // read QSettings
    23.  
    24. mutex.unlock();
    25. }
    26.  
    27. private:
    28. QMutex mutex;
    29.  
    30. };
    To copy to clipboard, switch view to plain text mode 

    Also in your Thread class, the run() method is an infinite loop that never gives the Qt event loop the opportunity to execute. So it will completely block any signals or slots in the thread from executing. As I explained in earlier posts, you need to call the processEvents() method in your thread classes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2025
    Posts
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: signals not work

    Hi, I'm starting from scratch with a thread without run();
    I can't get my signal to work anymore; it's always true!

    And if you have any ideas for organizing my code, see my other posts.

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4. #include <QDebug>
    5.  
    6. #include "consignes.h"
    7. #include "threadsondes.h"
    8.  
    9. Consignes *m_initConsignes;
    10. ThreadSondes m_mainThreadSondes;
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. qDebug() << "main";
    15.  
    16. QApplication app(argc, argv);
    17. QApplication::setOverrideCursor(QCursor(Qt::BlankCursor)); // cache pointeur Sourie
    18.  
    19. m_initConsignes = new Consignes(QString("settings/consignes.ini"), QSettings::IniFormat);
    20. m_initConsignes ->controleConsignes();
    21.  
    22. QThread* m_thread = new QThread();
    23. m_mainThreadSondes.moveToThread(m_thread);
    24. QObject::connect(m_thread, &QThread::started, &m_mainThreadSondes, &ThreadSondes::process);
    25. m_thread ->start();
    26.  
    27. MainWindow window;
    28.  
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. #include "temperatures.h"
    7. #include "threadsondes.h"
    8. #include "affichecons.h"
    9.  
    10. class MainWindow: public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MainWindow(QWidget *parent = nullptr);
    16.  
    17. QTimer *m_timer1;
    18.  
    19. int m_timerTemps = 10000;
    20.  
    21. public slots:
    22. void changeLabel(int m_mode);
    23.  
    24. signals:
    25. void changeGainableState(bool m_state);
    26.  
    27. private:
    28. Temperatures *m_mainwindowTemperatures;
    29.  
    30. ThreadSondes m_mainwindowThreadSondes;
    31.  
    32. AfficheCons *m_afficheCons;
    33.  
    34. QWidget *m_window;
    35.  
    36. QLabel *m_label;
    37.  
    38. QLCDNumber *m_tempExtLue;
    39. QLCDNumber *m_tempUnitExtLue;
    40. QLCDNumber *m_tempEcExtLue;
    41. QLCDNumber *m_tempUnitIntLue;
    42. QLCDNumber *m_tempEcIntLue;
    43.  
    44. QGroupBox *m_disp1; //tempExt
    45. QGroupBox *m_disp2; //tempUnitExt
    46. QGroupBox *m_disp3; //tempEcExt
    47. QGroupBox *m_disp4; //tempUnitInt
    48. QGroupBox *m_disp5; //tempEcInt
    49.  
    50. QPushButton *m_marche;
    51. QPushButton *m_consigne;
    52. QPushButton *m_stop;
    53.  
    54. void affichageTemperatures();
    55.  
    56. void afficheConsignes();
    57. void stopGainable();
    58. void reStartGainable();
    59. };
    60. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. :QWidget (parent)
    5. {
    6. qDebug() << "Mainwindow";
    7.  
    8. m_mainwindowTemperatures = new Temperatures(QString("settings/temperatures.ini"), QSettings::IniFormat);
    9.  
    10. m_window = new QWidget;
    11. m_window ->setFixedSize(1920,1080);
    12. m_window ->setWindowTitle("Gainable");
    13. adjustSize();
    14.  
    15. m_label = new QLabel(m_window);
    16.  
    17. m_disp1 = new QGroupBox("Temp°C Ext",m_window);
    18. m_disp1 ->setStyleSheet("QGroupBox:title {color: orangered;}");
    19. m_disp1 ->setFont(QFont("Times", 18, QFont::Bold));
    20. m_disp1 ->setGeometry(200,40,240,120);
    21. m_tempExtLue = new QLCDNumber(m_disp1);
    22. m_tempExtLue ->setGeometry(0,35,240,80);
    23.  
    24. m_disp2 = new QGroupBox("Temp°C UnitExt",m_window);
    25. m_disp2 ->setStyleSheet("QGroupBox:title {color: orangered;}");
    26. m_disp2 ->setFont(QFont("Times", 18, QFont::Bold));
    27. m_disp2 ->setGeometry(520,40,240,120);
    28. m_tempUnitExtLue = new QLCDNumber(m_disp2);
    29. m_tempUnitExtLue ->setGeometry(0,35,240,80);
    30.  
    31. m_disp3 = new QGroupBox("Temp°C EcUnitExt",m_window);
    32. m_disp3 ->setStyleSheet("QGroupBox:title {color: orangered;}");
    33. m_disp3 ->setFont(QFont("Times", 18, QFont::Bold));
    34. m_disp3 ->setGeometry(840,40,240,120);
    35. m_tempEcExtLue = new QLCDNumber(m_disp3);
    36. m_tempEcExtLue ->setGeometry(0,35,240,80);
    37.  
    38. m_disp4 = new QGroupBox("Temp°C UnitInt",m_window);
    39. m_disp4 ->setStyleSheet("QGroupBox:title {color: orangered;}");
    40. m_disp4 ->setFont(QFont("Times", 18, QFont::Bold));
    41. m_disp4 ->setGeometry(1160,40,240,120);
    42. m_tempUnitIntLue = new QLCDNumber(m_disp4);
    43. m_tempUnitIntLue ->setGeometry(0,35,240,80);
    44.  
    45. m_disp5 = new QGroupBox("Temp°C EcUnitInt",m_window);
    46. m_disp5 ->setStyleSheet("QGroupBox:title {color: orangered;}");
    47. m_disp5 ->setFont(QFont("Times", 18, QFont::Bold));
    48. m_disp5 ->setGeometry(1480,40,240,120);
    49. m_tempEcIntLue = new QLCDNumber(m_disp5);
    50. m_tempEcIntLue ->setGeometry(0,35,240,80);
    51.  
    52. affichageTemperatures();
    53.  
    54. m_marche = new QPushButton("Démarrage ??",m_window);
    55. m_marche ->setGeometry(1080,900,240,95);
    56. m_marche ->setStyleSheet("font-size: 30px;background-color: lime");
    57. m_marche ->hide();
    58.  
    59. m_consigne = new QPushButton("Consignes",m_window);
    60. m_consigne ->setGeometry(1340,900,240,95);
    61. m_consigne ->setStyleSheet("font-size: 30px;");
    62. m_consigne ->show();
    63.  
    64. m_stop = new QPushButton("Stop ??",m_window);
    65. m_stop ->setGeometry(1600,900,240,95);
    66. m_stop ->setStyleSheet("font-size: 30px;background-color: red");
    67. m_stop ->show();
    68.  
    69. m_timer1 = new QTimer(this);
    70. m_timer1 ->start(m_timerTemps);
    71. connect(m_timer1, &QTimer::timeout, this, &MainWindow::affichageTemperatures); // affiche temperature toute les 10 secondes
    72.  
    73. changeLabel(3);
    74.  
    75. connect(m_marche, &QPushButton::clicked, this, &MainWindow::reStartGainable);
    76. connect(m_consigne, &QPushButton::clicked, this, &MainWindow::afficheConsignes);
    77. connect(m_stop, &QPushButton::clicked, this, &MainWindow::stopGainable);
    78.  
    79. connect(this, &MainWindow::changeGainableState, &m_mainwindowThreadSondes, &ThreadSondes::onChangeGainableState);
    80.  
    81. m_window->show();
    82. }
    83.  
    84. void MainWindow::affichageTemperatures()
    85. {
    86. m_mainwindowTemperatures ->lireToutesTemperatures();
    87.  
    88. m_tempExtLue ->display(m_mainwindowTemperatures ->temperatureExt);
    89. m_tempUnitExtLue ->display(m_mainwindowTemperatures ->temperatureUnitExt);
    90. m_tempEcExtLue ->display(m_mainwindowTemperatures ->temperatureEcExt);
    91. m_tempUnitIntLue ->display(m_mainwindowTemperatures ->temperatureUnitInt);
    92. m_tempEcIntLue ->display(m_mainwindowTemperatures ->temperatureEcInt);
    93. }
    94.  
    95. void MainWindow::changeLabel(int m_mode)
    96. {
    97. switch (m_mode) {
    98.  
    99. case 0:
    100.  
    101. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/froid.jpg"));
    102.  
    103. break;
    104.  
    105. case 1:
    106.  
    107. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/chauffage.jpg"));
    108.  
    109. break;
    110.  
    111. case 2:
    112.  
    113. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/canicule.jpg"));
    114.  
    115. break;
    116.  
    117. default:
    118.  
    119. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/abigael.jpg"));
    120.  
    121. break;
    122.  
    123. }
    124. }
    125.  
    126. void MainWindow::afficheConsignes()
    127. {
    128. qDebug() << "afficheConsignes()";
    129. m_afficheCons = new AfficheCons();
    130. m_afficheCons ->readCons();
    131. }
    132.  
    133. void MainWindow::stopGainable()
    134. {
    135. qDebug() << "stopGainable()";
    136. m_stop ->hide();
    137. m_marche ->show();
    138. emit changeGainableState(false);
    139. }
    140.  
    141. void MainWindow::reStartGainable()
    142. {
    143. qDebug() << "restartGainable()";
    144. m_marche ->hide();
    145. m_stop ->show();
    146. emit changeGainableState(true);
    147. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef THREADSONDES_H
    2. #define THREADSONDES_H
    3.  
    4. #include "temperatures.h"
    5.  
    6. class ThreadSondes: public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ThreadSondes();
    12.  
    13. ~ThreadSondes();
    14.  
    15. public slots:
    16. void process();
    17.  
    18. void onChangeGainableState(bool m_state);
    19.  
    20. signals:
    21.  
    22. private:
    23. Temperatures *m_threadSondesTemperatures;
    24.  
    25. bool departGainable = true;
    26.  
    27. float tempoEcritureTemperatures = 15;
    28. unsigned long long departTempoEcritureTemperatures;
    29.  
    30. void ecritToutesTemperaturesLue();
    31. };
    32.  
    33. #endif //THREADSONDES
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "threadsondes.h"
    2.  
    3. #include <QDebug>
    4.  
    5. #include <ctime>
    6.  
    7. ThreadSondes::ThreadSondes()
    8. {
    9. qDebug() << "ThreadSondes";
    10.  
    11. m_threadSondesTemperatures = new Temperatures(QString("settings/temperatures.ini"), QSettings::IniFormat);
    12. }
    13.  
    14. ThreadSondes::~ThreadSondes()
    15. {
    16.  
    17. }
    18.  
    19. void ThreadSondes::onChangeGainableState(bool m_state)
    20. {
    21. departGainable = m_state;
    22. }
    23.  
    24. void ThreadSondes::process()
    25. {
    26. qDebug() << "process()";
    27.  
    28. while(1) {
    29.  
    30. qDebug() << departGainable;
    31.  
    32. if (time(NULL) - departTempoEcritureTemperatures >= tempoEcritureTemperatures) {
    33. m_threadSondesTemperatures ->ecritToutesTemperatures();
    34. departTempoEcritureTemperatures = time(NULL);
    35. }
    36. }
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signals not work

    Now you block the Qt queue in the process method (while(1) loop). Use Qtimer to run the process method periodically.

Similar Threads

  1. Signals and Slots only don't work
    By QwQ in forum Newbie
    Replies: 1
    Last Post: 14th February 2020, 17:08
  2. Signals don't work
    By Calypoter in forum Qt Programming
    Replies: 10
    Last Post: 8th January 2012, 08:20
  3. after subclassing will the inbuild signals work
    By babu198649 in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2007, 15:10
  4. QHttp signals don't work !!
    By probine in forum Qt Programming
    Replies: 11
    Last Post: 11th January 2007, 15:02

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.