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. Not able to call a method of QWidget class from QMainWindow class
Forum Updated to NodeBB v4.3 + New Features

Not able to call a method of QWidget class from QMainWindow class

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 2 Posters 3.8k 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.
  • M mostefa

    Hi @Ahti

    What is the content of Others.h ?

    AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by
    #3

    @mostefa
    others.h

    #ifndef OTHERS_H
    #define OTHERS_H
    
    #include <QWidget>
    #include <QMessageBox>
    #include <QString>
    
    class others
    {
    public:
        explicit others(QWidget *parent = 0);
    private:
        void setupMessage();
        QMessageBox* constructMessage(QWidget *parent,QMessageBox *message,
                                      int x,int y,int w,int h,QString style) ;
        QMessageBox *errorMessage ;
    };
    
    #endif // OTHERS_H
    
    

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #4

      Hi again @Ahti

      The problem

      error: no matching function for call to ‘others::constructMessage(QWidget* const,...)

      Is that you have a function which need a QWidget* as first param,

      But you in your setup Message() function, you are using this, as first param,(which refer to others.cpp , and others is not a QWidget class) ==> this is the problem.

      What you can do is depending on your need:

      1. Send parent of other to setupMessage() function , then your function will become setupMessage(QWidget* parent);

      Here is my sample

      Others.h

      #ifndef OTHERS_H
      #define OTHERS_H
      
      #include <QWidget>
      
      
      QT_FORWARD_DECLARE_CLASS(QMessageBox)
      QT_FORWARD_DECLARE_CLASS(QString)
      
      
      class others
      {
      public:
          explicit others(QWidget *parent = 0);
      private:
          /**
           * @brief setupMessage
           * @param parent
           */
          void setupMessage(QWidget *parent);
          /**
           * @brief constructMessage
           * @param parent
           * @param message
           * @param x
           * @param y
           * @param w
           * @param h
           * @param style
           * @return
           */
          QMessageBox* constructMessage(QWidget *parent, QMessageBox *message,
                                        int x, int y, int w, int h, QString style) ;
          /**
           * @brief errorMessage
           */
          QMessageBox *errorMessage ;
      };
      
      #endif // OTHERS_H
      

      Others.cpp

      #include "others.h"
      #include <QMessageBox>
      
      /**
       * @brief others::others
       * @param parent
       */
      others::others(QWidget *parent)
      {
         setupMessage(parent);
      }
      
      /**
       * @brief others::constructMessage
       * @param parent
       * @param message
       * @param x
       * @param y
       * @param w
       * @param h
       * @param style
       * @return
       */
      QMessageBox* others::constructMessage(QWidget *parent,QMessageBox *message,
                                        int x,int y,int w,int h,
                                        QString style){
          message = new QMessageBox(parent) ;
          message->setGeometry(x,y,w,h);
          message->setStyleSheet(style);
          return message ;
      }
      
      
      /**
       * @brief others::setupMessage
       * @param parent
       */
      void others::setupMessage(QWidget* parent){
          errorMessage = constructMessage(parent,errorMessage,400,400,0,0
                                          ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                           "font: 75 bold 12pt Arial;") ;
      }
      
      1. Or Other class have to inherit from QWidget

      Hope this can help,

      Best Regards !

      AhtiA 1 Reply Last reply
      1
      • M mostefa

        Hi again @Ahti

        The problem

        error: no matching function for call to ‘others::constructMessage(QWidget* const,...)

        Is that you have a function which need a QWidget* as first param,

        But you in your setup Message() function, you are using this, as first param,(which refer to others.cpp , and others is not a QWidget class) ==> this is the problem.

        What you can do is depending on your need:

        1. Send parent of other to setupMessage() function , then your function will become setupMessage(QWidget* parent);

        Here is my sample

        Others.h

        #ifndef OTHERS_H
        #define OTHERS_H
        
        #include <QWidget>
        
        
        QT_FORWARD_DECLARE_CLASS(QMessageBox)
        QT_FORWARD_DECLARE_CLASS(QString)
        
        
        class others
        {
        public:
            explicit others(QWidget *parent = 0);
        private:
            /**
             * @brief setupMessage
             * @param parent
             */
            void setupMessage(QWidget *parent);
            /**
             * @brief constructMessage
             * @param parent
             * @param message
             * @param x
             * @param y
             * @param w
             * @param h
             * @param style
             * @return
             */
            QMessageBox* constructMessage(QWidget *parent, QMessageBox *message,
                                          int x, int y, int w, int h, QString style) ;
            /**
             * @brief errorMessage
             */
            QMessageBox *errorMessage ;
        };
        
        #endif // OTHERS_H
        

        Others.cpp

        #include "others.h"
        #include <QMessageBox>
        
        /**
         * @brief others::others
         * @param parent
         */
        others::others(QWidget *parent)
        {
           setupMessage(parent);
        }
        
        /**
         * @brief others::constructMessage
         * @param parent
         * @param message
         * @param x
         * @param y
         * @param w
         * @param h
         * @param style
         * @return
         */
        QMessageBox* others::constructMessage(QWidget *parent,QMessageBox *message,
                                          int x,int y,int w,int h,
                                          QString style){
            message = new QMessageBox(parent) ;
            message->setGeometry(x,y,w,h);
            message->setStyleSheet(style);
            return message ;
        }
        
        
        /**
         * @brief others::setupMessage
         * @param parent
         */
        void others::setupMessage(QWidget* parent){
            errorMessage = constructMessage(parent,errorMessage,400,400,0,0
                                            ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                             "font: 75 bold 12pt Arial;") ;
        }
        
        1. Or Other class have to inherit from QWidget

        Hope this can help,

        Best Regards !

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on last edited by
        #5

        @mostefa

        now i get this error: "The program has unexpectedly finished."

        My codes:

        others.h

        #ifndef OTHERS_H
        #define OTHERS_H
        
        #include <QWidget>
        #include <QMessageBox>
        #include <QString>
        
        class others
        {
        public:
            explicit others(QWidget *parent = 0);
        private:
            void setupMessage(QWidget *parent);
            QMessageBox* constructMessage(QWidget *parent,QMessageBox *message,
                                          int x,int y,int w,int h,QString style) ;
            QMessageBox *errorMessage ;
        };
        
        #endif // OTHERS_H
        
        

        others.cpp

        #include "others.h"
        
        others::others(QWidget *parent)
        {
           setupMessage(parent);
        }
        
        QMessageBox* others::constructMessage(QWidget *parent,QMessageBox *message,
                                          int x,int y,int w,int h,
                                          QString style){
            message = new QMessageBox(parent) ;
            message->setGeometry(x,y,w,h);
            message->setStyleSheet(style);
            return message ;
        }
        
        void others::setupMessage(QWidget *parent){
            errorMessage = constructMessage(parent,errorMessage,400,400,0,0
                                            ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                             "font: 75 bold 12pt Arial;") ;
        }
        
        

        what is a signature ?? Lol

        M 1 Reply Last reply
        0
        • AhtiA Ahti

          @mostefa

          now i get this error: "The program has unexpectedly finished."

          My codes:

          others.h

          #ifndef OTHERS_H
          #define OTHERS_H
          
          #include <QWidget>
          #include <QMessageBox>
          #include <QString>
          
          class others
          {
          public:
              explicit others(QWidget *parent = 0);
          private:
              void setupMessage(QWidget *parent);
              QMessageBox* constructMessage(QWidget *parent,QMessageBox *message,
                                            int x,int y,int w,int h,QString style) ;
              QMessageBox *errorMessage ;
          };
          
          #endif // OTHERS_H
          
          

          others.cpp

          #include "others.h"
          
          others::others(QWidget *parent)
          {
             setupMessage(parent);
          }
          
          QMessageBox* others::constructMessage(QWidget *parent,QMessageBox *message,
                                            int x,int y,int w,int h,
                                            QString style){
              message = new QMessageBox(parent) ;
              message->setGeometry(x,y,w,h);
              message->setStyleSheet(style);
              return message ;
          }
          
          void others::setupMessage(QWidget *parent){
              errorMessage = constructMessage(parent,errorMessage,400,400,0,0
                                              ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                               "font: 75 bold 12pt Arial;") ;
          }
          
          
          M Offline
          M Offline
          mostefa
          wrote on last edited by
          #6

          @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

          @mostefa

          now i get this error: "The program has unexpectedly finished."

          The source of this error is not others class , i don't know what you are doing in the other part of your code, you can run the program again with debugger by using F5 , and see what line is causing the problem,

          If you want you can share other aprt of your code...

          AhtiA 1 Reply Last reply
          0
          • M mostefa

            @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

            @mostefa

            now i get this error: "The program has unexpectedly finished."

            The source of this error is not others class , i don't know what you are doing in the other part of your code, you can run the program again with debugger by using F5 , and see what line is causing the problem,

            If you want you can share other aprt of your code...

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by Ahti
            #7

            @mostefa well i have already shared it with u check the "eyecare.cpp" class that i posted in the question

            and here is the "main.cpp"

            #include "eyecare.h"
            #include "others.h"
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication::setStyle(QStyleFactory::create("Fusion")) ;
                QApplication app(argc, argv);
            
                others win1;
                EyeCare win2;
                win2.setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint) ;
                win2.showNormal();
                return app.exec();
            }
            
            

            and when i debug my program i get the message saying something like this "The program has terminated because it got a message from the operating system"

            what is a signature ?? Lol

            M 1 Reply Last reply
            0
            • AhtiA Ahti

              @mostefa well i have already shared it with u check the "eyecare.cpp" class that i posted in the question

              and here is the "main.cpp"

              #include "eyecare.h"
              #include "others.h"
              #include <QApplication>
              
              int main(int argc, char *argv[])
              {
                  QApplication::setStyle(QStyleFactory::create("Fusion")) ;
                  QApplication app(argc, argv);
              
                  others win1;
                  EyeCare win2;
                  win2.setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint) ;
                  win2.showNormal();
                  return app.exec();
              }
              
              

              and when i debug my program i get the message saying something like this "The program has terminated because it got a message from the operating system"

              M Offline
              M Offline
              mostefa
              wrote on last edited by
              #8

              @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

              @mostefa well i have already shared it with u check the "eyecare.cpp" class that i posted in the question

              and here is the "main.cpp"

              #include "eyecare.h"
              #include "others.h"
              #include <QApplication>
              
              int main(int argc, char *argv[])
              {
                  QApplication::setStyle(QStyleFactory::create("Fusion")) ;
                  QApplication app(argc, argv);
              
                  others win1;
                  EyeCare win2;
                  win2.setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint) ;
                  win2.showNormal();
                  return app.exec();
              }
              
              

              and when i debug my program i get the message saying something like this "The program has terminated because it got a message from the operating system"

              In eyecare.cpp

              you have this line

              errorMessage->setText("Current monitor resolution not supported!");
              

              But looks like errorMessage is pointer that is never initialized before ?

              AhtiA 2 Replies Last reply
              0
              • M mostefa

                @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

                @mostefa well i have already shared it with u check the "eyecare.cpp" class that i posted in the question

                and here is the "main.cpp"

                #include "eyecare.h"
                #include "others.h"
                #include <QApplication>
                
                int main(int argc, char *argv[])
                {
                    QApplication::setStyle(QStyleFactory::create("Fusion")) ;
                    QApplication app(argc, argv);
                
                    others win1;
                    EyeCare win2;
                    win2.setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint) ;
                    win2.showNormal();
                    return app.exec();
                }
                
                

                and when i debug my program i get the message saying something like this "The program has terminated because it got a message from the operating system"

                In eyecare.cpp

                you have this line

                errorMessage->setText("Current monitor resolution not supported!");
                

                But looks like errorMessage is pointer that is never initialized before ?

                AhtiA Offline
                AhtiA Offline
                Ahti
                wrote on last edited by Ahti
                #9

                @mostefa
                that is initialized in the others.cpp itself in the setupMessage() function.

                ...
                void others::setupMessage(QWidget *parent){
                    errorMessage = constructMessage(parent,errorMessage,400,400,0,0
                                                    ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                                     "font: 75 bold 12pt Arial;") ;
                }
                

                what is a signature ?? Lol

                1 Reply Last reply
                0
                • M mostefa

                  @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

                  @mostefa well i have already shared it with u check the "eyecare.cpp" class that i posted in the question

                  and here is the "main.cpp"

                  #include "eyecare.h"
                  #include "others.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication::setStyle(QStyleFactory::create("Fusion")) ;
                      QApplication app(argc, argv);
                  
                      others win1;
                      EyeCare win2;
                      win2.setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint) ;
                      win2.showNormal();
                      return app.exec();
                  }
                  
                  

                  and when i debug my program i get the message saying something like this "The program has terminated because it got a message from the operating system"

                  In eyecare.cpp

                  you have this line

                  errorMessage->setText("Current monitor resolution not supported!");
                  

                  But looks like errorMessage is pointer that is never initialized before ?

                  AhtiA Offline
                  AhtiA Offline
                  Ahti
                  wrote on last edited by
                  #10

                  @mostefa and in main.cpp i am first creating an object of "others" class ( before eyecare class ) which would call the constructor of others class and which in turn would initialize the errorMessage pointer in the setupMessage() function itself.

                  what is a signature ?? Lol

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mostefa
                    wrote on last edited by mostefa
                    #11

                    What is the content of eyecare.h ....,

                    and the content of this function setupSimpleAndErrorMessage

                    You need to provide a more consistent details about what are you doing

                    AhtiA 1 Reply Last reply
                    0
                    • M mostefa

                      What is the content of eyecare.h ....,

                      and the content of this function setupSimpleAndErrorMessage

                      You need to provide a more consistent details about what are you doing

                      AhtiA Offline
                      AhtiA Offline
                      Ahti
                      wrote on last edited by Ahti
                      #12

                      @mostefa The debugger shows the problem is with errorMessage itself.

                      I am just trying to display a message when i encounter the resolution is less than 700 * 500.

                      what is a signature ?? Lol

                      M 1 Reply Last reply
                      0
                      • AhtiA Ahti

                        @mostefa The debugger shows the problem is with errorMessage itself.

                        I am just trying to display a message when i encounter the resolution is less than 700 * 500.

                        M Offline
                        M Offline
                        mostefa
                        wrote on last edited by
                        #13

                        @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

                        @mostefa The debugger shows the problem is with errorMessage itself.

                        For me it's normal , cause as i told you before ,if i refer to the part of code that you shared with us i am not seeing where errorMessage is initialized.

                        Do you have var memeber called errorMessage on your eyecare.h ?

                        If yes , and if it's a pointer you need to initialize it,

                        If you are doing something else , can you PLEASE share the full code of your eyecare.h ?

                        AhtiA 1 Reply Last reply
                        0
                        • M mostefa

                          @Ahti said in Not able to call a method of QWidget class from QMainWindow class:

                          @mostefa The debugger shows the problem is with errorMessage itself.

                          For me it's normal , cause as i told you before ,if i refer to the part of code that you shared with us i am not seeing where errorMessage is initialized.

                          Do you have var memeber called errorMessage on your eyecare.h ?

                          If yes , and if it's a pointer you need to initialize it,

                          If you are doing something else , can you PLEASE share the full code of your eyecare.h ?

                          AhtiA Offline
                          AhtiA Offline
                          Ahti
                          wrote on last edited by Ahti
                          #14

                          @mostefa

                          well eyecare.h has a lot of content and errorMessage is not initialized in eyecare its initialized in others itself and no there is no such variable as errorMessage in eyecare.h its in others.h

                          what is a signature ?? Lol

                          1 Reply Last reply
                          0

                          • Login

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