Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort::readyRead couldn't connect to my slot.
Forum Update on Monday, May 27th 2025

QSerialPort::readyRead couldn't connect to my slot.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 245 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Enem
    wrote on last edited by Enem
    #1
    //mainwindow.cpp
    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    #include <QMetaMethod>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        serialPort = new QSerialPort(this);
        serialPort->setPortName("COM4");
        serialPort->setBaudRate(QSerialPort::Baud9600);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setParity(QSerialPort::NoParity);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
    
        serialPort->open(QIODevice::ReadWrite);
        if(serialPort->isOpen())
            serialPort->write("*IDN?\n");
    
        connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::readData);
    
        if(isSignalConnected(QMetaMethod::fromSignal(&QSerialPort::readyRead)))
            qDebug() << "connected";
        else
            qDebug() << "disconnected";
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::readData(){
        static QByteArray ba;
    
        size_t current = 0;
        if(serialPort->waitForBytesWritten(1000)){
    
        }
    
        if(serialPort->waitForReadyRead(1000)){
    
        }
        if(serialPort->bytesAvailable()){
            ba = serialPort->readAll();
        }
    
    }
    
    //mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MainWindow;
    }
    QT_END_NAMESPACE
    
    class QSerialPort;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QSerialPort* serialPort;
    private slots:
        void readData();
    };
    #endif // MAINWINDOW_H
    
    

    This is minimal example to reproduce the error.
    QObject::connect returns false when i try to connect QSerialPort::readyRead to MainWindow::readData slot. A lambda instead of class member function also doesn't work.
    How could I fix this? What I'm doing wrong? Please, help me to figure it out.
    Extra question: How will this work for multiple theads?
    EDIT:
    Qt version is 6.6.1. Compiler is MinGW 64-bit. Project generate system is cmake

    jsulmJ Christian EhrlicherC 2 Replies Last reply
    0
    • E Enem
      //mainwindow.cpp
      #include "mainwindow.h"
      #include "./ui_mainwindow.h"
      
      #include <QMetaMethod>
      #include <QSerialPort>
      #include <QSerialPortInfo>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          serialPort = new QSerialPort(this);
          serialPort->setPortName("COM4");
          serialPort->setBaudRate(QSerialPort::Baud9600);
          serialPort->setDataBits(QSerialPort::Data8);
          serialPort->setParity(QSerialPort::NoParity);
          serialPort->setStopBits(QSerialPort::OneStop);
          serialPort->setFlowControl(QSerialPort::NoFlowControl);
      
          serialPort->open(QIODevice::ReadWrite);
          if(serialPort->isOpen())
              serialPort->write("*IDN?\n");
      
          connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::readData);
      
          if(isSignalConnected(QMetaMethod::fromSignal(&QSerialPort::readyRead)))
              qDebug() << "connected";
          else
              qDebug() << "disconnected";
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::readData(){
          static QByteArray ba;
      
          size_t current = 0;
          if(serialPort->waitForBytesWritten(1000)){
      
          }
      
          if(serialPort->waitForReadyRead(1000)){
      
          }
          if(serialPort->bytesAvailable()){
              ba = serialPort->readAll();
          }
      
      }
      
      //mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      QT_BEGIN_NAMESPACE
      namespace Ui {
      class MainWindow;
      }
      QT_END_NAMESPACE
      
      class QSerialPort;
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          QSerialPort* serialPort;
      private slots:
          void readData();
      };
      #endif // MAINWINDOW_H
      
      

      This is minimal example to reproduce the error.
      QObject::connect returns false when i try to connect QSerialPort::readyRead to MainWindow::readData slot. A lambda instead of class member function also doesn't work.
      How could I fix this? What I'm doing wrong? Please, help me to figure it out.
      Extra question: How will this work for multiple theads?
      EDIT:
      Qt version is 6.6.1. Compiler is MinGW 64-bit. Project generate system is cmake

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Enem said in QSerialPort::readyRead couldn't connect to my slot.:

      if(isSignalConnected(QMetaMethod::fromSignal(&QSerialPort::readyRead)))
      qDebug() << "connected";
      else
      qDebug() << "disconnected";

      Why don't you simply check the return value of your connect(...) call?

      Read https://doc.qt.io/qt-6/qobject.html#isSignalConnected
      "signal must be a signal member of this object, otherwise the behaviour is undefined"
      This is exactly what you're doing wrong.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      1
      • E Enem
        //mainwindow.cpp
        #include "mainwindow.h"
        #include "./ui_mainwindow.h"
        
        #include <QMetaMethod>
        #include <QSerialPort>
        #include <QSerialPortInfo>
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            serialPort = new QSerialPort(this);
            serialPort->setPortName("COM4");
            serialPort->setBaudRate(QSerialPort::Baud9600);
            serialPort->setDataBits(QSerialPort::Data8);
            serialPort->setParity(QSerialPort::NoParity);
            serialPort->setStopBits(QSerialPort::OneStop);
            serialPort->setFlowControl(QSerialPort::NoFlowControl);
        
            serialPort->open(QIODevice::ReadWrite);
            if(serialPort->isOpen())
                serialPort->write("*IDN?\n");
        
            connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::readData);
        
            if(isSignalConnected(QMetaMethod::fromSignal(&QSerialPort::readyRead)))
                qDebug() << "connected";
            else
                qDebug() << "disconnected";
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::readData(){
            static QByteArray ba;
        
            size_t current = 0;
            if(serialPort->waitForBytesWritten(1000)){
        
            }
        
            if(serialPort->waitForReadyRead(1000)){
        
            }
            if(serialPort->bytesAvailable()){
                ba = serialPort->readAll();
            }
        
        }
        
        //mainwindow.h
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui {
        class MainWindow;
        }
        QT_END_NAMESPACE
        
        class QSerialPort;
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
            QSerialPort* serialPort;
        private slots:
            void readData();
        };
        #endif // MAINWINDOW_H
        
        

        This is minimal example to reproduce the error.
        QObject::connect returns false when i try to connect QSerialPort::readyRead to MainWindow::readData slot. A lambda instead of class member function also doesn't work.
        How could I fix this? What I'm doing wrong? Please, help me to figure it out.
        Extra question: How will this work for multiple theads?
        EDIT:
        Qt version is 6.6.1. Compiler is MinGW 64-bit. Project generate system is cmake

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Enem said in QSerialPort::readyRead couldn't connect to my slot.:

          if(isSignalConnected(QMetaMethod::fromSignal(&QSerialPort::readyRead)))
          qDebug() << "connected";
          else
          qDebug() << "disconnected";

          Why don't you simply check the return value of your connect(...) call?

          Read https://doc.qt.io/qt-6/qobject.html#isSignalConnected
          "signal must be a signal member of this object, otherwise the behaviour is undefined"
          This is exactly what you're doing wrong.

          E Offline
          E Offline
          Enem
          wrote on last edited by
          #4

          @jsulm
          Oh, this is stupid mistake. I'm tired and I can't see anything in front of me. Very grateful to you!

          1 Reply Last reply
          1
          • E Enem has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved