QObject::connect: 需要括号,信号 QSerialPort::readyRead in ..\voltage_sensor\dialop.cpp:41

Posted

技术标签:

【中文标题】QObject::connect: 需要括号,信号 QSerialPort::readyRead in ..\\voltage_sensor\\dialop.cpp:41【英文标题】:QObject::connect: Parentheses expected, signal QSerialPort::readyRead in ..\voltage_sensor\dialop.cpp:41QObject::connect: 需要括号,信号 QSerialPort::readyRead in ..\voltage_sensor\dialop.cpp:41 【发布时间】:2020-03-04 16:02:50 【问题描述】:

我不确定为什么 Qt 会输出这个给我。代码编译,所有语法格式正确。我曾尝试查看不同的论坛帖子,但无法查明错误。

我目前正在使用面包板、电路和灯泡进行测试;所有硬件都正常工作,我可以在 Arduino 软件中捕获电压(如果需要,我可以添加 Arduino 代码)。

我遇到的问题是我无法在 Qt 中显示电压。我正在使用 Arduino Uno 和 Arduino 电压传感器 (VCC

电压传感器.pro

QT       += core gui serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = voltage_sensor
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    dialog.cpp

HEADERS += \
    dialog.h

FORMS += \
    dialog.ui

# Default rules for deployment.
qnx: target.path = /tmp/$$TARGET/bin
else: unix:!android: target.path = /opt/$$TARGET/bin
!isEmpty(target.path): INSTALLS += target

对话框.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtSerialPort/QSerialPortInfo>
#include <QByteArray>

QT_BEGIN_NAMESPACE
namespace Ui  class Dialog; 
QT_END_NAMESPACE

class Dialog : public QDialog

    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void readSerial();
    void updateVoltage(QString);

private:
    Ui::Dialog *ui;

    QSerialPort *arduino;
    static const quint16 arduino_uno_vendor_id = 9025;
    static const quint16 arduino_uno_product_id = 67;
    QByteArray serialData;
    QString serialBuffer;
    QString parsed_data;
    double voltage_value;
;
#endif // DIALOG_H

对话框.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <string>
#include <QDebug>
#include <QMessageBox>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)

    ui->setupUi(this);
    ui->voltagelcdNumber->display("-------");
    arduino = new QSerialPort(this);
    serialBuffer = "";
    parsed_data = "";
    voltage_value = 0.0;

    bool arduino_is_available = false;
    QString arduino_uno_port_name;
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
        if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier())
            if((serialPortInfo.productIdentifier() == arduino_uno_product_id) && (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id))
                arduino_is_available = true;
                arduino_uno_port_name = serialPortInfo.portName();
            
        
    

    if(arduino_is_available)
    
        qDebug()<<"Found the port \n";
        arduino->setPortName(arduino_uno_port_name);
        arduino->open(QSerialPort::ReadOnly);
        arduino->setBaudRate(QSerialPort::Baud9600);
        arduino->setDataBits(QSerialPort::Data8);
        arduino->setFlowControl(QSerialPort::NoFlowControl);
        arduino->setParity(QSerialPort::NoParity);
        arduino->setStopBits(QSerialPort::OneStop);
        QObject::connect(arduino, SIGNAL(readyRead), this, SLOT(readSerial()));
     else 
        qDebug()<<"Could not find the correct port \n";
        QMessageBox::information(this,"Serial Port Error", "Could not open the serial port");
    


Dialog::~Dialog()

    if(arduino->isOpen())
    
        arduino->close();
    
    delete ui;


void Dialog::readSerial()

    QStringList buffer_split = serialBuffer.split(",");
    if(buffer_split.length() < 3)
    
        serialData = arduino->readAll();
        serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
        serialData.clear();
     else 
        serialBuffer = "";
        qDebug() << buffer_split << "\n";
        parsed_data = buffer_split[1];
        voltage_value = (parsed_data.toDouble()) - 0.1;
        qDebug() << "Voltage: " << voltage_value << "\n";
        parsed_data = QString::number(voltage_value,'g',4);
        Dialog::updateVoltage(parsed_data);
    


void Dialog::updateVoltage(QString sensor_reading)

    ui->voltagelcdNumber->display(sensor_reading);

main.cpp

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])

    QApplication a(argc, argv);
    Dialog w;
    w.setWindowTitle("Voltage Sensor");
    w.setFixedSize(434,122);
    w.show();

    return a.exec();

对话框.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>449</width>
    <height>157</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QLabel" name="voltagelabel">
       <property name="text">
        <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;&lt;span style=&quot; font-size:24pt; font-weight:600; color:#ff0000;&quot;&gt;Voltage&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLCDNumber" name="voltagelcdNumber">
       <property name="palette">
        <palette>
         <active>
          <colorrole role="WindowText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>255</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Light">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Dark">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Text">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="ButtonText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="PlaceholderText">
           <brush brushstyle="SolidPattern">
            <color alpha="128">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
         </active>
         <inactive>
          <colorrole role="WindowText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>255</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Light">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Dark">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Text">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="ButtonText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="PlaceholderText">
           <brush brushstyle="SolidPattern">
            <color alpha="128">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
         </inactive>
         <disabled>
          <colorrole role="WindowText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Light">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Dark">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="Text">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="ButtonText">
           <brush brushstyle="SolidPattern">
            <color alpha="255">
             <red>20</red>
             <green>20</green>
             <blue>85</blue>
            </color>
           </brush>
          </colorrole>
          <colorrole role="PlaceholderText">
           <brush brushstyle="SolidPattern">
            <color alpha="128">
             <red>0</red>
             <green>0</green>
             <blue>0</blue>
            </color>
           </brush>
          </colorrole>
         </disabled>
        </palette>
       </property>
       <property name="digitCount">
        <number>7</number>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

应用程序输出

QObject::connect: Parentheses expected, signal QSerialPort::readyRead in ..\voltage_sensor\dialog.cpp:41
QObject::connect:  (receiver name: 'Dialog')
10:56:14: C:/Users/judeb/Desktop/build-voltage_sensor-Desktop_Qt_5_12_6_MinGW_32_bit-Debug/debug/voltage_sensor.exe exited with code 0

【问题讨论】:

【参考方案1】:

现代连接语法通过在编译时检查连接来避免晦涩的运行时错误。试试这个:

QObject::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);

您的原始代码的问题是我认为您缺少一些括号:

QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));

【讨论】:

这修复了应用程序输出问题,但它仍然不会输出电压 你试过调试吗?你收到串口数据了吗? 我正在接收串行数据,我已经进行了调试,并且在代码之间进行了大量测试以查看代码是否有效。但我不知道为什么我设计的 UI 上没有电压输出 提出一个新问题,minimal reproducible example 显示问题 我添加了图片和代码:***.com/questions/60583135/no-output-from-qt-ui

以上是关于QObject::connect: 需要括号,信号 QSerialPort::readyRead in ..\voltage_sensor\dialop.cpp:41的主要内容,如果未能解决你的问题,请参考以下文章

QObject::connect: 没有这样的信号(类名)(信号名)(属性)

QObject::connect: 没有这样的信号错误 C++

QObject::connect: 没有这样的信号progressbarV::keyReleaseEvent()

如果在调用 QObject::connect() 之前发出信号,如何避免竞争?

QObject::connect: 没有这样的信号运行时警告 - 找不到原因

包装 QObject::connect 错误