Click here to Skip to main content
15,887,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a class I've created it works fine. I want to add the ability to display an error using QMessageBox. When I did this the code after the call to my class is not executed.

Here is the class

C#
#include <connect2sql.h>


connect2SQL::connect2SQL(QObject *parent) :
QObject(parent)
{

    qDebug() << "inside constructor connect2SQL called from " ;
}


 connect2SQL::~connect2SQL()
{
    //qDebug() << "inside destructor connect2SQL::~connect2SQL";
}

 void connect2SQL::TryConnect()
 {

     db.setHostName(Server);
     db.setDatabaseName(Catalog);
     db.setUserName(User);
     db.setPassword(Password);
     //qDebug() << "inside connect2SQL::TryConnect db.open() =" <<db.open();
     if (db.open())
         isConnected = true;

     else
     {
          // this didn't work
         // apparently can't use
         // message box in your own class
//         QMessageBox::warning(
//                 NULL,
//                 "SQL connection failed",
//                 "SQL connection failed");


             loggedIn=false;
             TryAgain=true;
             Abort=false;
             isConnected = false;




       }


           //qDebug() << "inside  connect2SQL::TryConnect isConnected =" <<isConnected;



 }


Here is the code where I call the class

con->TryConnect();

    //qDebug() << "inside Login dialog:: isConnected =" << con->isConnected;


    if (con->isConnected==false) {


        if (QMessageBox::question(this, "Login Failed",
                                  "Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
        {
            loggedIn=false;
            TryAgain=true;
            Abort=false;


        }
....


Not all the code is here, the point is the QMessageBox::question doesn't appear and the program ends with exit with code 0. If I take the QMessageBox::warning out of my class then QMessageBox::question does appear. Any ideas?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Dec-15 21:39pm    
There is no such concept as "call to my class". What call, exactly? Did you use the debugger?
—SA
Jochen Arndt 16-Dec-15 3:29am    
It seems that a critical Qt error occurs when calling QMessageBox::warning(). In such cases the application terminates immediately and there should be some output with debug builds (in the debug window or even on the shell / command line when the program has been started from there). This error message should give you a hint what is going wrong.

Are there any compiler warnings when using the highest warn level? These may already indicate that something is not OK.

1 solution

Beginners mistake. By "calling" I mean executing one of the classes methods. Is that the terminology?
I used the debugger as you suggeted. On the line that executes the QMessagebox. I step into and it opens <qstring.h> and a yellow arrow is on a line of code like this.

#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
    inline QT_ASCII_CAST_WARN QString(const char *ch)
        : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
    {}


the debugger shows a message stopped "end stepping range". So that didn't help much. Do you have any more suggestions? Thanks for the help thus far.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900