Results 1 to 5 of 5

Thread: Segmentation error! Please help!!!

  1. #1
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. #include <QApplication>
    2. #include "FenetreAccueil.h"
    3.  
    4. int main(int argc,char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7. FenetreAccueil login;
    8. login.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    FenetreAccueil.h

    Qt Code:
    1. #ifndef FENETREACCUEIL_H
    2. #define FENETREACCUEIL_H
    3.  
    4. #include <QtGui>
    5. //#include <QCoreApplication>
    6. #include <QtSql>
    7. #include "FenetrePrincipale.h"
    8.  
    9.  
    10. class FenetreAccueil : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit FenetreAccueil(QWidget *parent = 0);
    16.  
    17. public slots:
    18. void connectDB();
    19.  
    20. private:
    21. QPushButton *m_connect;
    22. QPushButton *m_cancel;
    23. // QLineEdit *m_password;
    24. //QLineEdit *m_login;
    25. QLineEdit *m_addressBD;
    26. QLabel *m_label;
    27.  
    28. };
    29.  
    30. #endif // FENETREACCUEIL_H
    To copy to clipboard, switch view to plain text mode 

    FenetreAccueil.cpp
    Qt Code:
    1. #include "FenetreAccueil.h"
    2.  
    3. FenetreAccueil::FenetreAccueil(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. //Creation du layout de formulaire et ses widgets
    7. QLineEdit *m_login=new QLineEdit;
    8. QLineEdit *m_password=new QLineEdit;
    9. m_password->setEchoMode(QLineEdit::Password);//affichage d'asterisques a la saisie
    10. QLineEdit *m_addressBD=new QLineEdit;//format adresse IP ou pas?
    11.  
    12. QFormLayout *formAccueil=new QFormLayout;
    13. formAccueil->addRow(tr("&Login"),m_login);
    14. formAccueil->addRow(tr("Password"),m_password);
    15. formAccueil->addRow(tr("mySQL server address"),m_addressBD);
    16.  
    17. //Creation d'un layout horizontal pour les boutons
    18. m_connect=new QPushButton(tr("Connect"));
    19. m_cancel=new QPushButton(tr("Cancel"));
    20.  
    21. QHBoxLayout *boutonLayout=new QHBoxLayout;
    22. boutonLayout->addWidget(m_connect);
    23. boutonLayout->addWidget(m_cancel);
    24.  
    25.  
    26. //Creation d'un layout pour le logo
    27. QLabel *logo=new QLabel(this);
    28. logo->setPixmap(QPixmap("images/grandlogo.png"));
    29.  
    30. QHBoxLayout *logoLayout=new QHBoxLayout;
    31. logoLayout->addWidget(logo);
    32.  
    33.  
    34. //Creation d'un layout pour le message d'acceuil
    35. m_label = new QLabel(tr("Welcome in callTracker"),this);
    36.  
    37. QHBoxLayout *labelLayout=new QHBoxLayout;
    38. labelLayout->addWidget(m_label);
    39.  
    40. //Creation du layout principal de la fenetre
    41. QVBoxLayout *layoutPrincipal=new QVBoxLayout;
    42.  
    43. //Ajout du layout d'accueil
    44. layoutPrincipal->addLayout(labelLayout);
    45.  
    46. //Ajout du layout de formulaire
    47. layoutPrincipal->addLayout(formAccueil);
    48.  
    49. //Ajout du layout des boutons
    50. layoutPrincipal->addLayout(boutonLayout);
    51.  
    52. setLayout(layoutPrincipal);
    53. setWindowTitle(tr("Welcome"));
    54. setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
    55. resize(300,350);
    56.  
    57. //Generation des signaux et des slots
    58. connect(m_connect,SIGNAL(clicked()),this,SLOT(connectDB()));
    59. connect(m_cancel,SIGNAL(clicked()),this, SLOT(quit()));
    60. }
    61.  
    62. void FenetreAccueil::connectDB()
    63. {
    64. QString server;
    65. [B] server=m_addressBD->text();[/B] //HERE IS THE ERROR !!!!!!!!!!!!!!!
    66.  
    67. if(server)
    68.  
    69. QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    70. db.setHostName(server);
    71. db.setDatabaseName("callTracker");
    72. db.setUserName("appClient");
    73. db.setPassword("Clown2neige");
    74. bool ok=db.open();
    75. //dataB.exec();
    76.  
    77. //QApplication app2();
    78. if(ok)
    79. {
    80. FenetrePrincipale *fenP;
    81. fenP->show();
    82. }
    83. else
    84. QMessageBox::critical(this,"callTracker",tr("Connection to database failed"));
    85.  
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for your help.
    Last edited by phapha; 26th October 2011 at 12:14.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Segmentation error! Please help!!!

    It looks more like the error should be here:
    Qt Code:
    1. FenetrePrincipale *fenP;
    2. fenP->show();
    To copy to clipboard, switch view to plain text mode 
    You forgot to create an object (with operator new).

    ----
    edit:
    @down
    nix is right, I missed that somehow anyway, above comment is still valid
    Last edited by stampede; 26th October 2011 at 13:17.

  3. The following user says thank you to stampede for this useful post:

    phapha (30th October 2011)

  4. #3
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Segmentation error! Please help!!!

    In your C++ instead of

    Qt Code:
    1. QLineEdit *m_addressBD=new QLineEdit;
    To copy to clipboard, switch view to plain text mode 

    Do

    Qt Code:
    1. m_addressBD=new QLineEdit();
    To copy to clipboard, switch view to plain text mode 

    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.

  5. The following user says thank you to nix for this useful post:

    phapha (30th October 2011)

  6. #4
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error! Please help!!!

    Thank you stampede and nix. It works well now.

  7. #5
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

Similar Threads

  1. Segmentation error in run
    By kurrachow in forum Newbie
    Replies: 1
    Last Post: 21st April 2011, 13:13
  2. QT 4.6.0 with threads - UIC segmentation error
    By edmondo1984 in forum Installation and Deployment
    Replies: 5
    Last Post: 15th November 2010, 18:56
  3. building error: Segmentation fault
    By kaycee1 in forum Installation and Deployment
    Replies: 2
    Last Post: 21st October 2010, 10:08
  4. segmentation fault error
    By sagi in forum Installation and Deployment
    Replies: 2
    Last Post: 25th July 2008, 05:37
  5. Replies: 9
    Last Post: 6th September 2007, 08:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.