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. Error when using simple API
Forum Updated to NodeBB v4.3 + New Features

Error when using simple API

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 655 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.
  • A Offline
    A Offline
    Alfie Anil
    wrote on last edited by
    #1

    Hey everyone,
    I'm just playing around with a simple API and trying to connect it to qt for a little project. I've written the same code before, but instead of just a function, I had it in a pushButton function.
    I'm getting an error saying:

    terminate called after throwing an instance of 'std::out_of_range'
    what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

    This is the code:

    void Widget::connectAPI()
    {
        qDebug() << "Beginning to connect";
        QNetworkAccessManager *man = new QNetworkAccessManager(this);
        connect(man, &QNetworkAccessManager::finished, this, &Widget::imageDownloadFinished);
        QUrl url = QUrl(imgUrl);
        QNetworkRequest request(url);
        man->get(request);
        qDebug() << "Finished Connection1";
    }
    
    
    void Widget::imageDownloadFinished(QNetworkReply *reply)
    {
        qDebug() << "Finished Connection";
        QString replynet = reply->readAll();
        std::string replynetstd = replynet.toUtf8().constData();
        replynetstd.replace(0, 11, "");
        splitter(replynetstd);
    
        qDebug() << "Beginning image function";
    
    
    }
    

    So when I did it before (and it worked), connectAPI() was void::void Widget::on_pushButton_clicked();

    Thanks in advance for the help

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

      @Alfie-Anil said in Error when using simple API:

      replynetstd.replace(0, 11, "");

      you should make sure that your string is long enough...

      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
      0
      • A Offline
        A Offline
        Alfie Anil
        wrote on last edited by
        #3

        @Christian-Ehrlicher Sorry should have mentioned this before but the program stops after the connectAPI() ends. e.g. the final output is "Finished Connection"

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

          Apart from the fact that you create a new QNetworkManager instance every time for no reason (and leaking it) I don't see why it should stop there. Therefore use a debugger and see where it really asserts.

          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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            That because reply in that function is an invalid pointer. You do not assign it the return value of your get call.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Alfie Anil
              wrote on last edited by
              #6

              hi @SGaist I'm not completely sure what you mean. Do you think you could draft what the code should look like?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You have a class member called reply, correct ?
                Where in your code do you assign it something valid ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Alfie Anil
                  wrote on last edited by Alfie Anil
                  #8

                  Hey @SGaist Thanks for the help.
                  Unfortunately after some more testing I found that the problem was not there. The problem is that later in the function, I convert the bytearray into a QPixmap and add it to a vector. And in the widget.cpp file I use label->setPixmap(imagevector.at(0)).
                  The problem with this is that the label and buttons have all been loaded before the function finishes. So do you know if there is a way to get around that please?
                  Would a sleep function be the most sufficient and efficient way? Thanks

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

                    @Alfie-Anil said in Error when using simple API:

                    The problem with this is that the label and buttons have all been loaded before the function finishes.

                    What do you mean with this? Properly initialize your members in the ctor (e.g. with nullptr) and all is fine.

                    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