Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. cannot convert argument 1 from 'QWidget *' to 'QObject *'
Forum Update on Monday, May 27th 2025

cannot convert argument 1 from 'QWidget *' to 'QObject *'

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 1.0k 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.
  • A Offline
    A Offline
    Alcor
    wrote on 14 Aug 2023, 02:10 last edited by Chris Kawa
    #1

    Please bear with me and let me explain my problem.

    I have a piece of code that uses QSyntaxHighlighter Class ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter)

    In the code, a class named GlslHighlighter is defined in glslHighlighter.h as follows:

    class GlslHighlighter: public QSyntaxHighlighter
    {
    public:
    
        GlslHighlighter();
        virtual ~GlslHighlighter();
    
        void setDocument(
            QTextDocument *textdoc
        ,   const e_GlslLanguageType language);
    
    .............
    
    }
    

    constructor in glslHighlighter.cpp is as follows:

    GlslHighlighter::GlslHighlighter()
    :   QSyntaxHighlighter(static_cast<QWidget*>(NULL))
    ,   m_language(GLSLT_Common)
    {
    }
    

    Now, the constructor for QSyntaxHighlighter in Qt documentation is defined as follows:

    QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent)
    
    QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent)
    

    ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter-1)

    Even in Qt4 documentation it is as follows:

    QSyntaxHighlighter::QSyntaxHighlighter ( QObject * parent )
    
    QSyntaxHighlighter::QSyntaxHighlighter ( QTextDocument * parent )
    
    QSyntaxHighlighter::QSyntaxHighlighter ( QTextEdit * parent )
    

    (https://het.as.utexas.edu/HET/Software/html/qsyntaxhighlighter.html#QSyntaxHighlighter)

    When I am trying to compile GlslHighlighter.cpp, i get the error message as follows:

    \glslhighlighter.cpp(40): error C2664: 'QSyntaxHighlighter::QSyntaxHighlighter(const QSyntaxHighlighter &)': cannot convert argument 1 from 'QWidget *' to 'QObject *'

    Now, here is my doubt. Isn't argument 1 of the type QWidget* in violation of the definition of the constructor of the class GlslHighlighter? I don't see any way to fix this error without altering the argument type in the definition file ie glslHighlighter.h

    C J 2 Replies Last reply 14 Aug 2023, 03:32
    0
    • A Alcor
      14 Aug 2023, 02:10

      Please bear with me and let me explain my problem.

      I have a piece of code that uses QSyntaxHighlighter Class ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter)

      In the code, a class named GlslHighlighter is defined in glslHighlighter.h as follows:

      class GlslHighlighter: public QSyntaxHighlighter
      {
      public:
      
          GlslHighlighter();
          virtual ~GlslHighlighter();
      
          void setDocument(
              QTextDocument *textdoc
          ,   const e_GlslLanguageType language);
      
      .............
      
      }
      

      constructor in glslHighlighter.cpp is as follows:

      GlslHighlighter::GlslHighlighter()
      :   QSyntaxHighlighter(static_cast<QWidget*>(NULL))
      ,   m_language(GLSLT_Common)
      {
      }
      

      Now, the constructor for QSyntaxHighlighter in Qt documentation is defined as follows:

      QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent)
      
      QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent)
      

      ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter-1)

      Even in Qt4 documentation it is as follows:

      QSyntaxHighlighter::QSyntaxHighlighter ( QObject * parent )
      
      QSyntaxHighlighter::QSyntaxHighlighter ( QTextDocument * parent )
      
      QSyntaxHighlighter::QSyntaxHighlighter ( QTextEdit * parent )
      

      (https://het.as.utexas.edu/HET/Software/html/qsyntaxhighlighter.html#QSyntaxHighlighter)

      When I am trying to compile GlslHighlighter.cpp, i get the error message as follows:

      \glslhighlighter.cpp(40): error C2664: 'QSyntaxHighlighter::QSyntaxHighlighter(const QSyntaxHighlighter &)': cannot convert argument 1 from 'QWidget *' to 'QObject *'

      Now, here is my doubt. Isn't argument 1 of the type QWidget* in violation of the definition of the constructor of the class GlslHighlighter? I don't see any way to fix this error without altering the argument type in the definition file ie glslHighlighter.h

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 14 Aug 2023, 06:08 last edited by
      #3

      @Alcor your GlsHighlighter class is set up wrong, from the get go.

      said in cannot convert argument 1 from 'QWidget *' to 'QObject *':

      class GlslHighlighter: public QSyntaxHighlighter
      {
      public:
      GlslHighlighter();
      virtual ~GlslHighlighter();

      void setDocument(
      QTextDocument *textdoc
      , const e_GlslLanguageType language);

      .............
      }

       class GlslHighlighter: public QSyntaxHighlighter
       {
            Q_OBJECT
       
      public:
         GlslHighlighter(QObject *parent = nullptr);
         virtual ~GlslHighlighter();
       
       void setDocument(
           QTextDocument *textdoc
       ,   const e_GlslLanguageType language);
       
       .............
      > }
      

      in *.cpp

      GlslHighlighter::GlslHighlighter(QObject *parent)
      : QSyntaxHighlighter(parent)
      , m_language(GLSLT_Common)
      {
      }
      

      That fixes your issue, you don't need to modify any calling code and the now added Q_Object macro will prevent future pain and misery


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • A Alcor
        14 Aug 2023, 02:10

        Please bear with me and let me explain my problem.

        I have a piece of code that uses QSyntaxHighlighter Class ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter)

        In the code, a class named GlslHighlighter is defined in glslHighlighter.h as follows:

        class GlslHighlighter: public QSyntaxHighlighter
        {
        public:
        
            GlslHighlighter();
            virtual ~GlslHighlighter();
        
            void setDocument(
                QTextDocument *textdoc
            ,   const e_GlslLanguageType language);
        
        .............
        
        }
        

        constructor in glslHighlighter.cpp is as follows:

        GlslHighlighter::GlslHighlighter()
        :   QSyntaxHighlighter(static_cast<QWidget*>(NULL))
        ,   m_language(GLSLT_Common)
        {
        }
        

        Now, the constructor for QSyntaxHighlighter in Qt documentation is defined as follows:

        QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent)
        
        QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent)
        

        ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter-1)

        Even in Qt4 documentation it is as follows:

        QSyntaxHighlighter::QSyntaxHighlighter ( QObject * parent )
        
        QSyntaxHighlighter::QSyntaxHighlighter ( QTextDocument * parent )
        
        QSyntaxHighlighter::QSyntaxHighlighter ( QTextEdit * parent )
        

        (https://het.as.utexas.edu/HET/Software/html/qsyntaxhighlighter.html#QSyntaxHighlighter)

        When I am trying to compile GlslHighlighter.cpp, i get the error message as follows:

        \glslhighlighter.cpp(40): error C2664: 'QSyntaxHighlighter::QSyntaxHighlighter(const QSyntaxHighlighter &)': cannot convert argument 1 from 'QWidget *' to 'QObject *'

        Now, here is my doubt. Isn't argument 1 of the type QWidget* in violation of the definition of the constructor of the class GlslHighlighter? I don't see any way to fix this error without altering the argument type in the definition file ie glslHighlighter.h

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 14 Aug 2023, 03:32 last edited by
        #2

        @Alcor since you pass a nullptr to the base class what's the problem to pass a QObject nullptr?

        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
        • A Alcor
          14 Aug 2023, 02:10

          Please bear with me and let me explain my problem.

          I have a piece of code that uses QSyntaxHighlighter Class ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter)

          In the code, a class named GlslHighlighter is defined in glslHighlighter.h as follows:

          class GlslHighlighter: public QSyntaxHighlighter
          {
          public:
          
              GlslHighlighter();
              virtual ~GlslHighlighter();
          
              void setDocument(
                  QTextDocument *textdoc
              ,   const e_GlslLanguageType language);
          
          .............
          
          }
          

          constructor in glslHighlighter.cpp is as follows:

          GlslHighlighter::GlslHighlighter()
          :   QSyntaxHighlighter(static_cast<QWidget*>(NULL))
          ,   m_language(GLSLT_Common)
          {
          }
          

          Now, the constructor for QSyntaxHighlighter in Qt documentation is defined as follows:

          QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent)
          
          QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent)
          

          ( https://doc.qt.io/qt-5.15/qsyntaxhighlighter.html#QSyntaxHighlighter-1)

          Even in Qt4 documentation it is as follows:

          QSyntaxHighlighter::QSyntaxHighlighter ( QObject * parent )
          
          QSyntaxHighlighter::QSyntaxHighlighter ( QTextDocument * parent )
          
          QSyntaxHighlighter::QSyntaxHighlighter ( QTextEdit * parent )
          

          (https://het.as.utexas.edu/HET/Software/html/qsyntaxhighlighter.html#QSyntaxHighlighter)

          When I am trying to compile GlslHighlighter.cpp, i get the error message as follows:

          \glslhighlighter.cpp(40): error C2664: 'QSyntaxHighlighter::QSyntaxHighlighter(const QSyntaxHighlighter &)': cannot convert argument 1 from 'QWidget *' to 'QObject *'

          Now, here is my doubt. Isn't argument 1 of the type QWidget* in violation of the definition of the constructor of the class GlslHighlighter? I don't see any way to fix this error without altering the argument type in the definition file ie glslHighlighter.h

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 14 Aug 2023, 06:08 last edited by
          #3

          @Alcor your GlsHighlighter class is set up wrong, from the get go.

          said in cannot convert argument 1 from 'QWidget *' to 'QObject *':

          class GlslHighlighter: public QSyntaxHighlighter
          {
          public:
          GlslHighlighter();
          virtual ~GlslHighlighter();

          void setDocument(
          QTextDocument *textdoc
          , const e_GlslLanguageType language);

          .............
          }

           class GlslHighlighter: public QSyntaxHighlighter
           {
                Q_OBJECT
           
          public:
             GlslHighlighter(QObject *parent = nullptr);
             virtual ~GlslHighlighter();
           
           void setDocument(
               QTextDocument *textdoc
           ,   const e_GlslLanguageType language);
           
           .............
          > }
          

          in *.cpp

          GlslHighlighter::GlslHighlighter(QObject *parent)
          : QSyntaxHighlighter(parent)
          , m_language(GLSLT_Common)
          {
          }
          

          That fixes your issue, you don't need to modify any calling code and the now added Q_Object macro will prevent future pain and misery


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • A Offline
            A Offline
            Alcor
            wrote on 14 Aug 2023, 12:50 last edited by
            #4

            @J-Hilk said in cannot convert argument 1 from 'QWidget *' to 'QObject *':

            QObject *parent

            Thank You. It worked.

            1 Reply Last reply
            1
            • SGaistS SGaist has marked this topic as solved on 14 Aug 2023, 17:59

            1/4

            14 Aug 2023, 02:10

            • Login

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