2017-03-22 13 views
0

自己教養の新人コーダーは、ここで間違いを赦してください。私はプログラムがシリアルデータを送信/読み込みし、その読み込み部分に問題があるようにしようとしています。私はプルダウンから通信ポートを選択し、必要なものを送信することができます。私が受信側で多数の例をオンラインでコーディングし始めたとき、それはコンパイルに失敗し、私はなぜそれを理解できないようです。私は正確にQTの例からコードをコピーする場合、それ仕事かもしれないが、それは文句を言わない(つまり、選択のためのタブをプルダウンコンボボックスを使用)コードをコンパイルするときに「呼び出しに一致する関数がありません」。 [QTSerialPort開封]

#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.... 

に私はそれを何をしたい "という行を行います接続(シリアル、& QSerialPort :: readyRead、これ、& MainWindow :: readSerial); " は私にエラーを与えて犯人です: mainwindow.cpp:52:エラー:メインウィンドウ「への呼び出しに該当する機能::(無効、()のQIODevice :: (無効QSerialPort &、()、メインウィンドウのconstを接続しますMainWindow :: *)()) ' (シリアル、& QSerialPort :: readyRead、this、& MainWindow :: readSerial);

QObjectとQSerialPortライブラリのヘルプ情報を読み込もうとしましたが、SIGNALとSLOTの内容はこのアマチュアにとってあまりにも混乱しています。私も、サンプルをオンラインで貼り付けて試してみました。

答えて

0

送信者のポインタを渡す必要があります。

connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)

connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type)

connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)

connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)

変更:

connect(serial, &QSerialPort::readyRead, this,&MainWindow::readSerial); 

connect(&serial, &QSerialPort::readyRead, this,&MainWindow::readSerial); 
+0

にありがとうございました。それはそれをした! – Xarddrax

+0

私の答えが役に立ったら、それを正しいものとしてマークしてください。 – eyllanesc

関連する問題