使用我自己的类中的QMessageBox时出现问题 [英] Problem using QMessageBox from my own class

查看:507
本文介绍了使用我自己的类中的QMessageBox时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我创建的课程它工作正常。我想添加使用QMessageBox显示错误的功能。当我这样做时,我的课程调用后的代码没有被执行。



这是班级



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

#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;


        }
....





并非所有代码都在这里,关键是QMessageBox ::问题没有出现,程序以退出代码0结束。如果我从我的类中取出QMessageBox :: warning,那么QMessageBox ::问题确实出现了。有什么想法吗?



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?

推荐答案

初学者错了。通过调用我的意思是执行一个类方法。那是术语吗?

我按照你的建议使用了调试器。在执行QMessagebox的行上。我走进去打开< qstring.h>并且在这样的代码行上有一个黄色箭头。



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.


这篇关于使用我自己的类中的QMessageBox时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆