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. QCommandLineParser inheritance and vector problem.

QCommandLineParser inheritance and vector problem.

Scheduled Pinned Locked Moved Solved General and Desktop
qt c++qtwidgetsinheritance
16 Posts 4 Posters 1.1k 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.
  • K Khamza
    27 Jan 2025, 14:13

    So, i wanted to inherit QCommandLineParser to save all option logic in my own class.
    This is small example:

    #include <QCommandLineParser>
    class CommandLineParser : public QCommandLineParser
    {
    public:
        CommandLineParser()
        {
            QCommandLineOption *option = new QCommandLineOption("Option");
            addOption(*option);
           //^ - WARNING HERE
        }
    private:
        QVector<QCommandLineOption *> options;
    };
    

    This gives me warning but compiles and works fine:

    error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'
    

    But if i change QVector to std::vector or QList it gives no warnings and also works fine.
    I thought maybe problem is that in QCommandLineOption declaration used Q_DISABLE_COPY(QCommandLineOption). But as i said despite this i can use QList.

    So, my question is why using QVector<QCommandLineOption *> options; gives me this warning?

    Thanks in advance.
    P.S using Qt Creator, Qt 5.

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 27 Jan 2025, 14:38 last edited by
    #4

    @Khamza said in QCommandLineParser inheritance and vector problem.:

    QCommandLineOption *option = new QCommandLineOption("Option");

    Any reason you're allocating on the heap?

    Please show how you're calling the base constructor.
    I see no reason why the type of options member would cause this error. Try to rename options - not sure there is a property with this name in QCommandLineParser.

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

    K 1 Reply Last reply 27 Jan 2025, 14:43
    2
    • J jsulm
      27 Jan 2025, 14:38

      @Khamza said in QCommandLineParser inheritance and vector problem.:

      QCommandLineOption *option = new QCommandLineOption("Option");

      Any reason you're allocating on the heap?

      Please show how you're calling the base constructor.
      I see no reason why the type of options member would cause this error. Try to rename options - not sure there is a property with this name in QCommandLineParser.

      K Offline
      K Offline
      Khamza
      wrote on 27 Jan 2025, 14:43 last edited by Khamza
      #5

      @jsulm

      class CommandLineParser : public QCommandLineParser
      {
      public:
          CommandLineParser()
              : QCommandLineParser()
          {
              QCommandLineOption *option = new QCommandLineOption("Option");
              addOption(*option);
          }
      private:
          QVector<QCommandLineOption *> options;
      };
      
      int main(int argc, char** argv) {
          QApplication a(argc, argv);
          MainWindow w;
          
          w.show();
          return a.exec();
      }
      

      Use heap allocation to freely use option in class without many copyings.
      Besides can it be IDE mistake?
      P.S renaming option also didn't help...

      J S 2 Replies Last reply 27 Jan 2025, 20:20
      0
      • K Khamza
        27 Jan 2025, 14:43

        @jsulm

        class CommandLineParser : public QCommandLineParser
        {
        public:
            CommandLineParser()
                : QCommandLineParser()
            {
                QCommandLineOption *option = new QCommandLineOption("Option");
                addOption(*option);
            }
        private:
            QVector<QCommandLineOption *> options;
        };
        
        int main(int argc, char** argv) {
            QApplication a(argc, argv);
            MainWindow w;
            
            w.show();
            return a.exec();
        }
        

        Use heap allocation to freely use option in class without many copyings.
        Besides can it be IDE mistake?
        P.S renaming option also didn't help...

        J Offline
        J Offline
        JonB
        wrote on 27 Jan 2025, 20:20 last edited by
        #6

        @Khamza said in QCommandLineParser inheritance and vector problem.:

        Besides can it be IDE mistake?

        Where are you getting the

        error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'

        from? You say "This gives me warning but compiles and works fine", so I don't really understand. You get this at runtime, or what?

        FWIW: At Qt6 QVector is gone, it just becomes QList. If you say your code works or doesn't warn with QList instead of QVector you could replace and pretend you never saw this issue. :)

        K 1 Reply Last reply 28 Jan 2025, 04:57
        1
        • J JonB
          27 Jan 2025, 20:20

          @Khamza said in QCommandLineParser inheritance and vector problem.:

          Besides can it be IDE mistake?

          Where are you getting the

          error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'

          from? You say "This gives me warning but compiles and works fine", so I don't really understand. You get this at runtime, or what?

          FWIW: At Qt6 QVector is gone, it just becomes QList. If you say your code works or doesn't warn with QList instead of QVector you could replace and pretend you never saw this issue. :)

          K Offline
          K Offline
          Khamza
          wrote on 28 Jan 2025, 04:57 last edited by Khamza
          #7

          @JonB
          option.png

          option2.png

          Red stripes due to git

          J 1 Reply Last reply 28 Jan 2025, 05:58
          0
          • K Khamza
            28 Jan 2025, 04:57

            @JonB
            option.png

            option2.png

            Red stripes due to git

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 28 Jan 2025, 05:58 last edited by
            #8

            @Khamza Please post code as text, not pictures.
            This comes from code model I guess. Does your project compile?

            "Use heap allocation to freely use option in class without many copyings" - why do you think there is less copying if you allocate it on the heap? You are passing it by reference, not pointer.

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

            K 1 Reply Last reply 28 Jan 2025, 06:04
            0
            • J jsulm
              28 Jan 2025, 05:58

              @Khamza Please post code as text, not pictures.
              This comes from code model I guess. Does your project compile?

              "Use heap allocation to freely use option in class without many copyings" - why do you think there is less copying if you allocate it on the heap? You are passing it by reference, not pointer.

              K Offline
              K Offline
              Khamza
              wrote on 28 Jan 2025, 06:04 last edited by Khamza
              #9

              @jsulm Sorry for that. Yes project compiles and runs. And QCommandLineParser functions also work.
              P.S Regarding heap allocation, I realized that I was wrong

              J 1 Reply Last reply 28 Jan 2025, 06:05
              0
              • K Khamza
                28 Jan 2025, 06:04

                @jsulm Sorry for that. Yes project compiles and runs. And QCommandLineParser functions also work.
                P.S Regarding heap allocation, I realized that I was wrong

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 28 Jan 2025, 06:05 last edited by
                #10

                @Khamza Then that error is a false positive from the code model and you can ignore it.

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

                K 1 Reply Last reply 28 Jan 2025, 06:15
                0
                • J jsulm
                  28 Jan 2025, 06:05

                  @Khamza Then that error is a false positive from the code model and you can ignore it.

                  K Offline
                  K Offline
                  Khamza
                  wrote on 28 Jan 2025, 06:15 last edited by Khamza
                  #11

                  @jsulm
                  This

                  class CommandLineParser : public QCommandLineParser
                  {
                  public:
                      CommandLineParser(QApplication &a)
                          : QCommandLineParser()
                      {
                          QCommandLineOption option = QCommandLineOption("option", "this is description", "argument");
                          addHelpOption(); // main.cpp:38:9: error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'
                          addVersionOption(); // same here
                          addOption(option); // same here
                          process(a);
                          qInfo() << "Names: " << optionNames(); // same here
                          qInfo() << "Is set:" << isSet(option) << value(option); // also here
                      }
                  public:
                      QVector<QCommandLineOption *> options;
                  //    QList<QCommandLineOption *> options;
                  };
                  
                  int main(int argc, char** argv) {
                      QApplication a(argc, argv);
                      MainWindow w;
                      CommandLineParser parser(a);
                      w.show();
                      return a.exec();
                  }
                  

                  gives me

                  Names:  ("option")
                  Is set: true "asd"
                  

                  So, as you see works fine. You say it's false positive, but may it be UB?

                  J 1 Reply Last reply 28 Jan 2025, 06:41
                  0
                  • K Khamza
                    28 Jan 2025, 06:15

                    @jsulm
                    This

                    class CommandLineParser : public QCommandLineParser
                    {
                    public:
                        CommandLineParser(QApplication &a)
                            : QCommandLineParser()
                        {
                            QCommandLineOption option = QCommandLineOption("option", "this is description", "argument");
                            addHelpOption(); // main.cpp:38:9: error: cannot initialize object parameter of type 'QCommandLineParser' with an expression of type 'CommandLineParser'
                            addVersionOption(); // same here
                            addOption(option); // same here
                            process(a);
                            qInfo() << "Names: " << optionNames(); // same here
                            qInfo() << "Is set:" << isSet(option) << value(option); // also here
                        }
                    public:
                        QVector<QCommandLineOption *> options;
                    //    QList<QCommandLineOption *> options;
                    };
                    
                    int main(int argc, char** argv) {
                        QApplication a(argc, argv);
                        MainWindow w;
                        CommandLineParser parser(a);
                        w.show();
                        return a.exec();
                    }
                    

                    gives me

                    Names:  ("option")
                    Is set: true "asd"
                    

                    So, as you see works fine. You say it's false positive, but may it be UB?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 28 Jan 2025, 06:41 last edited by
                    #12

                    @Khamza said in QCommandLineParser inheritance and vector problem.:

                    but may it be UB?

                    Do you mean undefined behavior? Why should it be?
                    As I said: this error comes from code model, not from the compiler when you compile your project. Code model can generate false positive errors/warnings. Important is what the compiler says when you compile.

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

                    K 1 Reply Last reply 28 Jan 2025, 06:45
                    1
                    • J jsulm
                      28 Jan 2025, 06:41

                      @Khamza said in QCommandLineParser inheritance and vector problem.:

                      but may it be UB?

                      Do you mean undefined behavior? Why should it be?
                      As I said: this error comes from code model, not from the compiler when you compile your project. Code model can generate false positive errors/warnings. Important is what the compiler says when you compile.

                      K Offline
                      K Offline
                      Khamza
                      wrote on 28 Jan 2025, 06:45 last edited by
                      #13

                      @jsulm
                      Ok, got it. Thanks a lot.
                      P.S
                      So, i thought that that i need my options must "live" when use parser.process(). But internally QLineOptionParser copies all option to QList<QCommandLineOption> commandLineOptionList; so i simply remove that vector...

                      J 1 Reply Last reply 28 Jan 2025, 09:14
                      0
                      • K Khamza has marked this topic as solved on 28 Jan 2025, 06:46
                      • K Khamza
                        27 Jan 2025, 14:43

                        @jsulm

                        class CommandLineParser : public QCommandLineParser
                        {
                        public:
                            CommandLineParser()
                                : QCommandLineParser()
                            {
                                QCommandLineOption *option = new QCommandLineOption("Option");
                                addOption(*option);
                            }
                        private:
                            QVector<QCommandLineOption *> options;
                        };
                        
                        int main(int argc, char** argv) {
                            QApplication a(argc, argv);
                            MainWindow w;
                            
                            w.show();
                            return a.exec();
                        }
                        

                        Use heap allocation to freely use option in class without many copyings.
                        Besides can it be IDE mistake?
                        P.S renaming option also didn't help...

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 28 Jan 2025, 08:34 last edited by
                        #14

                        Hi,

                        @Khamza said in QCommandLineParser inheritance and vector problem.:

                        @jsulm

                        class CommandLineParser : public QCommandLineParser
                        {
                        public:
                            CommandLineParser()
                                : QCommandLineParser()
                            {
                                QCommandLineOption *option = new QCommandLineOption("Option");
                                addOption(*option);
                            }
                        
                        

                        One small side note: there's no need to allocate the option on the heap. You are just adding a layer of indirection for no benefit.

                        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
                        1
                        • K Khamza
                          28 Jan 2025, 06:45

                          @jsulm
                          Ok, got it. Thanks a lot.
                          P.S
                          So, i thought that that i need my options must "live" when use parser.process(). But internally QLineOptionParser copies all option to QList<QCommandLineOption> commandLineOptionList; so i simply remove that vector...

                          J Offline
                          J Offline
                          JonB
                          wrote on 28 Jan 2025, 09:14 last edited by
                          #15

                          @Khamza
                          Just to wrap up.

                          • I think you are going to get rid of the new, so it does not matter now, but be aware that the way you wrote will leak the created QCommandLineOption. You assign to local variable and do not delete it. It would show up if you used e.g. valgrind.

                          • The message you get is only from the code model, and seems inexplicable. Which model are you using? If it's the internal one that no longer gets any attention and might be "off". You might wish to check if you are using the clang code model, it's settable somewhere in settings.

                          • I'm not sure whether the red lines are from your editing or from the code model. If the latter it looks like it's got itself lost all over the place.

                          • If I understand you rightly, you said the "cannot initialize ..." message only occurs if you have QVector<QCommandLineOption *> and not if you make that a QList. That line comes after the error message one and just has no relationship to it, so it's a mystery.

                          • As I wrote earlier, why don't you change over from QVector to QList anyway if that solves it, as that will be better going forward and makes no difference to you now?

                          K 1 Reply Last reply 28 Jan 2025, 19:35
                          1
                          • J JonB
                            28 Jan 2025, 09:14

                            @Khamza
                            Just to wrap up.

                            • I think you are going to get rid of the new, so it does not matter now, but be aware that the way you wrote will leak the created QCommandLineOption. You assign to local variable and do not delete it. It would show up if you used e.g. valgrind.

                            • The message you get is only from the code model, and seems inexplicable. Which model are you using? If it's the internal one that no longer gets any attention and might be "off". You might wish to check if you are using the clang code model, it's settable somewhere in settings.

                            • I'm not sure whether the red lines are from your editing or from the code model. If the latter it looks like it's got itself lost all over the place.

                            • If I understand you rightly, you said the "cannot initialize ..." message only occurs if you have QVector<QCommandLineOption *> and not if you make that a QList. That line comes after the error message one and just has no relationship to it, so it's a mystery.

                            • As I wrote earlier, why don't you change over from QVector to QList anyway if that solves it, as that will be better going forward and makes no difference to you now?

                            K Offline
                            K Offline
                            Khamza
                            wrote on 28 Jan 2025, 19:35 last edited by Khamza
                            #16

                            @JonB
                            Thanks for your response.

                            1. +
                            2. I use built-in model. Yes it's clang
                            3. I thought it's because of git.
                            4. +
                            5. So, i thought that QVector is more preferable than QList. Also i already used QVector in all other code.
                              P.S. Why should i call explicitly QCommandLineParser in CommandLineParser? Isn't it called implicitly?
                            1 Reply Last reply
                            0

                            13/16

                            28 Jan 2025, 06:45

                            • Login

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