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. QString replace %U
QtWS25 Last Chance

QString replace %U

Scheduled Pinned Locked Moved Solved General and Desktop
41 Posts 5 Posters 8.6k 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.
  • S Offline
    S Offline
    sonichy
    wrote on 25 Oct 2021, 11:53 last edited by sonichy
    #1

    OpenWith menu is done, open action (for example) should replace gedit %U to gedit filepath.

    QString filepath = "*********";
    QString s = linux_desktop_file.exec;
    s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to  'replace'
    

    替代文字

    替代文字

    https://github.com/sonichy

    J 2 Replies Last reply 25 Oct 2021, 12:00
    0
    • S sonichy
      25 Oct 2021, 11:53

      OpenWith menu is done, open action (for example) should replace gedit %U to gedit filepath.

      QString filepath = "*********";
      QString s = linux_desktop_file.exec;
      s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to  'replace'
      

      替代文字

      替代文字

      J Offline
      J Offline
      JonB
      wrote on 25 Oct 2021, 12:00 last edited by JonB
      #2

      @sonichy
      If you have a question or a problem or an issue please state what it is. There is no question here. Why make people have to guess what you might mean? It involves you in less typing that those who try to answer, so it's hardly arduous.

      S 1 Reply Last reply 25 Oct 2021, 13:51
      3
      • J JonB
        25 Oct 2021, 12:00

        @sonichy
        If you have a question or a problem or an issue please state what it is. There is no question here. Why make people have to guess what you might mean? It involves you in less typing that those who try to answer, so it's hardly arduous.

        S Offline
        S Offline
        sonichy
        wrote on 25 Oct 2021, 13:51 last edited by sonichy
        #3

        @JonB Can not relace "%U", % is an escape String.

        https://github.com/sonichy

        M 1 Reply Last reply 25 Oct 2021, 14:34
        0
        • S sonichy
          25 Oct 2021, 13:51

          @JonB Can not relace "%U", % is an escape String.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 Oct 2021, 14:34 last edited by
          #4

          @sonichy

          Hi
          what type is filepath ?

          this works

          alt text

          S 1 Reply Last reply 25 Oct 2021, 15:26
          2
          • M mrjj
            25 Oct 2021, 14:34

            @sonichy

            Hi
            what type is filepath ?

            this works

            alt text

            S Offline
            S Offline
            sonichy
            wrote on 25 Oct 2021, 15:26 last edited by
            #5

            @mrjj QString

            https://github.com/sonichy

            1 Reply Last reply
            0
            • S sonichy
              25 Oct 2021, 11:53

              OpenWith menu is done, open action (for example) should replace gedit %U to gedit filepath.

              QString filepath = "*********";
              QString s = linux_desktop_file.exec;
              s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to  'replace'
              

              替代文字

              替代文字

              J Offline
              J Offline
              JonB
              wrote on 25 Oct 2021, 17:20 last edited by JonB
              #6

              @sonichy said in QString replace %U:

              s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to  'replace'
              

              This has nothing to do with the %U part.

              You show a warning in the code editor. That depends on what Code Model you use. Show your line actually generating an error from the compiler when you build. If it compiles, there is nothing wrong with it.

              S 1 Reply Last reply 26 Oct 2021, 04:13
              1
              • J JonB
                25 Oct 2021, 17:20

                @sonichy said in QString replace %U:

                s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to  'replace'
                

                This has nothing to do with the %U part.

                You show a warning in the code editor. That depends on what Code Model you use. Show your line actually generating an error from the compiler when you build. If it compiles, there is nothing wrong with it.

                S Offline
                S Offline
                sonichy
                wrote on 26 Oct 2021, 04:13 last edited by sonichy
                #7

                @JonB
                mainwindow.cpp: In lambda function:
                mainwindow.cpp:379:79: error: passing ‘const QString’ as ‘this’ argument discards qualifiers [-fpermissive]
                sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);

                https://github.com/sonichy

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sonichy
                  wrote on 26 Oct 2021, 04:48 last edited by sonichy
                  #8

                  Change connect lambda [=] to [&], error disappare, what is the difference ?
                  New problem: sExec always run the last whatever I click the first, second, third !

                  "deepin-draw %U"
                  "/usr/bin/display-im6.q16 -nostdin %F"
                  "deepin-album %F"
                  "/usr/bin/google-chrome-stable %U"
                  "deepin-image-viewer --new-window %F"
                  
                  "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
                  

                  https://github.com/sonichy

                  E 2 Replies Last reply 26 Oct 2021, 04:58
                  0
                  • S sonichy
                    26 Oct 2021, 04:48

                    Change connect lambda [=] to [&], error disappare, what is the difference ?
                    New problem: sExec always run the last whatever I click the first, second, third !

                    "deepin-draw %U"
                    "/usr/bin/display-im6.q16 -nostdin %F"
                    "deepin-album %F"
                    "/usr/bin/google-chrome-stable %U"
                    "deepin-image-viewer --new-window %F"
                    
                    "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
                    
                    E Offline
                    E Offline
                    eyllanesc
                    wrote on 26 Oct 2021, 04:58 last edited by eyllanesc
                    #9

                    @sonichy Read the official docs: https://en.cppreference.com/w/cpp/language/lambda, in special Lambda capture section.

                    In simple words: with = you are passing values by copy, instead with & you are passing it as a reference. In your initial case "sExec" inside the lambda is a copy of the original. On the other hand, it is not advisable to use & or = (its can cause bugs), it is better to indicate the variables explicitly: [&sExec]

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    4
                    • S sonichy
                      26 Oct 2021, 04:48

                      Change connect lambda [=] to [&], error disappare, what is the difference ?
                      New problem: sExec always run the last whatever I click the first, second, third !

                      "deepin-draw %U"
                      "/usr/bin/display-im6.q16 -nostdin %F"
                      "deepin-album %F"
                      "/usr/bin/google-chrome-stable %U"
                      "deepin-image-viewer --new-window %F"
                      
                      "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
                      
                      E Offline
                      E Offline
                      eyllanesc
                      wrote on 26 Oct 2021, 06:39 last edited by eyllanesc
                      #10

                      @sonichy I recommend you not to abuse lambdas, a better option is to save the variables in a property to be able to execute it in a slot:

                      QString sExec= ....
                      QString sPath = ....
                      
                      QAction *action = new QAction(...);
                      action->setProperty("sExec", sExec);
                      action->setProperty("sPath", sPath);
                      // TODO
                      
                      connect(action, &QAction::triggered, this, &Foo::handle_triggered);
                      
                      void Foo::handle_triggered(){
                          if(QAction *action = qobject_cast<QAction *>(sender())){
                              QString sExec= action->property("sExec").toString();
                              QString sPath = action->property("sPath").toString();
                              qDebug() << sExec  << sPath;
                          }
                      }
                      

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      S 1 Reply Last reply 26 Oct 2021, 07:44
                      0
                      • E eyllanesc
                        26 Oct 2021, 06:39

                        @sonichy I recommend you not to abuse lambdas, a better option is to save the variables in a property to be able to execute it in a slot:

                        QString sExec= ....
                        QString sPath = ....
                        
                        QAction *action = new QAction(...);
                        action->setProperty("sExec", sExec);
                        action->setProperty("sPath", sPath);
                        // TODO
                        
                        connect(action, &QAction::triggered, this, &Foo::handle_triggered);
                        
                        void Foo::handle_triggered(){
                            if(QAction *action = qobject_cast<QAction *>(sender())){
                                QString sExec= action->property("sExec").toString();
                                QString sPath = action->property("sPath").toString();
                                qDebug() << sExec  << sPath;
                            }
                        }
                        
                        S Offline
                        S Offline
                        sonichy
                        wrote on 26 Oct 2021, 07:44 last edited by
                        #11

                        @eyllanesc Move replace out of lambda work fine.

                        QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
                        sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);
                        sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive);
                        connect(action, &QAction::triggered, [=](){
                            QProcess *process = new QProcess;
                            process->setWorkingDirectory(sPath);
                            qDebug() << sExec;
                            process->startDetached(sExec, QStringList());
                        });
                        

                        https://github.com/sonichy

                        E J 2 Replies Last reply 26 Oct 2021, 07:47
                        0
                        • S sonichy
                          26 Oct 2021, 07:44

                          @eyllanesc Move replace out of lambda work fine.

                          QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
                          sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);
                          sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive);
                          connect(action, &QAction::triggered, [=](){
                              QProcess *process = new QProcess;
                              process->setWorkingDirectory(sPath);
                              qDebug() << sExec;
                              process->startDetached(sExec, QStringList());
                          });
                          
                          E Offline
                          E Offline
                          eyllanesc
                          wrote on 26 Oct 2021, 07:47 last edited by
                          #12

                          @sonichy Change [=] to [sPath, sExec]

                          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                          1 Reply Last reply
                          0
                          • S sonichy
                            26 Oct 2021, 07:44

                            @eyllanesc Move replace out of lambda work fine.

                            QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
                            sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);
                            sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive);
                            connect(action, &QAction::triggered, [=](){
                                QProcess *process = new QProcess;
                                process->setWorkingDirectory(sPath);
                                qDebug() << sExec;
                                process->startDetached(sExec, QStringList());
                            });
                            
                            J Offline
                            J Offline
                            JonB
                            wrote on 26 Oct 2021, 07:53 last edited by
                            #13

                            @sonichy
                            Also you leak the QProcess *process = new QProcess;. Assuming once startDetached() has been called Qt no longer needs the QProcess, try making it a stack instance, you'll have to make sure that works without issue.

                            S 1 Reply Last reply 26 Oct 2021, 07:57
                            0
                            • J JonB
                              26 Oct 2021, 07:53

                              @sonichy
                              Also you leak the QProcess *process = new QProcess;. Assuming once startDetached() has been called Qt no longer needs the QProcess, try making it a stack instance, you'll have to make sure that works without issue.

                              S Offline
                              S Offline
                              sonichy
                              wrote on 26 Oct 2021, 07:57 last edited by sonichy
                              #14

                              @JonB Yes , the process do not start !
                              deepin-draw "/home/sonichy/Desktop/KuGou.png"

                              https://github.com/sonichy

                              J 1 Reply Last reply 26 Oct 2021, 08:00
                              0
                              • S sonichy
                                26 Oct 2021, 07:57

                                @JonB Yes , the process do not start !
                                deepin-draw "/home/sonichy/Desktop/KuGou.png"

                                J Offline
                                J Offline
                                JonB
                                wrote on 26 Oct 2021, 08:00 last edited by JonB
                                #15

                                @sonichy said in QString replace %U:

                                Yes , the process do not start !

                                Under what circumstances? The way your code above is/was? After you have changed to make QProcess a stack variable? Both?

                                Please try to give some clear detail with your posts. You give so little information, we have to keep asking you for more....

                                S 1 Reply Last reply 26 Oct 2021, 08:07
                                1
                                • J JonB
                                  26 Oct 2021, 08:00

                                  @sonichy said in QString replace %U:

                                  Yes , the process do not start !

                                  Under what circumstances? The way your code above is/was? After you have changed to make QProcess a stack variable? Both?

                                  Please try to give some clear detail with your posts. You give so little information, we have to keep asking you for more....

                                  S Offline
                                  S Offline
                                  sonichy
                                  wrote on 26 Oct 2021, 08:07 last edited by
                                  #16

                                  @JonB This is new problem after modify the code, QProcess do not start in lambda !

                                  https://github.com/sonichy

                                  jsulmJ J 2 Replies Last reply 26 Oct 2021, 08:09
                                  0
                                  • S sonichy
                                    26 Oct 2021, 08:07

                                    @JonB This is new problem after modify the code, QProcess do not start in lambda !

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 26 Oct 2021, 08:09 last edited by
                                    #17

                                    @sonichy Would you mind to post the current code, or should we guess what is wrong?

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

                                    S 1 Reply Last reply 26 Oct 2021, 08:24
                                    0
                                    • S sonichy
                                      26 Oct 2021, 08:07

                                      @JonB This is new problem after modify the code, QProcess do not start in lambda !

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 26 Oct 2021, 08:22 last edited by
                                      #18

                                      @sonichy said in QString replace %U:

                                      This is new problem after modify the code

                                      Fair enough, I did say you would have see whether it causes problems. This raises a nasty question of what Qt requires the scope/lifetime of the process used in QProcess::startDetached() to be: it cannot be "forever", because we know detached processes survive even exiting the parent program. Maybe it requires it to exist till at least started() signal has been received, I don't know.

                                      Actually, hang on! The overload of startDetached you are using is bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr). That is static!

                                      Soooo:

                                      • process->setWorkingDirectory(sPath); has no effect (the method signature above takes an optional parameter for the working directory instead).

                                      • Your QProcess *process = new QProcess; is not used.

                                      • You can write QProcess::startDetached(sExec, QStringList()); instead, no QProcess instance needed, and no leaking.

                                      • Given this I find it hard to believe that changing QProcess *process to QProcess process can have any effect....

                                      S 1 Reply Last reply 26 Oct 2021, 08:33
                                      0
                                      • jsulmJ jsulm
                                        26 Oct 2021, 08:09

                                        @sonichy Would you mind to post the current code, or should we guess what is wrong?

                                        S Offline
                                        S Offline
                                        sonichy
                                        wrote on 26 Oct 2021, 08:24 last edited by
                                        #19

                                        @jsulm Click QAction QProcess do not start, copy qDebug() content to terminal can run.

                                        QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
                                        sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);
                                        sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive);
                                        connect(action, &QAction::triggered, [=](){
                                               QProcess *process = new QProcess;
                                               process->setWorkingDirectory(path);
                                               qDebug() << sExec;
                                               //process->start(sExec);
                                               process->setProgram(sExec);
                                               process->startDetached();
                                        });
                                        

                                        https://github.com/sonichy

                                        J 1 Reply Last reply 26 Oct 2021, 08:32
                                        0
                                        • S sonichy
                                          26 Oct 2021, 08:24

                                          @jsulm Click QAction QProcess do not start, copy qDebug() content to terminal can run.

                                          QString sExec = readSettings(desktop, "Desktop Entry", "Exec");
                                          sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive);
                                          sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive);
                                          connect(action, &QAction::triggered, [=](){
                                                 QProcess *process = new QProcess;
                                                 process->setWorkingDirectory(path);
                                                 qDebug() << sExec;
                                                 //process->start(sExec);
                                                 process->setProgram(sExec);
                                                 process->startDetached();
                                          });
                                          
                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 26 Oct 2021, 08:32 last edited by JonB
                                          #20

                                          @sonichy
                                          Is sExec set to the correct full path to the executable? What do you think your process->setWorkingDirectory(path); achieves? Because if you think it is used to locate the executable to run, it is not.

                                          The bool QProcess::startDetached(qint64 *pid = nullptr) overload you are now using has a return value. Do you think you ought look at that if you are having a problem?

                                          1 Reply Last reply
                                          1

                                          5/41

                                          25 Oct 2021, 15:26

                                          topic:navigator.unread, 36
                                          • Login

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