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. Subclassing QIODevice
Qt 6.11 is out! See what's new in the release blog

Subclassing QIODevice

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 4.7k Views 2 Watching
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #6
        int error_code = port1.open(QIODevice::ReadWrite);
        if (error_code)
            qDebug("Port opened");
    

    This is wrong - if you can't open the port you should not work on it.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    P 1 Reply Last reply
    3
    • Christian EhrlicherC Christian Ehrlicher
          int error_code = port1.open(QIODevice::ReadWrite);
          if (error_code)
              qDebug("Port opened");
      

      This is wrong - if you can't open the port you should not work on it.

      P Offline
      P Offline
      PaulNewman
      wrote on last edited by
      #7

      @Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not

      Christian EhrlicherC 1 Reply Last reply
      0
      • P PaulNewman

        @Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #8

        @PaulNewman But you still go on when it failed.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        P 1 Reply Last reply
        0
        • M MoonRaiser

          Hello, i'm trying to subclass QIODevice, create my own QSerialPort with another backend. I dont wanna use standard module due to it isnt compatitible with my hardware https://bugreports.qt.io/browse/QTBUG-87265
          I tried to follow docs and constructed this

          class SerialPort : public QIODevice {
          
              Q_OBJECT
              Q_DECLARE_PRIVATE(SerialPort)
          public:
              explicit SerialPort(QObject* parent = nullptr);
              explicit SerialPort(const QString& name, QObject* parent = nullptr);
              explicit SerialPort(const SerialPortInfo& info, QObject* parent = nullptr);
              ~SerialPort();
          
             ...
          public:
              bool open(OpenMode mode) override;
              void close() override;
              qint64 pos() const override;
              qint64 size() const override;
              bool atEnd() const override;
              qint64 bytesAvailable() const override;
              qint64 bytesToWrite() const override;
              bool waitForReadyRead(int msecs = 0) override;
              bool waitForBytesWritten(int msecs = 0) override;
              inline bool isSequential() const override;
          
              QString portName() const;
              bool setPortName(const QString& name);
          
              int baudRate() const;
              bool setBaudRate(const int& baudRate);
          
              DataBits dataBits() const;
              bool setDataBits(const DataBits& dataBits);
          
              Parity parity() const;
              bool setParity(const Parity& parity);
          
              StopBits stopBits() const;
              bool setStopBits(const StopBits& stopBits);
          
              FlowControl flowControl() const;
              bool setFlowControl(const FlowControl& flowControl);
          
          signals:
              void baudRateChanged(int baudRate);
              void dataBitsChanged(SerialPort::DataBits dataBits);
              void parityChanged(SerialPort::Parity parity);
              void stopBitsChanged(SerialPort::StopBits stopBits);
              void flowControlChanged(SerialPort::FlowControl flowControl);
          
          protected:
              qint64 readData(char* data, qint64 maxlen) override;
              qint64 writeData(const char* data, qint64 len) override;
          
          private:
              uint m_timeout = 1000;
              Q_DISABLE_COPY(SerialPort)
          
              // QIODevice interface
          public:
              bool canReadLine() const override;
          };
          

          alt text
          But i caught this error. I implemented my own isSequential func but it uses another.
          My own version looks like this

          bool SerialPort::isSequential() const
          {
              qDebug(__PRETTY_FUNCTION__);
              return true;
          }
          

          So what i do wrong? If i dont provide enough info everything is available here https://github.com/Jihadist/libserial-wrapper

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #9

          @MoonRaiser
          you open the serial port before you try to change the settings, you should actually get console warnings about that.
          Set settings first than open the port


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          P 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @MoonRaiser
            you open the serial port before you try to change the settings, you should actually get console warnings about that.
            Set settings first than open the port

            P Offline
            P Offline
            PaulNewman
            wrote on last edited by
            #10

            @J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?

            J.HilkJ 1 Reply Last reply
            0
            • P PaulNewman

              @J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #11

              @PaulNewman I did in fact not see that 🤷‍♂️


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @PaulNewman But you still go on when it failed.

                P Offline
                P Offline
                PaulNewman
                wrote on last edited by
                #12

                @Christian-Ehrlicher i dont, look at check() func

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @PaulNewman said in Subclassing QIODevice:

                  i dont, look at check() func

                  You do...

                      int error_code = port1.open(QIODevice::ReadWrite);
                      if (error_code)
                          qDebug("Port opened");
                      if (port1.setBaudRate(9600)) <<-- here you try access even if open failed
                  

                  and btw: open() returns a boolean, not an integer

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  P 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @PaulNewman said in Subclassing QIODevice:

                    i dont, look at check() func

                    You do...

                        int error_code = port1.open(QIODevice::ReadWrite);
                        if (error_code)
                            qDebug("Port opened");
                        if (port1.setBaudRate(9600)) <<-- here you try access even if open failed
                    

                    and btw: open() returns a boolean, not an integer

                    P Offline
                    P Offline
                    PaulNewman
                    wrote on last edited by
                    #14

                    @Christian-Ehrlicher you are right here, but check() calls abort() if not true, so it never returns if port cannot be opened. It's only test case but it doesnt work when port was opened

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      PaulNewman
                      wrote on last edited by PaulNewman
                      #15

                      @Christian-Ehrlicher said in Subclassing QIODevice:

                      if (port1.setBaudRate(9600)) <<-- here you try access even if open failed

                      I fixed this, but problem stays...
                      Source code from topic header was updated to.
                      alt text

                      Why did it call QIODevice::isSequential but not my own implementation?

                      1 Reply Last reply
                      0

                      • Login

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