0

I get the following error:

QWidget: must construct a QApplication before a widget

It worked 3 days ago, but now it doesn't work anymore, and I don't know why.

What does this error mean? I don't know if I modified something.

Here is my code:

mainwindw.cpp

void MainWindow::on_Statistic_button_clicked(){
    s1 =new stat1(this);
    s1->show();
}

stat1.cpp

stat1::stat1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::stat1)
{
    ui->setupUi(this);
    //mLayout = new QVBoxLayout;
    mChart = new OpenChart(this);
    setCentralWidget(mChart);
    //mLayout->addWidget(mChart);
    //mLayout->setMargin(0);
    //setLayout(mLayout);
    //int num;
    QSqlQuery q,q1,q2;
    q1.exec("SELECT count(distinct adresse) from abonne");
    q1.next();
    int n = q1.value(0).toInt();
    QString adr[100];

    int j = 0;
    q.exec("SELECT distinct adresse from abonne");
    while(j<n)
    {
        q.next();
        adr[j]=q.value(0).toString();
        j++;
    }


    QMap<QString,int> cartes;
    for(j=0;j<n;j++)
    {
        q2.exec("SELECT count(abonne.id_abonne) FROM abonne,fidels where abonne.id_abonne=fidels.id_abonne and abonne.adresse='"+adr[j]+"'");
        q2.next();
        int nbr = q2.value(0).toInt();
        cartes[adr[j]] = nbr;
    }


    mChart->setTitle("Cartes/Adresses");
    mChart->setTipo(OpenChart::Sectores_2D);
    mChart->setTipoleyenda(OpenChart::Circular);
    const auto cartesEnd=cartes.end();
    for(auto i=cartes.begin(); i!=cartesEnd; ++i){
        mChart->addItem(i.key(), i.value());
    }
}

stat1::~stat1()
{
    delete ui;
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Omar Krichen
  • 163
  • 1
  • 2
  • 12
  • The error means that you need to create an object of type `QApplication` before you create an object of a type that is or inherits from `QWidget`. The recommended way to do it is to create a `QApplication` on the first line of your `main` function. Also, you can't create widgets as global variables or inside the constructor of a global variable since global variables are created and their constructors are called before entering the `main` function. If you really need a widget as a global variable, create a pointer to it initially equal to `NULL`, then create the widget in the `main` function. – Donald Duck Dec 06 '17 at 16:45
  • Also, you might be interested in https://stackoverflow.com/q/21590421/4284627. – Donald Duck Dec 06 '17 at 16:56
  • @Donald Duck i m working in project qt Application with widgets – Omar Krichen Dec 06 '17 at 17:05
  • @DonaldDuck can you help me please ? it doesn t work – Omar Krichen Dec 10 '17 at 16:22
  • @​OmarKrichen You haven't provided enough information for me to solve your problem. Please read how to create a [mcve]. All I can say now is that you need to create a `QApplication` object at the beginning of your `main` function and that you can't have any global or static variables of any type that is or inherits from `QWidget`, and you can't create any such objects in the constructors of any class that instantiates a global or static variable. – Donald Duck Dec 10 '17 at 16:35

0 Answers0