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. Trouble using Qt Socket Programming [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Trouble using Qt Socket Programming [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 8.4k Views 1 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    Bare in mind, I am a newbie at Qt. My goal is to connect to a server with a certain ip address and wait for data on the port. Once data is available, i want to read the port. Right now alls I am trying to do is create a socket using Qt's programming interface. This is what I have so far.

    @#include <QtGui>
    #include <QtNetwork/QTcpSocket>
    #include <QtNetwork/QAbstractSocket>
    #include "connection.h"
    #include "ui_connection.h"

    // globals
    QTcpSocket tcpSocket;
    QString hostAddress = "127.0.0.1";
    unsigned short port = 4395;

    Connection::Connection(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Connection)
    {
    ui->setupUi(this);

    // connect to host using designated port number
    tcpSocket.connectToHost(hostAddress, port, QIODevice::ReadWrite);
    
    // signals and slots for tcp socket
    connect(tcpSocket, SIGNAL(QTcpSocket::connected()), this, SLOT(socketConnected()));
    connect(tcpSocket, SIGNAL(QTcpSocket::disconnected()), this, SLOT(socketDisconnected()));
    

    }

    Connection::~Connection()
    {
    delete ui;
    }

    void Connection::socketConnected()
    {
    ui->txttcpstate->setText("Connected");
    }

    void Connection::socketDisconnected()
    {
    ui->txttcpstate->setText("Disconnected");
    }@

    After doing this, I get 2 errors in the connect string. Anyone see off the bat what the issue could be? From the tutorials, all i could get from them were, create the socket, connect to host, use connect() with signals and slots to read data. Right now in the above code, alls im seeing is if i am connected or disconnected.

    I feel like this should be simple :/

    Thanks in advance.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Try removing the "QTcpSocket::" prefix from connected() and disconnected() in lines 22 and 23.

      Also, as an aside, IMHO the globals in lines 8-10 are kind of icky. Would probably be better style to make them member variables.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        hmm, i removed them and still the same error. this is the error i am getting, and i believe it has to do with "this" in the connect functions.

        "no matching function for call to 'Connection::connect(QTcpSocket&, const char[13], Connection * const, Qt::connection type'"

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          What does your header for the Connection class look like?

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            vezprog
            wrote on last edited by
            #5

            Heres my header file code.

            @
            #ifndef CONNECTION_H
            #define CONNECTION_H

            #include <QMainWindow>
            #include <QtGui>
            #include <QtNetwork/QAbstractSocket>

            namespace Ui {
            class Connection;
            }

            class Connection : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit Connection(QWidget *parent = 0);
            ~Connection();

            private:
            Ui::Connection *ui;

            public slots:
            void socketConnected();
            };

            #endif // CONNECTION_H
            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              Oh... the TcpSocket in your connect needs to be a pointer. Try:

              @
              connect(&tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected()));
              @

              Edit: Looks like the @ formatting is shoving a semicolon after the tcpSocket for some reason. It shouldn't be there.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vezprog
                wrote on last edited by
                #7

                Oh wow. Thank you very much! I had a feeling it was something simple :)
                I appreciate your time!

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  No problem!

                  If your problem's solved, please edit your original post and change the thread title to "[SOLVED] Trouble using Qt Socket Programming"

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  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