سؤال

I'm working on a graphical interface which connects a 8051 with the computer through a serial port. I can write on the port perfectly from the MainWindow through:

void MainWindow::EnviarComando(QString comando)
{
    QByteArray send = (comando + "\r\n").toAscii();
    m_port->write(send);

    ui->list_log_enviados->addItem(comando);
    ui->list_log_enviados->setCurrentRow(ui->list_log_enviados->count()-1);

    m_cant_bytes_enviados += send.size();
}

being m_port the variable holding the stream for the connection. I want to instantiate QDialog objects from the menu but still be able to send commands from these dialogs. Is it possible? I tried through friends functions, external variables, etc but since the MainWindow object calls the QDialog, its out of scope anyways. Is there any other alternative?

هل كانت مفيدة؟

المحلول

If you instaniate the QDialog as this:

void MainWindow::on_menucommand_triggered()
{
    MyDialog d(this);
    if(d.exec()==QDialog::Accepted)
    {
        //do something..
    }
}

Then, MyDialog can retrieve MainWindow from its QDialog::parent() method. Like this:

void MyDialog::checkSomething()
{
    MainWindow *mainWindow;
    mainWindow=qobject_cast<MainWindow*>(parent());
    if(mainWindow)
    {
        // mainWindow->sendToSerialPort(bytes);
    }
}

At the point of view of myself, this could break the packaging of MainWindow class. May be you can overload the construct function of QDialog, and pass the serial port object in the argument.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top