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. Help me
Forum Updated to NodeBB v4.3 + New Features

Help me

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 767 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.
  • Q Qt fa schifo

    Hi everyone,
    I'm learning to use Qt but with a lot of difficulty. I created a TCP network connection but it doesn't work and I can't fix the code. Would some good soul help me?

    mytcpsocket.h

    #include <QtNetwork/QTcpSocket>
    #include <QtNetwork/QAbstractSocket>
    #include <QDebug>
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MyTcpSocket;
    }
    QT_END_NAMESPACE
    
    class MyTcpSocket : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MyTcpSocket(QWidget *parent = nullptr);
        ~MyTcpSocket();
        //    void doConnect();
    
    private slots:
        void on_cmdinvia_clicked();
    
        void on_cmdconnetti_clicked();
    
    public slots:
        void doConnect();
        void connected();
        void disconnected();
        void bytesWritten(qint64 bytes);
        void readyRead();
       // void subinviadato();
        void subinviadatotest();
    
    private:
        Ui::MyTcpSocket *ui;
            QTcpSocket *socket;
    
    
    };
    #endif // MYTCPSOCKET_H
    
    

    main.cpp

    #include "mytcpsocket.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MyTcpSocket w;
        w.show();
        return a.exec();
    }
    
    

    mytcpsocket.cpp

    #include "mytcpsocket.h"
    #include "ui_mytcpsocket.h"
    #include "QTimer"
    #include <QThread>
    #include <QString>
    #include <QObject>
    #include <QtNetwork/QTcpSocket>
    #include <QtNetwork/QAbstractSocket>
    #include <QDebug>
    #include <QtWidgets>
    
    MyTcpSocket::MyTcpSocket(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MyTcpSocket)
    {
        ui->setupUi(this);
    }
    
    MyTcpSocket::~MyTcpSocket()
    {
        delete ui;
    }
    
    void MyTcpSocket::on_cmdinvia_clicked()
    {
        subinviadatotest();
    }
    
    
    void MyTcpSocket::on_cmdconnetti_clicked()
    {
        readyRead();
        doConnect();
    }
    
    //void subinviadato();
    
    
    //MyTcpSocket::MyTcpSocket(QObject *parent) :
    //    QObject(parent)
    //{
    //}
    
    
    void MyTcpSocket::doConnect()
    {
        //socket = new QTcpSocket(this); //NON FUNZIONA
        //QTcpSocket socket;
    
        connect(socket, SIGNAL(connected()),this, SLOT(connected()));
        connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
        //connect(socket, SIGNAL(bytesWritten(1)),this, SLOT(bytesWritten(1)));
        connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
    
        qDebug() << "connecting...";
    
        // this is not blocking call
        socket->connectToHost("192.168.1.26", 88);
    
        // we need to wait...
        if(!socket->waitForConnected(5000))
        {
            qDebug() << "Error: " << socket->errorString();
        }
    }
    
    void MyTcpSocket::connected()
    {
        qDebug() << "connected...";
    
         subinviadatotest();
    
    }
    
    void MyTcpSocket::subinviadatotest()
    {
        //Dichiaro variabili che uso;
        QChar str;
        QString stringa;
        stringa = "{P01-P07-DAT-AXX}";
        long lunghezza;
    
        // (const char *)&str << stringa.at(stringa.length() - 2);
        //qDebug() str;
    
        //Misuro lunghezza stringhe
        lunghezza=stringa.length();
    
        //Visualizzo informazioni
        qDebug("  ---  ");
        QString ss = QString::number(lunghezza);
        qDebug() << ss;
        qDebug("  ---  ");
    
        long i;
        for(i=0;i<lunghezza;i++) {
    
            //if(stringa[i]=='}')
            //    break;
            QChar caratt;
            caratt=stringa[i];
            //qDebug("-");
            //qDebug() << caratt;
            str= caratt; //QChar('{');
            //const char *str = qPrintable(stringa);
    
            socket->write((const char *)&str, 1);
            socket->waitForBytesWritten(1000);
            QThread::msleep(100);
    
        }
    
    
    }
    
    
    
    void MyTcpSocket::disconnected()
    {
        qDebug() << "disconnected...";
    }
    
    void MyTcpSocket::bytesWritten(qint64 bytes)
    {
        qDebug() << bytes << " bytes written...";
    }
    
    void MyTcpSocket::readyRead()
    {
        qDebug() << "reading...";
    
        // read the data from the socket
        qDebug() << socket->readAll();
    }
    
    

    mytcpsocket.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MyTcpSocket</class>
     <widget class="QMainWindow" name="MyTcpSocket">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MyTcpSocket</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QPushButton" name="cmdinvia">
        <property name="geometry">
         <rect>
          <x>600</x>
          <y>490</y>
          <width>181</width>
          <height>29</height>
         </rect>
        </property>
        <property name="text">
         <string>Invia</string>
        </property>
       </widget>
       <widget class="QLineEdit" name="txtinvia">
        <property name="geometry">
         <rect>
          <x>30</x>
          <y>490</y>
          <width>561</width>
          <height>26</height>
         </rect>
        </property>
       </widget>
       <widget class="QTextEdit" name="txtricevuto">
        <property name="geometry">
         <rect>
          <x>30</x>
          <y>60</y>
          <width>751</width>
          <height>421</height>
         </rect>
        </property>
       </widget>
       <widget class="QPushButton" name="cmdconnetti">
        <property name="geometry">
         <rect>
          <x>520</x>
          <y>10</y>
          <width>231</width>
          <height>41</height>
         </rect>
        </property>
        <property name="text">
         <string>Connetti</string>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>26</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    I use Qt 12.0.2 with 6.6.2 min gw

    Ronel_qtmasterR Offline
    Ronel_qtmasterR Offline
    Ronel_qtmaster
    wrote on last edited by
    #2

    @Qt-fa-schifo hi and welcome to qt forum.As You are a beginner, i would advice you to check this https://www.bogotobogo.com/Qt/Qt5_QTcpSocket_Signals_Slots.php

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi and welcome to devnet,

      Beside the recommendation of @Ronel_qtmaster, you commented out the line where you create the QTcpSocket object so it can't even start to work.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Q Qt fa schifo

        Hi everyone,
        I'm learning to use Qt but with a lot of difficulty. I created a TCP network connection but it doesn't work and I can't fix the code. Would some good soul help me?

        mytcpsocket.h

        #include <QtNetwork/QTcpSocket>
        #include <QtNetwork/QAbstractSocket>
        #include <QDebug>
        
        QT_BEGIN_NAMESPACE
        namespace Ui {
        class MyTcpSocket;
        }
        QT_END_NAMESPACE
        
        class MyTcpSocket : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MyTcpSocket(QWidget *parent = nullptr);
            ~MyTcpSocket();
            //    void doConnect();
        
        private slots:
            void on_cmdinvia_clicked();
        
            void on_cmdconnetti_clicked();
        
        public slots:
            void doConnect();
            void connected();
            void disconnected();
            void bytesWritten(qint64 bytes);
            void readyRead();
           // void subinviadato();
            void subinviadatotest();
        
        private:
            Ui::MyTcpSocket *ui;
                QTcpSocket *socket;
        
        
        };
        #endif // MYTCPSOCKET_H
        
        

        main.cpp

        #include "mytcpsocket.h"
        
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MyTcpSocket w;
            w.show();
            return a.exec();
        }
        
        

        mytcpsocket.cpp

        #include "mytcpsocket.h"
        #include "ui_mytcpsocket.h"
        #include "QTimer"
        #include <QThread>
        #include <QString>
        #include <QObject>
        #include <QtNetwork/QTcpSocket>
        #include <QtNetwork/QAbstractSocket>
        #include <QDebug>
        #include <QtWidgets>
        
        MyTcpSocket::MyTcpSocket(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MyTcpSocket)
        {
            ui->setupUi(this);
        }
        
        MyTcpSocket::~MyTcpSocket()
        {
            delete ui;
        }
        
        void MyTcpSocket::on_cmdinvia_clicked()
        {
            subinviadatotest();
        }
        
        
        void MyTcpSocket::on_cmdconnetti_clicked()
        {
            readyRead();
            doConnect();
        }
        
        //void subinviadato();
        
        
        //MyTcpSocket::MyTcpSocket(QObject *parent) :
        //    QObject(parent)
        //{
        //}
        
        
        void MyTcpSocket::doConnect()
        {
            //socket = new QTcpSocket(this); //NON FUNZIONA
            //QTcpSocket socket;
        
            connect(socket, SIGNAL(connected()),this, SLOT(connected()));
            connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
            //connect(socket, SIGNAL(bytesWritten(1)),this, SLOT(bytesWritten(1)));
            connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
        
            qDebug() << "connecting...";
        
            // this is not blocking call
            socket->connectToHost("192.168.1.26", 88);
        
            // we need to wait...
            if(!socket->waitForConnected(5000))
            {
                qDebug() << "Error: " << socket->errorString();
            }
        }
        
        void MyTcpSocket::connected()
        {
            qDebug() << "connected...";
        
             subinviadatotest();
        
        }
        
        void MyTcpSocket::subinviadatotest()
        {
            //Dichiaro variabili che uso;
            QChar str;
            QString stringa;
            stringa = "{P01-P07-DAT-AXX}";
            long lunghezza;
        
            // (const char *)&str << stringa.at(stringa.length() - 2);
            //qDebug() str;
        
            //Misuro lunghezza stringhe
            lunghezza=stringa.length();
        
            //Visualizzo informazioni
            qDebug("  ---  ");
            QString ss = QString::number(lunghezza);
            qDebug() << ss;
            qDebug("  ---  ");
        
            long i;
            for(i=0;i<lunghezza;i++) {
        
                //if(stringa[i]=='}')
                //    break;
                QChar caratt;
                caratt=stringa[i];
                //qDebug("-");
                //qDebug() << caratt;
                str= caratt; //QChar('{');
                //const char *str = qPrintable(stringa);
        
                socket->write((const char *)&str, 1);
                socket->waitForBytesWritten(1000);
                QThread::msleep(100);
        
            }
        
        
        }
        
        
        
        void MyTcpSocket::disconnected()
        {
            qDebug() << "disconnected...";
        }
        
        void MyTcpSocket::bytesWritten(qint64 bytes)
        {
            qDebug() << bytes << " bytes written...";
        }
        
        void MyTcpSocket::readyRead()
        {
            qDebug() << "reading...";
        
            // read the data from the socket
            qDebug() << socket->readAll();
        }
        
        

        mytcpsocket.ui

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MyTcpSocket</class>
         <widget class="QMainWindow" name="MyTcpSocket">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>800</width>
            <height>600</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MyTcpSocket</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <widget class="QPushButton" name="cmdinvia">
            <property name="geometry">
             <rect>
              <x>600</x>
              <y>490</y>
              <width>181</width>
              <height>29</height>
             </rect>
            </property>
            <property name="text">
             <string>Invia</string>
            </property>
           </widget>
           <widget class="QLineEdit" name="txtinvia">
            <property name="geometry">
             <rect>
              <x>30</x>
              <y>490</y>
              <width>561</width>
              <height>26</height>
             </rect>
            </property>
           </widget>
           <widget class="QTextEdit" name="txtricevuto">
            <property name="geometry">
             <rect>
              <x>30</x>
              <y>60</y>
              <width>751</width>
              <height>421</height>
             </rect>
            </property>
           </widget>
           <widget class="QPushButton" name="cmdconnetti">
            <property name="geometry">
             <rect>
              <x>520</x>
              <y>10</y>
              <width>231</width>
              <height>41</height>
             </rect>
            </property>
            <property name="text">
             <string>Connetti</string>
            </property>
           </widget>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>800</width>
             <height>26</height>
            </rect>
           </property>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
         </widget>
         <resources/>
         <connections/>
        </ui>
        
        

        I use Qt 12.0.2 with 6.6.2 min gw

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @Qt-fa-schifo
        The first thing is to define/explain to us what "but it doesn't work" means exactly.

        What was the problem with

        //socket = new QTcpSocket(this); //NON FUNZIONA
        

        How did it "not work"? As @SGaist says that is needed to create the socket.

        And I suspect you are working from https://www.bogotobogo.com/Qt/Qt5_QTcpSocket_Signals_Slots.php. It helps if you say so.

        There is another Qt example at https://doc.qt.io/qt-6/qtnetwork-fortuneclient-example.html, just so you know. However for now you probably want to stick with one you have started on.

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          Qt fa schifo
          wrote on last edited by
          #5

          what I can't understand is why if I activate socket = new QTcpSocket(this); the program gives me the following error:

          :-1: error: debug/mytcpsocket.o: in function MyTcpSocket::doConnect()': C:........\Qt\esempio3\untitled\mytcpsocket.cpp:47: error: undefined reference to __imp__ZN10QTcpSocketC1EP7QObject'
          :-1: error: collect2.exe: error: ld returned 1 exit status
          :-1: warning: The build directory is not at the same level as the source directory, which could be the reason for the build failure.
          :-1: error: [Makefile.Debug:72: debug/untitled.exe] Error 1

          while if I leave it deactivated the program starts and then crashes.

          it should be the other way around, I don't understand....

          Ronel_qtmasterR JonBJ 2 Replies Last reply
          0
          • Q Qt fa schifo

            what I can't understand is why if I activate socket = new QTcpSocket(this); the program gives me the following error:

            :-1: error: debug/mytcpsocket.o: in function MyTcpSocket::doConnect()': C:........\Qt\esempio3\untitled\mytcpsocket.cpp:47: error: undefined reference to __imp__ZN10QTcpSocketC1EP7QObject'
            :-1: error: collect2.exe: error: ld returned 1 exit status
            :-1: warning: The build directory is not at the same level as the source directory, which could be the reason for the build failure.
            :-1: error: [Makefile.Debug:72: debug/untitled.exe] Error 1

            while if I leave it deactivated the program starts and then crashes.

            it should be the other way around, I don't understand....

            Ronel_qtmasterR Offline
            Ronel_qtmasterR Offline
            Ronel_qtmaster
            wrote on last edited by
            #6

            @Qt-fa-schifo following your code, it seems you don't even understand to basic syntax
            here:
            socket = new QTcpSocket(this); //NON FUNZIONA
            QTcpSocket socket;
            you are creating two different sockets with the same name.
            please when begining you should follow official exemples.Simply follow the link i sent you

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              Qt fa schifo
              wrote on last edited by
              #7

              that was a comment from another example and it was disabled so it doesn't cause the program to not work.
              If I create a new project and paste the bogotobogo examples into it they don't work.
              version incompatibility?

              1 Reply Last reply
              0
              • Q Qt fa schifo

                what I can't understand is why if I activate socket = new QTcpSocket(this); the program gives me the following error:

                :-1: error: debug/mytcpsocket.o: in function MyTcpSocket::doConnect()': C:........\Qt\esempio3\untitled\mytcpsocket.cpp:47: error: undefined reference to __imp__ZN10QTcpSocketC1EP7QObject'
                :-1: error: collect2.exe: error: ld returned 1 exit status
                :-1: warning: The build directory is not at the same level as the source directory, which could be the reason for the build failure.
                :-1: error: [Makefile.Debug:72: debug/untitled.exe] Error 1

                while if I leave it deactivated the program starts and then crashes.

                it should be the other way around, I don't understand....

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #8

                @Qt-fa-schifo
                If you do not have line socket = new QTcpSocket(this); then socket will be uninitialized and your program would be expected to crash.

                You do need socket = new QTcpSocket(this); . The error you show is a linker error. You are either not linking with the right libraries, or possibly the wrong version.

                I see you are MinGW, are you using qmake or cmake?

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  Qt fa schifo
                  wrote on last edited by
                  #9

                  I'm using qmake.

                  in effect with the original libraries

                  #include <QTcpSocket>
                  #include <QAbstractSocket>

                  give me error (file not found), I thought maybe are moved:

                  #include <QtNetwork/QTcpSocket>
                  #include <QtNetwork/QAbstractSocket>

                  so could this is the problem?
                  How do I find the right QTcpSocket and QAbstractSocket?

                  Q 1 Reply Last reply
                  0
                  • Q Qt fa schifo

                    I'm using qmake.

                    in effect with the original libraries

                    #include <QTcpSocket>
                    #include <QAbstractSocket>

                    give me error (file not found), I thought maybe are moved:

                    #include <QtNetwork/QTcpSocket>
                    #include <QtNetwork/QAbstractSocket>

                    so could this is the problem?
                    How do I find the right QTcpSocket and QAbstractSocket?

                    Q Offline
                    Q Offline
                    Qt fa schifo
                    wrote on last edited by
                    #10

                    I changed compiler but the error is still the same...

                    C H 2 Replies Last reply
                    0
                    • Q Qt fa schifo

                      I changed compiler but the error is still the same...

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #11

                      @Qt-fa-schifo In your PRO file do you have:

                      QT += network
                      

                      See Qt Network

                      Q 1 Reply Last reply
                      0
                      • Q Qt fa schifo

                        I changed compiler but the error is still the same...

                        H Offline
                        H Offline
                        Helmut.Jakoby
                        wrote on last edited by
                        #12

                        Hello my dear @Qt-is-shit (translation by @Qt-fa-schifo from Italian).
                        If so, why are you using Qt? And do you think you can get better answers with this nickname?

                        https://www.globalobjects.de/

                        1 Reply Last reply
                        0
                        • C ChrisW67

                          @Qt-fa-schifo In your PRO file do you have:

                          QT += network
                          

                          See Qt Network

                          Q Offline
                          Q Offline
                          Qt fa schifo
                          wrote on last edited by
                          #13

                          @ChrisW67 thank you very very very very very very very very very much

                          JonBJ 1 Reply Last reply
                          0
                          • Q Qt fa schifo

                            @ChrisW67 thank you very very very very very very very very very much

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #14

                            @Qt-fa-schifo
                            Just so you know: if you had showed your .pro from the start or said you were using qmake we could have guessed you were missing QT += network, that is why I asked whether you were qmake or cmake.

                            1 Reply Last reply
                            0
                            • Q Qt fa schifo 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