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. Wrong Implementation of single instance application
Forum Updated to NodeBB v4.3 + New Features

Wrong Implementation of single instance application

Scheduled Pinned Locked Moved Solved General and Desktop
qlocalserverqlocalsocketipcqtsingleapplica
12 Posts 4 Posters 4.1k Views 2 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    in readyRead could you check that mSocket->bytesAvailable() is the same as buffer.size() in hasPrevious?

    "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

    AyushExel204A 1 Reply Last reply
    0
    • VRoninV VRonin

      in readyRead could you check that mSocket->bytesAvailable() is the same as buffer.size() in hasPrevious?

      AyushExel204A Offline
      AyushExel204A Offline
      AyushExel204
      wrote on last edited by
      #3

      @VRonin yes they are same

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

        What's the output of mSocket->readAll() ? could you paste it here?

        P.S.

        playlist->addMedia(QUrl::fromLocalFile(QString(mSocket->readAll())); // no need to use STD string
        
        SingleInstance(QObject *parent = 0, QMediaPlaylist* pl=new QMediaPlaylist /*<- This is horrible and 99% memory leak guaranteed*/);
        

        "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

        AyushExel204A 1 Reply Last reply
        0
        • VRoninV VRonin

          What's the output of mSocket->readAll() ? could you paste it here?

          P.S.

          playlist->addMedia(QUrl::fromLocalFile(QString(mSocket->readAll())); // no need to use STD string
          
          SingleInstance(QObject *parent = 0, QMediaPlaylist* pl=new QMediaPlaylist /*<- This is horrible and 99% memory leak guaranteed*/);
          
          AyushExel204A Offline
          AyushExel204A Offline
          AyushExel204
          wrote on last edited by AyushExel204
          #5

          @VRonin Okay so I selected the 10 audio files and pressed "Enter" and printed the output of mSocket->readAll() . The output is :

          D:\Music\songs\5 second of summer\5 Seconds Of Summer\08 - End Up Here - (www.SongsLover.pk).mp3
          D:\Music\songs\5 second of summer\5 Seconds Of Summer\12 - Amnesia - (www.SongsLover.pk).mp3
          
          
          
          
          
          
          
          D:\Music\songs\5 second of summer\5 Seconds Of Summer\Voodoo Doll.mp3
          

          So it is clear that only 3 files are added in the playlist and the address of other files are just replaced by a new line.

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

            I suspect it has to do with encoding

            try replacing buffer.append(arg); with

            QDataStream out(&buffer,QIODevice::WriteOnly);
            out << arg;
            

            and replace playlist->addMedia(QUrl::fromLocalFile(QString::fromStdString(mSocket->readAll().toStdString()))); with

            const QByteArray buffer=mSocket->readAll();
            QDataStream in(buffer);
            QString argString;
            in >> argString;
            playlist->addMedia(QUrl::fromLocalFile(argString);
            

            and check that argString has all the lines in it

            "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

            AyushExel204A 1 Reply Last reply
            0
            • VRoninV VRonin

              I suspect it has to do with encoding

              try replacing buffer.append(arg); with

              QDataStream out(&buffer,QIODevice::WriteOnly);
              out << arg;
              

              and replace playlist->addMedia(QUrl::fromLocalFile(QString::fromStdString(mSocket->readAll().toStdString()))); with

              const QByteArray buffer=mSocket->readAll();
              QDataStream in(buffer);
              QString argString;
              in >> argString;
              playlist->addMedia(QUrl::fromLocalFile(argString);
              

              and check that argString has all the lines in it

              AyushExel204A Offline
              AyushExel204A Offline
              AyushExel204
              wrote on last edited by AyushExel204
              #7

              @VRonin Okay so now even when I open a single media file , it is not added in the playlist . So, I have reverted back to the original code. I want to add that, sometimes the program works correctly and all media files that I select are added in the playlist but most of the times few of them are not added , instead just a blank line is added in the playlist.

              kshegunovK 1 Reply Last reply
              0
              • AyushExel204A AyushExel204

                @VRonin Okay so now even when I open a single media file , it is not added in the playlist . So, I have reverted back to the original code. I want to add that, sometimes the program works correctly and all media files that I select are added in the playlist but most of the times few of them are not added , instead just a blank line is added in the playlist.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #8

                @AyushExel204

                You're overwriting the socket pointer when multiple connections are pending

                mSocket = mServer.nextPendingConnection();
                connect(mSocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
                

                Then you read from incorrect socket(s):

                playlist->addMedia(QUrl::fromLocalFile(QString::fromStdString(mSocket->readAll().toStdString())));
                

                Try this:

                void SingleInstance::readyRead()
                { 
                    QLocalSocket * socket = qobject_cast<QLocalSocket *>(sender());
                    playlist->addMedia(QUrl::fromLocalFile(socket->readAll()));
                    // ...
                    socket->deleteLater();
                }
                

                Also I don't understand why you insist on converting QString to std::string and then back again??!

                Read and abide by the Qt Code of Conduct

                AyushExel204A 1 Reply Last reply
                3
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  I still think that if the file path contains non-ASCII this implementation will fail as it converts from QString to QByteArray with no checks

                  "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

                  kshegunovK 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    I still think that if the file path contains non-ASCII this implementation will fail as it converts from QString to QByteArray with no checks

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #10

                    @VRonin

                    I still think that if the file path contains non-ASCII this implementation will fail as it converts from QString to QByteArray with no checks

                    Yes, it's quite possible.
                    Actually it turns out the QString is implicitly converted to utf8 and then added to the byte array. So it might actually work.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @AyushExel204

                      You're overwriting the socket pointer when multiple connections are pending

                      mSocket = mServer.nextPendingConnection();
                      connect(mSocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
                      

                      Then you read from incorrect socket(s):

                      playlist->addMedia(QUrl::fromLocalFile(QString::fromStdString(mSocket->readAll().toStdString())));
                      

                      Try this:

                      void SingleInstance::readyRead()
                      { 
                          QLocalSocket * socket = qobject_cast<QLocalSocket *>(sender());
                          playlist->addMedia(QUrl::fromLocalFile(socket->readAll()));
                          // ...
                          socket->deleteLater();
                      }
                      

                      Also I don't understand why you insist on converting QString to std::string and then back again??!

                      AyushExel204A Offline
                      AyushExel204A Offline
                      AyushExel204
                      wrote on last edited by AyushExel204
                      #11

                      @kshegunov said:

                      QLocalSocket * socket = qobject_cast<QLocalSocket *>(sender());
                      playlist->addMedia(QUrl::fromLocalFile(socket->readAll()));

                      Thanks .. this fixed my problem . but can you explain what this code does ?

                      1 Reply Last reply
                      0
                      • jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        sender() returns the pointer to the object which emitted the signal (http://doc.qt.io/qt-5.6/qobject.html#sender).
                        In this case it is a pointer to QLocalSocket, so you need to cast it from QObject* to QLocalSocket* using qobject_cast

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        • Login

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