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. String Split
Forum Updated to NodeBB v4.3 + New Features

String Split

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 7 Posters 3.0k 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.
  • jondoeJ jondoe

    @aha_1980

    How can I use this with download function ?

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by aha_1980
    #7

    @jondoe

    Try this:

    QString dlls = "test1.dll,test2.dll,test3.dll";
    const QStringList list = dlls.split(',');
    
    for (const QString &name : list) {
      const QString url = QString("http://sitename.com/dlls/") + name;
      const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
      qDebug() << "Downloading" << url << "to" << file << "...";
      HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
    }
    
    

    Qt has to stay free or it will die.

    jondoeJ Cobra91151C 2 Replies Last reply
    2
    • Gojir4G Gojir4

      @jondoe said in String Split:

      @aha_1980

      How can I use this with download function ?

      for (const QString &name : list {
        downloadFile(name);
      }
      
      jondoeJ Offline
      jondoeJ Offline
      jondoe
      wrote on last edited by jondoe
      #8

      @Gojir4

      I tried like this but I got error.

      QString getdlls = "http://sitename/dlls.php";
        QByteArray dllssonuc = QResolveUrl(getdlls);
      
        //qDebug() << "testedek" << dllssonuc;
      
        QString files = dllssonuc;
        const QStringList list = files.split(',');
        for (const QString &name : list) {
          //qDebug() << "deneyelim" << name;
      
            if (name != "")
            {
          HRESULT hr;
              LPCTSTR Url = _T("http://sitename.com/dlls/" + name), File = _T("C:\\Users\\user\\Desktop\\testklasor\\" + name);
              hr = URLDownloadToFile (0, Url, File, 0, 0);
              switch (hr)
                  {
                      case S_OK:
                          qDebug() << "Successful download\n";
                          break;
                      case E_OUTOFMEMORY:
                          qDebug() << "Out of memory error\n";
                          break;
                      case INET_E_DOWNLOAD_FAILURE:
                          qDebug() << "Cannot access server data\n";
                          break;
                      default:
                          qDebug() << "Unknown error\n";
                          break;
                  }
              }
      
      
        }
      
      1 Reply Last reply
      0
      • aha_1980A aha_1980

        @jondoe

        Try this:

        QString dlls = "test1.dll,test2.dll,test3.dll";
        const QStringList list = dlls.split(',');
        
        for (const QString &name : list) {
          const QString url = QString("http://sitename.com/dlls/") + name;
          const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
          qDebug() << "Downloading" << url << "to" << file << "...";
          HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
        }
        
        
        jondoeJ Offline
        jondoeJ Offline
        jondoe
        wrote on last edited by
        #9

        @aha_1980

        Error like this -> invalid conversion from 'const wchar_t*' to 'char' [-fpermissive] LPCTSTR Url

        aha_1980A 1 Reply Last reply
        0
        • T Offline
          T Offline
          TobbY
          wrote on last edited by TobbY
          #10

          Hi,

          you need to do conversion for that. please refer this code if that help.

          Required Libs

          #include <Windows.h>
          #include <urlmon.h>
          #include <Wininet.h>
          #pragma comment(lib, "urlmon.lib")
          #pragma comment(lib,"wininet.lib")
          

          To Query DLLs

              QString data = "test1.dll,test2.dll,test3.dll";
              QStringList list = data.split(",");
              for (const QString &name : list ) {
                  if (name != "") downloadFile(name);
              }
          

          To Download DLL

          void MainWindow::downloadFile(QString name)
          {
              HRESULT hr;
              LPCTSTR Url = (LPTSTR)QString("http://sitename.com/dlls/" + name).utf16(); // Convert QString to LPCTSTR 
              LPCTSTR File = (LPTSTR)QString("C:\\Users\\user\\Desktop\\testklasor\\" + name).utf16();
              hr = URLDownloadToFile (0, Url, File, 0, 0);
              switch (hr)
              {
              case S_OK:
                  qDebug() << "Successful download\n";
                  break;
              case E_OUTOFMEMORY:
                  qDebug() << "Out of memory error\n";
                  break;
              case INET_E_DOWNLOAD_FAILURE:
                  qDebug() << "Cannot access server data\n";
                  break;
              default:
                  qDebug() << "Unknown error\n" << hr;
                  break;
              }
          }
          
          jondoeJ 1 Reply Last reply
          1
          • T TobbY

            Hi,

            you need to do conversion for that. please refer this code if that help.

            Required Libs

            #include <Windows.h>
            #include <urlmon.h>
            #include <Wininet.h>
            #pragma comment(lib, "urlmon.lib")
            #pragma comment(lib,"wininet.lib")
            

            To Query DLLs

                QString data = "test1.dll,test2.dll,test3.dll";
                QStringList list = data.split(",");
                for (const QString &name : list ) {
                    if (name != "") downloadFile(name);
                }
            

            To Download DLL

            void MainWindow::downloadFile(QString name)
            {
                HRESULT hr;
                LPCTSTR Url = (LPTSTR)QString("http://sitename.com/dlls/" + name).utf16(); // Convert QString to LPCTSTR 
                LPCTSTR File = (LPTSTR)QString("C:\\Users\\user\\Desktop\\testklasor\\" + name).utf16();
                hr = URLDownloadToFile (0, Url, File, 0, 0);
                switch (hr)
                {
                case S_OK:
                    qDebug() << "Successful download\n";
                    break;
                case E_OUTOFMEMORY:
                    qDebug() << "Out of memory error\n";
                    break;
                case INET_E_DOWNLOAD_FAILURE:
                    qDebug() << "Cannot access server data\n";
                    break;
                default:
                    qDebug() << "Unknown error\n" << hr;
                    break;
                }
            }
            
            jondoeJ Offline
            jondoeJ Offline
            jondoe
            wrote on last edited by
            #11

            @TobbY

            Thank you for answer but debug like this.

            Unknown error
            -2146697211
            Unknown error
            -2146697211

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TobbY
              wrote on last edited by
              #12

              @TobbY said in String Split:

              URLDownloadToFile

              Hi,
              Have you tried it with correct url and dlls ?

              jondoeJ 1 Reply Last reply
              0
              • T TobbY

                @TobbY said in String Split:

                URLDownloadToFile

                Hi,
                Have you tried it with correct url and dlls ?

                jondoeJ Offline
                jondoeJ Offline
                jondoe
                wrote on last edited by
                #13

                @TobbY yes but still same error.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TobbY
                  wrote on last edited by TobbY
                  #14

                  Hi,
                  Please check error code. INET_E_RESOURCE_NOT_FOUND (0x800C0005L or -2146697211)

                  https://msdn.microsoft.com/en-us/library/bb268233(v=vs.85).aspx

                  1 Reply Last reply
                  0
                  • jondoeJ jondoe

                    @aha_1980

                    Error like this -> invalid conversion from 'const wchar_t*' to 'char' [-fpermissive] LPCTSTR Url

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @jondoe if you don't copy my code 1:1, you'll have to fix yourself.

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    2
                    • aha_1980A aha_1980

                      @jondoe

                      Try this:

                      QString dlls = "test1.dll,test2.dll,test3.dll";
                      const QStringList list = dlls.split(',');
                      
                      for (const QString &name : list) {
                        const QString url = QString("http://sitename.com/dlls/") + name;
                        const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
                        qDebug() << "Downloading" << url << "to" << file << "...";
                        HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
                      }
                      
                      
                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #16

                      @aha_1980 said in String Split:

                      @jondoe

                      Try this:

                      QString dlls = "test1.dll,test2.dll,test3.dll";
                      const QStringList list = dlls.split(',');
                      
                      for (const QString &name : list {
                        const QString url = QString("http://sitename.com/dlls/") + name;
                        const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
                        qDebug() << "Downloading" << url << "to" << file << "...";
                        HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
                      }
                      
                      

                      Hi! You forgot closing ) in the for loop)).

                      aha_1980A 1 Reply Last reply
                      1
                      • Cobra91151C Cobra91151

                        @aha_1980 said in String Split:

                        @jondoe

                        Try this:

                        QString dlls = "test1.dll,test2.dll,test3.dll";
                        const QStringList list = dlls.split(',');
                        
                        for (const QString &name : list {
                          const QString url = QString("http://sitename.com/dlls/") + name;
                          const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
                          qDebug() << "Downloading" << url << "to" << file << "...";
                          HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
                        }
                        
                        

                        Hi! You forgot closing ) in the for loop)).

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        @Cobra91151 said in String Split:

                        Hi! You forgot closing ) in the for loop)).

                        Psst. Don't tell anyone. It's part of the homework ;)

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #18

                          If you are using Qt I don't see why you have to complicate your life using LPCTSTR and stuff like that.

                          const QString dlls = "test1.dll,test2.dll,test3.dll";
                          const QStringList list = dlls.split(',');
                          QEventLoop waitLoop;
                          int dllsToDownLoad =  list.size();
                          auto manager = new QNetworkAccessManager;
                          for (const QString &name : list){
                              auto destinationFile = new QFile("C:/Users/user/Desktop/testfile/" + name);
                              if (!destinationFile.open(QFile::WriteOnly)){
                                  --dllsToDownLoad ;
                                  continue;
                              }
                              QNetworkReply* reply = manager->get(QNetworkRequest(QUrl::fromUserInput("http://sitename.com/dlls/" + name)));
                              QObject::connect(reply, &QNetworkReply::readyRead(), [destinationFile, reply]() -> void {
                                  destinationFile->write(reply->readAll());
                              });
                              QObject::connect(reply, &QNetworkReply::finished(), [&dllsToDownLoad, reply, &waitLoop]() -> void {
                                  reply->deleteLater();
                                  destinationFile->deleteLater();
                                  if (--dllsToDownLoad == 0){
                                      manager->deleteLater();
                                      waitLoop.quit();
                                  }
                              });
                          }
                          waitLoop.exec();
                          }
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          4

                          • Login

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