PDA

View Full Version : Signals and Slots only don't work



QwQ
4th February 2020, 15:28
Hi.
I am doing my homework about Temperature Converter.
13332
The problem is the "Celsius" and "Fahrenheit" can't be synchronous.


#include "dialog.h"
#include <QBoxLayout>

Dialog::Dialog(int tempCelsius, QWidget *parent)
: QDialog(parent)
{
celsiusGroupBox = new QGroupBox(this);
fahrenheitGroupBox = new QGroupBox(this);
celsiusDia = new QDial(celsiusGroupBox);
fahrenheitDia = new QDial(fahrenheitGroupBox);
celsiusLDNumber = new QLCDNumber(celsiusGroupBox);
fahrenheitLDNumber = new QLCDNumber(fahrenheitGroupBox);
celsiusGroupBox->setTitle("Celsius");
fahrenheitGroupBox->setTitle("Fahrenheit");
leftLayout->addWidget(celsiusDia);
leftLayout->addWidget(celsiusLDNumber);
rightLayout->addWidget(fahrenheitDia);
rightLayout->addWidget(fahrenheitLDNumber);
connect(celsiusDia, SIGNAL(valueChanged(int)), this, SLOT(setTempCelsius()));
connect(celsiusDia, SIGNAL(valueChanged(int)), celsiusLDNumber, SLOT(display(int)));
connect(this, SIGNAL(tempCelsiusChanged(int)), celsiusDia, SLOT(setValue(int)));
connect(fahrenheitDia, SIGNAL(valueChanged(int)), this, SLOT(setTempFahrenheit()));
connect(fahrenheitDia, SIGNAL(valueChanged(int)), fahrenheitLDNumber, SLOT(display(int)));
connect(this, SIGNAL(tempFahrenheitChanged(int)), fahrenheitDia, SLOT(setValue(int)));
m_tempCelsius = 0;
setTempCelsius(tempCelsius);
}

int Dialog::tempCelsius() const
{
return m_tempCelsius;
}

int Dialog::tempFahrenheit() const
{
return m_tempCelsius*(9.0/5.0)+32;
}

void Dialog::setTempCelsius(int tempCelsius)
{
if (m_tempCelsius==tempCelsius)
return;

m_tempCelsius = tempCelsius;
emit tempCelsiusChanged(m_tempCelsius);
emit tempFahrenheitChanged(tempFahrenheit());
}

void Dialog::setTempFahrenheit(int tempFahrenheit)
{
int tempCelsius = (tempFahrenheit - 32) * (5.0/9.0);
setTempCelsius(tempCelsius);
}

Maxfooo
14th February 2020, 17:08
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.



connect(celsiusDia, SIGNAL(valueChanged(int)), this, SLOT(setTempCelsius(int))); // Notice the 'int' param
connect(fahrenheitDia, SIGNAL(valueChanged(int)), this, SLOT(setTempFahrenheit(int))); // and again