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



  • 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();
        }
     ...
    


  • Hi @Ahti

    What is the content of Others.h ?



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


  • 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 !



  • @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;") ;
    }
    
    


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



  • @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"



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



  • @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;") ;
    }
    


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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.