Segmentation error! Please help!!!
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
Code:
#include <QApplication>
#include "FenetreAccueil.h"
int main(int argc,char* argv[])
{
FenetreAccueil login;
login.show();
return app.exec();
}
FenetreAccueil.h
Code:
#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:
// QLineEdit *m_password;
//QLineEdit *m_login;
};
#endif // FENETREACCUEIL_H
FenetreAccueil.cpp
Code:
#include "FenetreAccueil.h"
FenetreAccueil
::FenetreAccueil(QWidget *parent
) :{
//Creation du layout de formulaire et ses widgets
m_password
->setEchoMode
(QLineEdit::Password);
//affichage d'asterisques a la saisie
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
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);
//Creation d'un layout pour le logo
logo
->setPixmap
(QPixmap("images/grandlogo.png"));
logoLayout->addWidget(logo);
//Creation d'un layout pour le message d'acceuil
m_label
= new QLabel(tr
("Welcome in callTracker"),
this);
labelLayout->addWidget(m_label);
//Creation du layout principal de la fenetre
//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(connectDB()));
connect(m_cancel,SIGNAL(clicked()),this, SLOT(quit()));
}
void FenetreAccueil::connectDB()
{
[B] server=m_addressBD->text();[/B] //HERE IS THE ERROR !!!!!!!!!!!!!!!
if(server)
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.
Re: Segmentation error! Please help!!!
It looks more like the error should be here:
Code:
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
Re: Segmentation error! Please help!!!
In your C++ instead of
Do
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.
Re: Segmentation error! Please help!!!
Thank you stampede and nix. It works well now.:D
Re: Segmentation error! Please help!!!
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?