Unsolved Is data received from serial port possible to view on two forms?
-
connect(this, &MainWindow:: newSerialData, Setup, &Setup:: onNewSerialData);
Here there is an error in connect, says: Setup doesn't refer to a value.
As the message states. The
Setup
as the 3rd parameter is a class. You need an instance (just like you have for the 1st parameter). See the docs. -
@Shivam-Sharma
I said where you create Setup, you put the connect into the constructor of Setuphere:
void MainWindow::on_setupbutton_clicked() { setup = new Setup(this); setup->show(); connect(this, &MainWindow::newSerialData, setup, &Setup::onNewSerialData); }
but, every time you click on the setupButton you will create a new instance of that class, you're leaking memory
Edit changed for error free connect, thanks to @JonB for mentioning it
-
@J-Hilk
As per my previous, to help the user in this case you should change your 3rd param fromSetup
tosetup
anyway. Unless you want him to figure for himself (which would not be a bad idea, bit late now that I have spoken!) :) -
@J-Hilk everthing is error free except one, which I ain't undertsanding:
'Setup' does not name a type.
-
@Shivam-Sharma
where ?
It should point you to a line
actually, remove the mainwindow.h include from setup
here#include "setup.h" #include "ui_setup.h" #include <QSerialPort> #include <QDebug> #include <mainwindow.h> #include <QSerialPortInfo> #include <QFile>
and here
#ifndef SETUP_H #define SETUP_H #include <mainwindow.h> #include <QDialog>
thats a circular include with your mainwindow.h were you include setup.h
-
@Shivam-Sharma
As @J-Hilk has said. And further to that: (unless a Qt expert contradicts me) you should never be includingmainwindow.h
into anything other thanmainwindow.cpp
and yourmain.cpp
, not into any other UI stuff you define. Keeps it clean --- they should never need to know about yourMainWindow
class. This is true all over the place: a widget which accesses/opens a "child" widget should include that child's.h
file, but the "child" widgets should never need to know about/include the.h
file of other ("parent") widgets where they might be called from. At least usually. -
This post is deleted! -
-
Hi,
One small addition, when passing QString around like that (but it is also valid for other classes), it's a good habit to use const references. This allows to avoid creating unneeded copies.
-
@J-Hilk @JonB Hi guys. Need one more help. If i want to add a push button on the Setup form that connects and disconnects the serial data incoming on both the forms how could I do that.
This is the code for the push button, i can do it on the mainwindow form but unable to do it on the Setup form since it is not being able to access *serial.
Code:
void MainWindow::on_start_toggled(bool checked) { ui->start->setText(ui->start->isChecked() ? "Connect" : "Disconnect"); if (checked) { serial->close(); ui->start->setStyleSheet(QString("QPushButton {background-color: rgb(115, 210, 22);}")); } else { ui->start->setStyleSheet(QString("QPushButton {background-color: red;}")); serial = new QSerialPort(this); serial = new QSerialPort(this); serial->setPortName("ttyUSB0"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); if( serial->open(QIODevice::ReadWrite)) { qDebug()<<"Port Open OK"; connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived); } else { qDebug()<<"Can't Open Port : " << serial->error(); } serialReceived(); } }
-
@Shivam-Sharma Create a signal in Setup, connect that signal to clicked() signal of the button in Setup and then connect the signal you created in Setup (the first one I mentioned) to a slot in MainWindow where you can enable/disable the serial port.
-
I am sorry, but unable to understand. How do i connect the pushbutton to mainwindow from Setup form, since i cannot inherit the parent class, neither can i create an instance of it. Could you give a demo?
-
@Shivam-Sharma said in Is data received from serial port possible to view on two forms?:
How do i connect the pushbutton to mainwindow from Setup form
You don't, please read more carefully.
class Setup { signals: void onOffSerialPort(); }; void Setup::Setup() { connect(ui->button, &QPushButton::clicked, this, &Setup::onOffSerialButton); } // MainWindow connect(setup, &Setup::onOffSerialButton, this, &MainWindow::onOffSerialButton);
As you can see Setup does not know anything about MainWindow.
-
// MainWindow connect(setup, &Setup::onOffSerialButton, this, &MainWindow::onOffSerialButton);
shouldn't all of this be
onOffSerialPort
and the last part be :
&MainWindow::close); void Mainwindow::close() { serial->close(); }
-
@Shivam-Sharma Sorry, I don't understand what you mean. The code I provided is just to show the idea, you need to adapt it to your needs.
-
let me expand on @jsulm example
first of in your mainwindow, create a new function, defined as a slot
and copy the content of your on button clicked in there
public slots: void toggleSerial(bool checked); //mainwindow.cpp void MainWindow:: toggleSerial(bool checked) { ui->start->setText(ui->start->isChecked() ? "Connect" : "Disconnect"); if (checked) { serial->close(); ui->start->setStyleSheet(QString("QPushButton {background-color: rgb(115, 210, 22);}")); } else { ui->start->setStyleSheet(QString("QPushButton {background-color: red;}")); serial = new QSerialPort(this); serial = new QSerialPort(this); serial->setPortName("ttyUSB0"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); if( serial->open(QIODevice::ReadWrite)) { qDebug()<<"Port Open OK"; connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived); } else { qDebug()<<"Can't Open Port : " << serial->error(); } serialReceived(); } } void MainWindow::on_start_toggled(bool checked) { toggleSerial(checked); }
inside Setup add the signal like @jsulm suggested
signals: void onOffSerialPort(bool checked);
connect your button toggle signal (in Setup) to this signal (constructor of Setup
connect(ui->myButton, &QPushButton:: toggled, this, &Setup:: onOffSerialPort);
and finally
connect thatonOffSerialPort
signal to yourtoggleSerial
slotconnect(setup, &Setup:: onOffSerialPort, this, &MainWindow:: toggleSerial);
one thing to keep in mind, your 2 buttons will now not necessarily show the correct checked status.
I'll let you figure that one out.
-
Error: no member named start in ui::mainwindow,
that is because it isn't able to access the pushbutton from setup window. Help please.
Also:
void MainWindow::on_start_toggled(bool checked)
{
toggleSerial(checked);
}
this also has errors saying: on_start_toggled doesn't match any declaration. -
@Shivam-Sharma
where did you put this ?public slots: void toggleSerial(bool checked);
in mainwindow.h or on setup.h ?
-
in mainwindow.h
-
@Shivam-Sharma said in Is data received from serial port possible to view on two forms?:
that is because it isn't able to access the pushbutton from setup window
I repeat it once more: YOU DO NOT HAVE TO ACCESS THIS BUTTON FROM MAIN WINDOW!
Please show your current code.