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. Question on QPointer(solved)
Forum Updated to NodeBB v4.3 + New Features

Question on QPointer(solved)

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 898 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by houmingc
    #1

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

    1 Reply Last reply
    0
    • KiwiJeffK Offline
      KiwiJeffK Offline
      KiwiJeff
      wrote on last edited by
      #2

      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();
      }
      
      1 Reply Last reply
      1
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

        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;

        1 Reply Last reply
        0
        • KiwiJeffK Offline
          KiwiJeffK Offline
          KiwiJeff
          wrote on last edited by
          #4

          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.

          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