[SOLVED]Weird!!My Qt application is running with the whole main function commented out.



  • Hi all ,
    I have been working on a Qt application but it is giving me a hard time working with its main function.I need to trap the application before it starts so I can do some stuff such as checking if a given process is running before the application starts and display some message if it is not and prevent it from running.I have been trying to do stuff inside main to no avail until an idea crossed my mind to comment everything out inside the main function and see what happens.The application simply ran proving that everything i have been doing was just being ignored.Is the IDE just ignoring my changes or is there another way the application is starting that i am not aware of.I also suspect that it is using some a previous version of the project and just rejecting my latest changes.Any help would really be appreciated.

    I am using Qt 4.8.1 ,Qt Creator 2.4.1 and visual studio's 2008 compiler.my system is windows 7 .Here is my main function:
    @/*

    #include <QtGui>
    #include <QTextCodec>

    #include "frameworks/UBPlatformUtils.h"
    #include "frameworks/UBFileSystemUtils.h"

    #include "UBApplication.h"
    #include "UBSettings.h"

    /* Uncomment this for memory leaks detection /
    /

    #if defined(WIN32) && defined(_DEBUG)
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #define DEBUG_NEW new( _NORMAL_BLOCK, FILE, LINE )
    #define new DEBUG_NEW
    #endif
    */

    void ub_message_output(QtMsgType type, const char *msg) {
    // We must temporarily remove the handler to avoid the infinite recursion of
    // ub_message_output -> qt_message_output -> ub_message_output -> qt_message_output ...
    QtMsgHandler previousHandler = qInstallMsgHandler(0);

    #if defined(QT_NO_DEBUG)
    // Suppress qDebug output in release builds
    if (type != QtDebugMsg)
    {
    qt_message_output(type, msg);
    }

    #else
    // Default output in debug builds
    qt_message_output(type, msg);
    #endif

    if (UBApplication::app() && UBApplication::app()->isVerbose()) {
        QString logFileNamePath = UBSettings::userDataDirectory() + "/log/uniboard.log";
        QFile logFile&#40;logFileNamePath&#41;;
    
        if (logFile.exists(&#41; && logFile.size(&#41; > 10000000&#41;
            logFile.remove();
    
        if (logFile.open(QIODevice::Append | QIODevice::Text)) {
            QTextStream out(&logFile);
            out << QDateTime::currentDateTime().toString(Qt::ISODate)
                << "      " << msg << "\n";
            logFile.close();
        }
    }
    
    qInstallMsgHandler(previousHandler);
    

    }

    int main(int argc, char *argv[])
    {

    // Uncomment next section to have memory leaks information
    // tracing in VC++ debug mode under Windows
    

    /*
    #if defined(_MSC_VER) && defined(_DEBUG)
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    #endif
    */

    Q_INIT_RESOURCE(sankore);
    
    qInstallMsgHandler(ub_message_output);
    

    #if defined(Q_WS_X11)
    qDebug() << "Setting GraphicsSystem to raster";
    QApplication::setGraphicsSystem("raster");
    #endif

    UBApplication app("Sankore", argc, argv&#41;;
    
    //BUGFIX:
    //when importing a sankore file that contains a non standard character
    //the codecForLocale or the codecForCString is used to convert the file path
    //into a const char*. This is why in french windows setup the codec name shouldn't be
    //set to UTF-8. For example, setting UTF-8, will convert "Haïti" into "HaÂ-ti.
    
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"&#41;&#41;;
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    
    QStringList args = app.arguments();
    
    QString dumpPath = UBSettings::userDataDirectory() + "/log";
    QDir logDir(dumpPath);
    if (!logDir.exists())
        logDir.mkdir(dumpPath);
    
    QString fileToOpen;
    
    if (args.size() > 1) {
        // On Windows/Linux first argument is the file that has been double clicked.
        // On Mac OSX we use FileOpen QEvent to manage opening file in current instance. So we will never
        // have file to open as a parameter on OSX.
    
        QFile f(args[1]);
    
        if (f.exists()) {
            fileToOpen += args[1];
    
            if (app.sendMessage(UBSettings::appPingMessage, 20000)) {
                app.sendMessage(fileToOpen, 1000000);
                return 0;
            }
        }
    }
    
    app.initialize(false);
    
    QObject::connect(&app, SIGNAL(messageReceived(const QString&)), &app, SLOT(handleOpenMessage(const QString&)));
    
    qDebug() << "file name argument" << fileToOpen;
    int result = app.exec(fileToOpen&#41;;
    
    app.cleanup(&#41;;
    
    qDebug(&#41; << "application is quitting";
    
    
    
    return result;
    

    }
    @


  • Moderators

    From personal experience: Yes, sometimes it might.
    I have experienced similar issues with Qt creator 2.6 on windows 7 in the past.

    Check the executable if it is build at all. Apparently through switching between release and debug modes I had confused Qt creator. Even so I had definitely debug mode set, it compiled again and again the release version in its destination folder only. However, this is with a complex project setup.

    According to a recent post, it has been fixed with creator version 2.7.



  • Thanks Koahnig,
    The set up of my project is complex too.I guess my best option is to get rid of Qt Creator 2.4 and install 2.7 in case there are other traps down the way.I was reluctant to use it as it had a lot of bugs in its early days .This has really nailed me down.I ll let you know if I am successful and again,thanks.


  • Moderators

    Well, I am reluctant to change from 2.6 to 2.7 as well. 3 subversions down seem to be a good reason to update ;-)
    Let me know, if 2.7 has still the same problem.



  • I just tried QtCreator 2.7 to no avail ,the problem is still there but I have found a way to achieve what I want without messing with the original main function .However I am afraid it might be bad performance wise.
    The simplified code is as follows:
    @
    int main(int argc, char *argv[])
    {

    if (condition)
    {
    QApplication a(argc, argv);
    
    //THE ORIGINAL APPLICATON HERE
    
    return a.exec&#40;&#41;;
    }
    
    else
    {
        QApplication a(argc, argv&#41;;
        
        //MY CUSTOM STUFF
        
        return a.exec(&#41;;
    
    
    }
    

    }@
    I have never seen someone do this but it is doing what I want so far.Need to hear your thoughts on this.
    Thanks.


  • Moderators

    Sorry, I did not have a look to your source, since you stated that the IDE was ignoring your changes.

    Still; I would assume that we are talking two problems.
    One the missing compilation after changes of the main program.

    Second the behaviour of your main program itself. For this I would not be overly concerned about bad performance, because you have just an additional statement, which costs almost nothing. However, it looks a bit strange as you have arranged it.

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    if (condition)
    {
        //THE ORIGINAL APPLICATON HERE
    }
    else
    {
        //MY CUSTOM STUFF
    }
    return a.exec(&#41;;
    

    }@

    This looks more logical as long as QApplication is identical for both parts.



  • Sorry I kind of have mixed things here.I also wanted to stick to the logic that you proposed but I kept getting a crash for which the reason is still a mystery to me.The second QApplication gave me a free land I could play in without fear of messing things up.At least for now.

    As for the IDE problem ,the only way I am getting my changes to take effect is to go in the build directory and delete any previous builds.That way I am getting the IDEs attention!!
    Thanks koahnig for you answers,it really helped.


  • Moderators

    [quote author="musimbate" date="1368435181"]
    As for the IDE problem ,the only way I am getting my changes to take effect is to go in the build directory and delete any previous builds.That way I am getting the IDEs attention!!
    Thanks koahnig for you answers,it really helped.[/quote]

    I do not have to go back as far. Typically a "Run qmake" heals the problem introduced with switching between debug and release modes.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.