Results 1 to 5 of 5

Thread: signals not work

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2025
    Posts
    26
    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,315
    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.

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.