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

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.5k 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
    Ahti
    wrote on 14 Apr 2017, 14:59 last edited by Ahti
    #1

    I am trying to call a method of QWidget class from a QMainWindow Class but i keep getting the following error:

    Error: no matching function for call to others::constructMessage(...);

    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();
        QMessageBox* constructMessage(QWidget *parent,QMessageBox *message,
                                      int x,int y,int w,int h,QString style) ;
        QMessageBox *errorMessage ;
    };
    
    #endif // OTHERS_H
    
    

    Others.cpp ( as QWidget ) :

    #include "others.h"
    
    others::others(QWidget *parent)
    {
       setupMessage();
    }
    
    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(){
        errorMessage = constructMessage(this,errorMessage,400,400,0,0
                                        ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                         "font: 75 bold 12pt Arial;") ;
    }
    
    

    eyecare.cpp ( as QMainWindow ) :

    #include "eyecare.h"
    #include "others.h"
    #include "ui_eyecare.h"
    #include "QDesktopWidget"
    
    EyeCare::EyeCare(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::EyeCare)
    {    
        ui->setupUi(this);
        QDesktopWidget* widget = qApp->desktop();
        setupSimpleAndErrorMessage() ;
        QRect screenSize = widget->availableGeometry(widget->primaryScreen());
        if ( 0 ){//screenSize.width() > 700 && screenSize.height() > 500)
            setFixedSize(660,460);
        }else{
            errorMessage->setText("Current monitor resolution not supported!");
            errorMessage->show();
        }
     ...
    

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on 14 Apr 2017, 15:02 last edited by
      #2

      Hi @Ahti

      What is the content of Others.h ?

      A 1 Reply Last reply 14 Apr 2017, 17:09
      0
      • M mostefa
        14 Apr 2017, 15:02

        Hi @Ahti

        What is the content of Others.h ?

        A Offline
        A Offline
        Ahti
        wrote on 14 Apr 2017, 17:09 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 14 Apr 2017, 19:18 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 !

          A 1 Reply Last reply 15 Apr 2017, 05:48
          1
          • M mostefa
            14 Apr 2017, 19:18

            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 !

            A Offline
            A Offline
            Ahti
            wrote on 15 Apr 2017, 05:48 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 15 Apr 2017, 06:24
            0
            • A Ahti
              15 Apr 2017, 05:48

              @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 15 Apr 2017, 06:24 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...

              A 1 Reply Last reply 15 Apr 2017, 06:37
              0
              • M mostefa
                15 Apr 2017, 06:24

                @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...

                A Offline
                A Offline
                Ahti
                wrote on 15 Apr 2017, 06:37 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 15 Apr 2017, 06:46
                0
                • A Ahti
                  15 Apr 2017, 06:37

                  @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 15 Apr 2017, 06:46 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 ?

                  A 2 Replies Last reply 15 Apr 2017, 06:49
                  0
                  • M mostefa
                    15 Apr 2017, 06:46

                    @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 ?

                    A Offline
                    A Offline
                    Ahti
                    wrote on 15 Apr 2017, 06:49 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
                      15 Apr 2017, 06:46

                      @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 ?

                      A Offline
                      A Offline
                      Ahti
                      wrote on 15 Apr 2017, 07:04 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 15 Apr 2017, 07:07 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

                        A 1 Reply Last reply 15 Apr 2017, 14:47
                        0
                        • M mostefa
                          15 Apr 2017, 07:07

                          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

                          A Offline
                          A Offline
                          Ahti
                          wrote on 15 Apr 2017, 14:47 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 15 Apr 2017, 15:06
                          0
                          • A Ahti
                            15 Apr 2017, 14:47

                            @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 15 Apr 2017, 15:06 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 ?

                            A 1 Reply Last reply 21 Apr 2017, 07:28
                            0
                            • M mostefa
                              15 Apr 2017, 15:06

                              @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 ?

                              A Offline
                              A Offline
                              Ahti
                              wrote on 21 Apr 2017, 07:28 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

                              2/14

                              14 Apr 2017, 15:02

                              topic:navigator.unread, 12
                              • Login

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