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. Sent extra data by signal and slot of Qt

Sent extra data by signal and slot of Qt

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    @
    void MainWindow::update_data(QStringList const &data)
    {
    int const size = data.size();
    for(int i = 0; i != size; ++i)
    {
    QNetworkReply *reply = theme_get_->request_html(data[i]);
    connect(reply, SIGNAL(finished()), this, SLOT(update_message_number()));
    }
    }

    void MainWindow::update_message_number()
    {
    auto result = theme_get_->read_html(qobject_cast<QNetworkReply *>(sender()) );
    if(!result.first) qDebug() << result.second;

    int const number = parser_.get_anime_live_message_number(result.second);
    anime_live_model_->set_current_message_number(/*index of the row*/, number);
    

    }
    @

    As the source codes show, I need to send the index(i) to the update_message_number()
    but don't know how to achieve by the signal and slot mechanism

    I have one solution, declare a map, then
    @void MainWindow::update_data(QStringList const &data)
    {
    int const size = data.size();
    for(int i = 0; i != size; ++i)
    {
    QNetworkReply *reply = theme_get_->request_html(data[i]);
    connect(reply, SIGNAL(finished()), this, SLOT(update_message_number()));
    mapper.insert(std::make_pair(reply, i) );
    }
    }

    void MainWindow::update_message_number()
    {
    auto result = theme_get_->read_html(qobject_cast<QNetworkReply *>(sender()) );
    if(!result.first) qDebug() << result.second;

    int const number = parser_.get_anime_live_message_number(result.second);
    if(mapper.find(sender()) != mapper.end() )
      anime_live_model_->set_current_message_number(mapper[sender()], number);
    else
      QTimer::single_shot(500, this, SLOT(update_message_number(number ) ) );
    

    }
    @

    This solution is pretty complicated, how could I solve it by a better way?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stereomatching
      wrote on last edited by
      #2

      Quite expect the new signal and slot mechanism of Qt5
      Looks like it is not text-base again and able to co-work
      with bind, function and lambda

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Your possibilites:

        • use QSignalMapper to map a QNetworkReply to an index (and which is also the object-oriented-correct version of sender())
          @
          connect(signalMapper,
          static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped),
          this,
          &MainWindow::update_message_number);

        for (int i = 0; i != size; ++i)
        {
        ...
        signalMapper->setMapping(reply, i);
        connect(reply,
        &QNetworkReply::finished,
        signalMapper,
        static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
        }
        @

        • use a lambda to pass the local i
          @
          for (int i = 0; i != size; i++)
          {
          ...
          connect(reply, &QNetworkReply::finished, this, i { update_message_number(i); });
          }
          @
        1 Reply Last reply
        0
        • S Offline
          S Offline
          stereomatching
          wrote on last edited by
          #4

          Do Qt4.8.2 support this kind of signal and slot mechanism?
          if I could connect the signal and slot by any callable things like std::function
          then I could bind the data directly

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stereomatching
            wrote on last edited by
            #5

            I tried it, looks like Qt4.8.2 do not support it yet?
            Are you using Qt5.0?I think it is still under developing

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              No, the new connection syntax is exclusive to Qt 5. But you can still use the QSignalMapper solution and the ordinary connection syntax.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stereomatching
                wrote on last edited by
                #7

                I tried QSignalMapper, but I still need the original sender--reply
                QSignalMapper could bind int, but I don't know the original sender

                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