自学新手编码器在这里,所以原谅错误。我正在尝试使程序发送/读取串行数据,并在读取部分时遇到问题。我可以从下拉选择通信端口,并传输我需要的东西。当我开始在线使用大量示例对接收方进行编码时,它无法编译,我似乎无法弄清楚原因。如果我完全复制QT示例中的代码,可能工作,但它不会按我想要的那样(即使用组合框下拉选项卡进行选择)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QApplication>
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
QString commPort;
QSerialPort serial;
QByteArray charBuffer;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) //populates the combo box with all availble comm ports.
{
ui->comboBox->addItem(serialPortInfo.portName());
commPort=serialPortInfo.portName();
}
}
MainWindow::~MainWindow()
{
delete ui;
serial.close();
}
void MainWindow::on_comboBox_activated(const QString &commPort) //selects the chosen comm port.
{
ui->report->setText(commPort);
}
void MainWindow::openSerialPort()
{
serial.setPortName(commPort);
if(serial.open(QIODevice::ReadWrite))
{
qDebug("Serial Opened");
ui->report->setStyleSheet("QLabel{background-color:'green';}");
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial);
//^^That is the part that fails to compile
}
else
{
ui->report->setStyleSheet("QLabel{background-color:'red';}");
qDebug ("Serial NOT Opened");
qDebug() << serial.error();
qDebug() << serial.errorString();
}
}
void MainWindow::on_connectButton_pressed()
{
openSerialPort();
}
void MainWindow::readSerial()
{
qDebug()<<("serial works");
}
*more code follows, but not needed....
表示“connect(serial,&amp; QSerialPort :: readyRead,this,&amp; MainWindow :: readSerial)”的行;“ 是罪魁祸首给我错误: mainwindow.cpp:52:错误:没有匹配函数来调用'MainWindow :: connect(QSerialPort&amp;,void(QIODevice :: )(),MainWindow const,void(MainWindow :: *)())” connect(serial,&amp; QSerialPort :: readyRead,this,&amp; MainWindow :: readSerial);
我尝试读取QObject和QSerialPort库帮助信息,但SIGNAL和SLOT的东西对于这个amatuer来说太混乱了。我还在网上采集了部分示例并粘贴以尝试修复它,...没有骰子。
答案 0 :(得分:0)
您必须传递发件人的指针。
connect (const QObject * sender,const char * signal,const QObject * receiver,const char *方法,Qt :: ConnectionType类型)
connect (const QObject * sender,const char * signal,const char * method,Qt :: ConnectionType type)
连接(const QObject * sender,PointerToMemberFunction信号,const QObject *接收器,PointerToMemberFunction方法,Qt :: ConnectionType类型)
连接(const QObject * sender,PointerToMemberFunction信号,Functor仿函数)
connect (const QObject * sender,PointerToMemberFunction信号,const QObject * context,Functor functor,Qt :: ConnectionType类型)
变化:
connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial);
到
connect(&serial, &QSerialPort::readyRead, this,&MainWindow::readSerial);