Question on QPointer(solved)



  • #include <QLabel>
    #include <QApplication>
    error: QWidget: Must construct a QApplication before a QWidget

    QPushButton* button11 = new QPushButton("PEC 1");
    int main(int argc, char ** argv)
    {
    QApplication app(argc, argv);
    button11->text("Hello");
    return app.exec();
    }

    Will the below code resolve the error?
    ---------------------------- My remedy-----------------------
    QPointer<QPushButton> button11 = new QPushButton("PEC 1");
    int main(int argc, char ** argv)
    {
    QApplication app(argc, argv);
    button11->text("Hello");
    return app.exec();
    }



  • No, the problem is that you create a QWidget before your QApplication is constructed. Try the following:

    QPointer<QPushButton> button11;
    int main(int argc, char ** argv)
    {
        QApplication app(argc, argv);
        button11 = new QPushButton("PEC 1");
        button11->text("Hello");
        return app.exec();
    }


  • Because i am coding in main, i created myslots.h to QObject::connect
    Is it possible to tell compiler that the variable declaration is already declared in main.cpp

    ----------------myslots.h---------------------------
    extern QPointer<QPushButton> button11;



  • I think the thread topic is no longer relevant to your problem and thus maybe you should start a new thread.

    Either way, I am not sure what your goal or use case is that you need to work in main and declare connects in a header and thus use an extern declaration.


Log in to reply
 

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