简体   繁体   中英

QT slots and signals fail

Hi there i got problem with signal and slots in qt. In main i have created object of mainwindow. in mainwindow.cpp i creating object of another class(modbus_tcp). i also creating connection here

void MainWindow::on_ConnectB_clicked()
{

    modbus_tcp appts;
    appts.slave();
    connect(&appts,SIGNAL(msgSended(QString)),this,SLOT(msgEdit(QString)));
}

between slot declared in mainwindow.cpp/h

public slots:
void msgEdit(QString m);

void MainWindow::msgEdit(QString m)
{
ui->sendEdit->setText(m);
ui->recvEdit->setText(m);
//QMessageBox::information(0,"bad", "nope nope nope");
}

and signal declared in modbus_tcp.h

signals:
void msgSended(QString);

next i emiting signal in modbus_tcp.cpp

emit msgSended("asdasd");

and nothing happen

when i trying to emit in mainwindow.cpp its working

any ideas ?

void MainWindow::on_ConnectB_clicked()
{

    modbus_tcp appts;
    appts.slave();
    connect(&appts,SIGNAL(msgSended(QString)),this,SLOT(msgEdit(QString)));
}

appts was created in stack, so it will be deleted at the end of slot execution. Try to create it in the heap(try to use pointer).

void MainWindow::on_ConnectB_clicked()
{

    modbus_tcp *appts = new modbus_tcp;
    connect(appts,SIGNAL(msgSended(QString)),this,SLOT(msgEdit(QString)));//first!
    appts->slave();//now you can call it
}

Use pointers, but first of all connect , and after this call slave . You emit signal in slave , but there is no connection in this time. You should do connection firstly and after that, you will be able to catch signals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM