Results 1 to 2 of 2

Thread: Signals and Slots only don't work

  1. #1
    Join Date
    Feb 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Question Signals and Slots only don't work

    Hi.
    I am doing my homework about Temperature Converter.
    kWANobd7DcKYUQx.jpg
    The problem is the "Celsius" and "Fahrenheit" can't be synchronous.
    Qt Code:
    1. #include "dialog.h"
    2. #include <QBoxLayout>
    3.  
    4. Dialog::Dialog(int tempCelsius, QWidget *parent)
    5. : QDialog(parent)
    6. {
    7. celsiusGroupBox = new QGroupBox(this);
    8. fahrenheitGroupBox = new QGroupBox(this);
    9. celsiusDia = new QDial(celsiusGroupBox);
    10. fahrenheitDia = new QDial(fahrenheitGroupBox);
    11. celsiusLDNumber = new QLCDNumber(celsiusGroupBox);
    12. fahrenheitLDNumber = new QLCDNumber(fahrenheitGroupBox);
    13. celsiusGroupBox->setTitle("Celsius");
    14. fahrenheitGroupBox->setTitle("Fahrenheit");
    15. leftLayout->addWidget(celsiusDia);
    16. leftLayout->addWidget(celsiusLDNumber);
    17. rightLayout->addWidget(fahrenheitDia);
    18. rightLayout->addWidget(fahrenheitLDNumber);
    19. connect(celsiusDia, SIGNAL(valueChanged(int)), this, SLOT(setTempCelsius()));
    20. connect(celsiusDia, SIGNAL(valueChanged(int)), celsiusLDNumber, SLOT(display(int)));
    21. connect(this, SIGNAL(tempCelsiusChanged(int)), celsiusDia, SLOT(setValue(int)));
    22. connect(fahrenheitDia, SIGNAL(valueChanged(int)), this, SLOT(setTempFahrenheit()));
    23. connect(fahrenheitDia, SIGNAL(valueChanged(int)), fahrenheitLDNumber, SLOT(display(int)));
    24. connect(this, SIGNAL(tempFahrenheitChanged(int)), fahrenheitDia, SLOT(setValue(int)));
    25. m_tempCelsius = 0;
    26. setTempCelsius(tempCelsius);
    27. }
    28.  
    29. int Dialog::tempCelsius() const
    30. {
    31. return m_tempCelsius;
    32. }
    33.  
    34. int Dialog::tempFahrenheit() const
    35. {
    36. return m_tempCelsius*(9.0/5.0)+32;
    37. }
    38.  
    39. void Dialog::setTempCelsius(int tempCelsius)
    40. {
    41. if (m_tempCelsius==tempCelsius)
    42. return;
    43.  
    44. m_tempCelsius = tempCelsius;
    45. emit tempCelsiusChanged(m_tempCelsius);
    46. emit tempFahrenheitChanged(tempFahrenheit());
    47. }
    48.  
    49. void Dialog::setTempFahrenheit(int tempFahrenheit)
    50. {
    51. int tempCelsius = (tempFahrenheit - 32) * (5.0/9.0);
    52. setTempCelsius(tempCelsius);
    53. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots only don't work

    I'm trying my best to help, but I'm not fully sure I understand the question.
    I can see one problem is a disconnect between some of your signal methods and slot methods.

    The problem areas are lines 19 and 22.
    Your slot methods need to have the 'int' parameter to match the signal methods.

    Qt Code:
    1. connect(celsiusDia, SIGNAL(valueChanged(int)), this, SLOT(setTempCelsius(int))); // Notice the 'int' param
    2. connect(fahrenheitDia, SIGNAL(valueChanged(int)), this, SLOT(setTempFahrenheit(int))); // and again
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 13:15
  2. Replies: 15
    Last Post: 13th June 2010, 11:04
  3. Problem Getting User-Defined Signals/Slots to Work
    By Radiotelephone in forum Newbie
    Replies: 2
    Last Post: 3rd August 2009, 08:14
  4. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 10:32
  5. help with signals and slots
    By superutsav in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2006, 13:49

Tags for this Thread

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.