PDA

View Full Version : Segmentation error! Please help!!!



phapha
26th October 2011, 12:01
Hi all,
I'm a newbie in Qt; I've write a little program but I have segmentation error in run. I don't know how to fix it;
Below is my code:

main.cpp



#include <QApplication>
#include "FenetreAccueil.h"

int main(int argc,char* argv[])
{
QApplication app(argc, argv);
FenetreAccueil login;
login.show();
return app.exec();
}


FenetreAccueil.h



#ifndef FENETREACCUEIL_H
#define FENETREACCUEIL_H

#include <QtGui>
//#include <QCoreApplication>
#include <QtSql>
#include "FenetrePrincipale.h"


class FenetreAccueil : public QWidget
{
Q_OBJECT

public:
explicit FenetreAccueil(QWidget *parent = 0);

public slots:
void connectDB();

private:
QPushButton *m_connect;
QPushButton *m_cancel;
// QLineEdit *m_password;
//QLineEdit *m_login;
QLineEdit *m_addressBD;
QLabel *m_label;

};

#endif // FENETREACCUEIL_H



FenetreAccueil.cpp


#include "FenetreAccueil.h"

FenetreAccueil::FenetreAccueil(QWidget *parent) :
QWidget(parent)
{
//Creation du layout de formulaire et ses widgets
QLineEdit *m_login=new QLineEdit;
QLineEdit *m_password=new QLineEdit;
m_password->setEchoMode(QLineEdit::Password);//affichage d'asterisques a la saisie
QLineEdit *m_addressBD=new QLineEdit;//format adresse IP ou pas?

QFormLayout *formAccueil=new QFormLayout;
formAccueil->addRow(tr("&Login"),m_login);
formAccueil->addRow(tr("Password"),m_password);
formAccueil->addRow(tr("mySQL server address"),m_addressBD);

//Creation d'un layout horizontal pour les boutons
m_connect=new QPushButton(tr("Connect"));
m_cancel=new QPushButton(tr("Cancel"));

QHBoxLayout *boutonLayout=new QHBoxLayout;
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);


//Creation d'un layout pour le logo
QLabel *logo=new QLabel(this);
logo->setPixmap(QPixmap("images/grandlogo.png"));

QHBoxLayout *logoLayout=new QHBoxLayout;
logoLayout->addWidget(logo);


//Creation d'un layout pour le message d'acceuil
m_label = new QLabel(tr("Welcome in callTracker"),this);

QHBoxLayout *labelLayout=new QHBoxLayout;
labelLayout->addWidget(m_label);

//Creation du layout principal de la fenetre
QVBoxLayout *layoutPrincipal=new QVBoxLayout;

//Ajout du layout d'accueil
layoutPrincipal->addLayout(labelLayout);

//Ajout du layout de formulaire
layoutPrincipal->addLayout(formAccueil);

//Ajout du layout des boutons
layoutPrincipal->addLayout(boutonLayout);

setLayout(layoutPrincipal);
setWindowTitle(tr("Welcome"));
setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
resize(300,350);

//Generation des signaux et des slots
connect(m_connect,SIGNAL(clicked()),this,SLOT(conn ectDB()));
connect(m_cancel,SIGNAL(clicked()),this, SLOT(quit()));
}

void FenetreAccueil::connectDB()
{
QString server;
server=m_addressBD->text(); //HERE IS THE ERROR !!!!!!!!!!!!!!!

if(server)

QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(server);
db.setDatabaseName("callTracker");
db.setUserName("appClient");
db.setPassword("Clown2neige");
bool ok=db.open();
//dataB.exec();

//QApplication app2();
if(ok)
{
FenetrePrincipale *fenP;
fenP->show();
}
else
QMessageBox::critical(this,"callTracker",tr("Connection to database failed"));


}


Thanks for your help.

stampede
26th October 2011, 12:46
It looks more like the error should be here:

FenetrePrincipale *fenP;
fenP->show();
You forgot to create an object (with operator new).

----
edit:
@down
nix is right, I missed that somehow ;) anyway, above comment is still valid

nix
26th October 2011, 13:08
In your C++ instead of


QLineEdit *m_addressBD=new QLineEdit;

Do


m_addressBD=new QLineEdit();

Do this for all widgets, you are redeclaring locally your variable in the constructor, so in your slot m_addressBD is null and you programm crash.

phapha
26th October 2011, 13:52
Thank you stampede and nix. It works well now.:D

phapha
26th October 2011, 17:01
Hi all. I'm sorry but even if I don't have the segmentation error any more, the code seems to not work. I cannot connect to the MysQL database (installed on the same computer) after clicking on connect. There is always the error message Box. I don't know if the app even try to connect to database.
Can someone help me?