c++ - Cannot Give a Parameter To QT connect Function -


i implemented reading bytes @ serial communication in qt project(5.5) in visual studio 2013. code here:

main.cpp

int main(int argc, char *argv[]) {      qapplication app(argc, argv);     //qcoreapplication coreapplication(argc, argv);     int argumentcount = qcoreapplication::arguments().size();     qstringlist argumentlist = qcoreapplication::arguments();      qtextstream standardoutput(stdout);      if (argumentcount == 1) {         standardoutput << qobject::tr("usage: %1 <serialportname> [baudrate]").arg(argumentlist.first()) << endl;         return 1;     }      qserialport serialport;     qstring serialportname = argumentlist.at(1);     serialport.setportname(serialportname);      int serialportbaudrate = (argumentcount > 2) ? argumentlist.at(2).toint() : qserialport::baud9600;     serialport.setbaudrate(serialportbaudrate);      if (!serialport.open(qiodevice::readonly)) {         standardoutput << qobject::tr("failed open port %1, error: %2").arg(serialportname).arg(serialport.errorstring()) << endl;         return 1;     }      camera cam2;     //cam2.showcamera();     cam2.showcamera();     cam2.showfullscreen();      qbytearray abc;     serialportreader serialportreader(&serialport,abc);      //return coreapplication.exec();     return app.exec();  };  

serialportreader.cpp

#include "serialportreader.h"  #include <qcoreapplication>  qt_use_namespace  serialportreader::serialportreader(qserialport *serialport, qbytearray abc, qobject *parent) : qobject(parent) , m_serialport(serialport) , m_standardoutput(stdout) {     connect(m_serialport, &qserialport::readyread, this, &serialportreader::handlereadyread);        }  serialportreader::~serialportreader() {  }  void serialportreader::handlereadyread() {     m_readdata.append(m_serialport->readall());     if (!m_readdata.isempty()) {         m_standardoutput << m_readdata << endl;         m_readdata.clear();     }  } 

this code works successfully. set contents of m_readdata array abc array can evaluate message in main scope. in order implement this, gave abc array handlereadyread function parameter. however, gives error.the code here:

serialportreader::serialportreader(qserialport *serialport, qbytearray abc, qobject *parent) : qobject(parent) , m_serialport(serialport) , m_standardoutput(stdout) {     connect(m_serialport, &qserialport::readyread, this, &serialportreader::handlereadyread(abc)); }  serialportreader::~serialportreader() {  }  void serialportreader::handlereadyread(qbytearray abc) {     m_readdata.append(m_serialport->readall());     if (!m_readdata.isempty()) {         m_standardoutput << m_readdata << endl;         m_readdata.clear();     } }   

error:

serialportreader.cpp(45): error c2102: '&' requires l-value 

line45:

connect(m_serialport, &qserialport::readyread, this, &serialportreader::handlereadyread(abc)); 

you can't pass local variable slot way, trying doesn't require it. store abc member variable in constructor, , use in slot. but: have change reference, otherwise call constructor copies array, original 1 in main function unaffected.

you implement array member variable of serialreader, , provide getter function, main function (or whoever consumer of data) can pull data reader instance.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -