I am sure my problem is very simple but I just can't debug it!!
I am trying to connect a spinBox widget to a custom function that sets the com port of my QSerialPort instance on Qt.
My Main Window contructor, I try connecting my com_spinBox to my custom function called setComPort() :
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this); // Here by default. Takes a pointer to mainwindow as argument
serial = new QSerialPort(this); // this (mainwindow) is parent
connect(ui->com_spinBox, SIGNAL(valueChanged(QString)),
this, SLOT(MainWindow::setComPort()));
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
qDebug() << "is " << serial->open(QSerialPort::ReadOnly);
qDebug() << "err " << serial->error();
}
I define my function in my MainWindow.cpp (and declare it in .h) too:
void MainWindow::setComPort()
{
qDebug() << "fu";
}
I do not see anything appear on debug console when changing value of my spinBox.. is it a synthax problem? I know qDebug works as I can print other stuff in my constructor no problem. Thanks!
You are mixing the old and new syntax of
QObject::connectand in a wrong way by using function pointer .. this is wrong:SLOT(MainWindow::setComPort().If you want to purposely use the old syntax .. do not use function pointers along with
SLOT, just use that slot signature; yourconnectshould be like this:On the other hand, using new syntax with function pointers is possible .. you just need to use overloaded signal signature of
QSpinBox, as documented here https://doc.qt.io/qt-5/qspinbox.html#valueChanged:Note also that, while Qt connect() allows the slot to have no arguments, and based on guessing your intention .. you might want to change the slot signature to receive the serial port number ..
MainWindow::setComPort(const QString&).