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. [SOLVED]QList and QTcpSocket*
Qt 6.11 is out! See what's new in the release blog

[SOLVED]QList and QTcpSocket*

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.6k 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.
  • O Offline
    O Offline
    Overflowz
    wrote on last edited by
    #1

    Hi, today I'm trying to write simple multithreaded client/server program, which will save connected client's sockets to QList and then send them automatically some messages. I've done something like this:

    global_objects.h
    [code]#ifndef GLOBAL_OBJECTS_H
    #define GLOBAL_OBJECTS_H
    #include <QList>
    #include <QTcpSocket>

    QList <QTcpSocket*> Clients;

    #endif // GLOBAL_OBJECTS_H[/code]

    server.cpp
    [code]void Server::incomingConnection(int handle)
    {
    foreach(QTcpSocket pSocket, Clients) //can't convert from const int to QTcpSocket
    {
    qDebug() << pSocket;
    }

    mThread *thread = new mThread(handle);
    thread->run();
    

    }[/code]

    I'm always getting this error. When I tried for loop, like this:

    [code]for(int i = 0; i < Clients.size(); i++) {
    Clients.at(i)->socketDescriptor(); //error, -> must point union class or whatever.
    }[/code]

    I don't get it, honestly. Somebody explain me please!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      master of disaster
      wrote on last edited by
      #2

      Try using this code:

      @
      for(int i = 0; i < Clients.size(); i++)
      {
      (Clients[i])->socketDescriptor();
      }
      @

      You should post the whole error message because it's difficult to help you with this information.

      And another thing: Some compilers don't like variables in loop definitions like this:
      @
      for(int i; ...
      @

      You should declare it outside the loop to keep it portable.

      On the other hand - this code is possible:
      @
      for(...)
      {
      int i;
      ...
      }
      @

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Overflowz
        wrote on last edited by
        #3

        Thanks, solved it.

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

          Please be sure and edit the title of the original post to add [Solved]. Thanks!

          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