Help me
-
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
-
@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
-
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.
-
@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.
-
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 1while if I leave it deactivated the program starts and then crashes.
it should be the other way around, I don't understand....
-
@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 -
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? -
@Qt-fa-schifo
If you do not have linesocket = new QTcpSocket(this);
thensocket
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?
-
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? -
I changed compiler but the error is still the same...
-
-
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? -
@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 missingQT += network
, that is why I asked whether you were qmake or cmake. -