尝试从 QDialog 写入串行端口。
我正在开发一个图形界面,它通过串行端口将 8051 与计算机连接起来。 我可以通过以下方式从主窗口完美地在端口上写入:
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();
}
m_port 是保存连接流的变量。 我想从菜单实例化 QDialog 对象,但仍然能够从这些对话框发送命令。是否可以? 我尝试通过朋友函数、外部变量等,但由于 MainWindow 对象调用 QDialog,它无论如何都超出了范围。还有其他选择吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您按如下方式实例化 QDialog:
那么,MyDialog 可以从其 QDialog::parent() 方法检索 MainWindow。像这样:
从我自己的角度来看,这可能会破坏 MainWindow 类的包装。也许您可以重载 QDialog 的构造函数,并在参数中传递串行端口对象。
If you instaniate the QDialog as this:
Then, MyDialog can retrieve MainWindow from its QDialog::parent() method. Like this:
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.