A
A
AlexSer2020-01-13 13:38:04
Qt
AlexSer, 2020-01-13 13:38:04

How can I solve the problem of passing a parameter to a slot in QT c++?

I am writing a program to receive and parse data on a com port. Faced such a problem. When connecting to a com port, it is necessary that the received data handler class be launched. But parameters cannot be passed to the slot.
via QObject::sender is also impossible, since first there is a signal to connect to the com port, then a signal to read data from the com port.
Here's what the algorithm looks like:
from the Action menu -> connect with someone -> connect to read data.
Here is the code:

В mainwindow    
connect(ui->actionSwelab_Alfa_1, SIGNAL(triggered()), this, SLOT(ConnCom());
connect(ui->actionSwelab_Alfa_2, SIGNAL(triggered()), this, SLOT(ConnCom());

void MainWindow::ConnCom()(QString port,int baudrate,QString analizator){
 
             serialPort=new QSerialPort(this);
             serialPort->setPortName(port);
             serialPort->setBaudRate(baudrate);
             serialPort->setDataBits(QSerialPort::Data8);
             serialPort->setParity(QSerialPort::NoParity);
             serialPort->setStopBits(QSerialPort::OneStop);
             serialPort->setFlowControl(QSerialPort::NoFlowControl);
            //настройки для CTS/RTS
            //serialPort->setRequestToSend(true);
            //serialPort->setDataTerminalReady(true);
             serialPort->open(QIODevice::ReadOnly);
      
##Сигнал на чтение данных с ком порта
            QObject::connect(serialPort,SIGNAL(readyRead()),this,SLOT(ReadDataSerial()));

             if(serialPort->isOpen()){
                 QMessageBox::about(this,"title","Соединение установлено успешно");
                 dialogSettCom->close();
                 //запускаем чтение данных с порта
             }else{
                  QMessageBox::warning(this,"title","Соединение не установлено");
             }

}

void MainWindow::ReadDataSerial(){
  QString byte;
  byte = serialPort->readAll();
  ui->terminal->append("send sample \n"+ byte);
//класс обработчик для каждого устройства свой?
 SweelabAlfa *Alfa;
 Alfa=new SweelabAlfa();

//я хочу чтобы приходил какой нить параметр, который  определял бы какой класс подключать
если бы QObjet::sender()  был бы , я бы просто использовал бы if().

}

Please tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question