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. Iterate over qtcp servers to connect concurrently

Iterate over qtcp servers to connect concurrently

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 257 Views
  • 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.
  • P Offline
    P Offline
    poormohammadf
    wrote on last edited by poormohammadf
    #1

    I want to show data from several servers on the paint layer but didn't manage to do so.
    my code snippet is:
    window.cpp

        int index = -1;
        QMapIterator<QString,int> i(Servers::myMap);
        while (i.hasNext()) {
            i.next();
            index++;
            QThreadPool pool;
            QFuture<void> tr;
            client = new Client;
            client->ConnectToServer(i.key(),i.value());
            layer = new MPaintLayer(mapWidget);
            timer = new QTimer;
            tr = QtConcurrent::run(&pool,add_layer,mapWidget,client,timer,layer);
            timer->start(2000);
            qDebug() << index;
            qDebug() << i.key() << ": " << i.value()<< "\n\n\n";
            tr.waitForFinished();
    

    what's wrong in this approach?
    my add_layer function is:

    QObject::connect(timer,SIGNAL(timeout()),client,SLOT(online_slot()), Qt::QueuedConnection);
    mapWidget->addLayer(layer);
    mapWidget->update();
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @poormohammadf said in iterate over qtcp servers to connect:

      what's wrong in this approach?

      The better question - what do you try to achieve and why not simply do create a QTcpConnection, start a timer and connect to the next one when it times out? Why do you need threads at all here?

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

      1 Reply Last reply
      1
      • P Offline
        P Offline
        poormohammadf
        wrote on last edited by
        #3

        I use Marble widget to show stream data from multiple servers on the map. each connection should add their own layer on the map. therefore each connection should be separated thread.

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

          @poormohammadf said in iterate over qtcp servers to connect:

          therefore each connection should be separated thread.

          This is no reason to move the tcp stuff into an own thread since the QTcpSocket is async. It just doesn't make any sense. Your client is created in the main thread so all the work will be done in the main thread too. If you really want to use threads for whatsoever reason, I would suggest you to read the QThread documentation and examples to understand when something is running in a separate thread (and what you have to take care for) - currently this is just some stuff thrown together in the hope it works without understanding of the basics (and, as I said - completely unneeded for the needs).

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

          1 Reply Last reply
          1

          • Login

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