[SOLVED] Qt doesn't seem to be compiling my C++ code...



  • Hi all, using Qt 5.2.1 MinGW trying to learn Qt at the same time. I made an app, and from what I'm seeing, it doesn't seem to be compiling my c++ code. I can build with no errors, and my window even opens up, but nothing works, and my compiler output suggests that I may not be compiling my code. Can someone take a look for me and verify, and enlighten me a little? Thanks!!

    Compiler output:

    @15:08:12: Running steps for project Test...
    15:08:12: Configuration unchanged, skipping qmake step.
    15:08:12: Starting: "C:\Qt\Qt5.2.1MinGw\Tools\mingw48_32\bin\mingw32-make.exe"
    C:/Qt/Qt5.2.1MinGw/Tools/mingw48_32/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory 'X:/Lambda UDP Test Program/Test/build-Test-Desktop_Qt_5_2_1_MinGW_32bit-Debug'
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../Test -I'../Test/qtquick2applicationviewer' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQuick' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQml' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtNetwork' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtGui' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtCore' -I'debug' -I'.' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/mkspecs/win32-g++' -o debug/main.o ../Test/main.cpp
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../Test -I'../Test/qtquick2applicationviewer' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQuick' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQml' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtNetwork' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtGui' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtCore' -I'debug' -I'.' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/mkspecs/win32-g++' -o debug/qtquick2applicationviewer.o ../Test/qtquick2applicationviewer/qtquick2applicationviewer.cpp
    C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/bin/moc.exe -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -D__GNUC__ -DWIN32 -I../Test -I'../Test/qtquick2applicationviewer' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQuick' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQml' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtNetwork' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtGui' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtCore' -I'debug' -I'.' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/mkspecs/win32-g++' ../Test/qtquick2applicationviewer/qtquick2applicationviewer.h -o debug/moc_qtquick2applicationviewer.cpp
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../Test -I'../Test/qtquick2applicationviewer' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQuick' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQml' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtNetwork' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtGui' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtCore' -I'debug' -I'.' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/mkspecs/win32-g++' -o debug/moc_qtquick2applicationviewer.o debug/moc_qtquick2applicationviewer.cpp
    g++ -Wl,-subsystem,windows -mthreads -o debug/Test.exe debug/main.o debug/qtquick2applicationviewer.o debug/moc_qtquick2applicationviewer.o -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmaind -LC:\Qt\Qt5.2.1MinGw\5.2.1\mingw48_32\lib -lQt5Quickd -lQt5Qmld -lQt5Networkd -lQt5Guid -lQt5Cored
    Copying application data...
    mingw32-make[1]: Leaving directory 'X:/Lambda UDP Test Program/Test/build-Test-Desktop_Qt_5_2_1_MinGW_32bit-Debug'
    Copying application data...
    15:08:17: The process "C:\Qt\Qt5.2.1MinGw\Tools\mingw48_32\bin\mingw32-make.exe" exited normally.
    15:08:17: Elapsed time: 00:05.@



  • Too long to fit on the post - here's the code:

    QML:

    @import QtQuick 2.0
    import QtMultimedia 5.0
    import QtQuick.Controls 1.1
    import QtQuick.Window 2.1
    import QtWinExtras 1.0
    import QtQuick.Layouts 1.1

    Rectangle {
    id: main
    width: 640
    height: 480
    signal onClicked_button

    TextInput {
        id: textInput
        x: 280
        y: 230
        width: 80
        height: 20
        text: "Change me please..."
        font.pixelSize: 12
    
        Button {
            id: button1
            x: 3
            y: 37
            text: "Button"
            onClicked:main.onClicked_button()
        }
    }
    

    }
    @

    header:

    @#ifndef TEST_H
    #define TEST_H

    #include <QObject>
    #include <QtCore>
    #include <QString>
    #include <QQuickView>
    #include <QQuickItem>
    #include <QGraphicsObject>
    #include <QDeclarative>
    #include <QQmlComponent> // for accessing QML from C++
    #include <QQmlContext> // for accessing C++ from QML

    QString str = "Hello World";

    class test : public QObject
    {
    Q_OBJECT
    public:
    //explicit test(QObject *parent = 0); // <-- this was inserted by the IDE class setup wizzard.

    test(QObject* parent, QQuickView* dialog)
        : QObject(parent), myDialog(dialog) { }
    

    signals:

    public slots:

    void test_button(){
    
        QQuickItem* item = myDialog->rootObject();
        QObject *text = item->findChild<QObject*>("textinput");
        text->setProperty("text", "Hello World");
    
        return;}
    

    private:
    bool switched;
    bool textOutput;
    };
    #endif // TEST_H
    @

    test.cpp:

    @#include "test.h"
    #include <QGuiApplication>
    #include <QtCore>
    #include <QObject>
    #include <QQuickItem>
    #include <QQmlComponent>
    #include <QQmlContext>
    #include <QQuickView>
    #include <QDeclarativeEngine>
    #include <QDeclarativeComponent>
    #include <QDeclarativeContext>

    test::test(QObject *parent) :
    QObject(parent)
    {
    }

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

    QGuiApplication app(argc, argv);
    
    QQuickView *view = new QQuickView(QUrl("test.qml"));
    
    QQuickItem *item = view->rootObject();
    
    test *funcs = new test(0, view);
    
    // connect button signals to their slots:
    QObject::connect(item, SIGNAL(buttonClicked_button()), funcs, SLOT(test_button()));
    
    
    view->show();
    //delete funcs;
    return app.exec();
    

    }
    @

    wizzard-generated main.cpp:

    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"

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

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/Test/main.qml"&#41;);
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @


  • Moderators

    Hi,

    [quote]
    @
    g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_QUICK_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../Test -I'../Test/qtquick2applicationviewer' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQuick' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtQml' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtNetwork' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtGui' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/include/QtCore' -I'debug' -I'.' -I'C:/Qt/Qt5.2.1MinGw/5.2.1/mingw48_32/mkspecs/win32-g++' -o debug/main.o ../Test/main.cpp
    @
    [/quote]That's a compilation right there -- g++ is your compiler, main.cpp is your C++ file.

    [quote]my window even opens up, but nothing works[/quote]Can you please describe "nothing works" in more detail?



  • When I click the button during runtime, nothing happens, not even an error message. Still trying to figure out the debug, will post more when I do, hopefully later today.


  • Moderators

    Two issues:

    Your top-level item is the Rectangle, but your Rectangle doesn't have a property called "text". In other words, your TextInput is NOT the top-level item, and therefore cannot be accessed from outside. (Do you remember why you added a custom "onClicked_button" signal? If so, I encourage you to explain it in your own words when you reply)

    Why do you have two "main" functions?



  • JKSH - I will answer part one first, and get back to you on the second in the morning... I just noticed something about my project when I read your question. It made me think a little differently about it and I lie that - so I'll answer you in the morning. AS for the custom onClicked - I have a project that has many buttons, so I wanted to connect them all to different slots, and my understanding was that I needed the different signals in order to do that. This particular project that I posted is the test version of my project. I test my basic functions here, then incorporate them into my project.

    If I understand what your saying , I don't need to include the button and text box into the rectangle? I can keep them all separate? Or should they be in the rectangle?



  • Ok, so it would seem that I had something mixed up. When I created the project, the first thing I did was to add a class header file through the "new c++ Class" wizzard in the file menu. I built my file from the .cpp file that was created with it because it contained the class constructor. So I added the main function to that. All the files were created in the /qml/ directory, so I assumed that the main function needed to be built from the .cpp file that was created from the header file. I only just saw the main file when I made this post, and at a quick glance, I misread it and thought that it pointed to the .cpp file that came along with the class file. So I will transfer my functions back to the main.cpp file and re-compile and report back to you. Hope this didn't waste too much of your time, sorry about that. Sometimes the most obvious things are well hidden...



  • You are also specifying your qml file using a relative path. This is relative to your running process' current working directory, which is quite probably not the same place as your source code.



  • ChrisW67 - the relative path in main.cpp was created by the Qt-Creator wizzard - That wasn't my doing. My error comes in the form that I didn't see the main.cpp file, and assumed that when the "Test" class I wanted was made by the same wizzard, that my code was required to be placed in there. I assumed that somewhere, Qt placed it's own "main" file that pointed to that cpp file it created along with the header file.

    So now, since I created this class, and wrote part of my program in the included cpp file, I'm wondering how correct I am in my programming since it compiled so easily. Keep an eye on this thread, hopefully I 'll have an update by tuesday morning. I think I'm going to make a "how to" example post when all this is working and done, so that other newbies like myself can see a working example and know the steps (but not the trouble lol) that I wen through to get to that point :)
    -I'll update tomorrow morning the latest. Thanks again for the help, both yourself and especially JKSH - you've given me a lot of insight on the workings of this system!! I'm actually learning as I go and that's the biggest help you can give. I appreciate all you've done for me and thanks in advance for helping with anything more I might ask! :)



  • So everything is about fixed at this point... I got everything working and building. I had to add the following to my pro file:

    @HEADERS += test2.h@

    Now the only issue I have arises when I run it. it can't find my main.qml file for some reason:

    @file:///X:/UDP Test Program/build-test2-Desktop_Qt_5_2_1_MinGW_32bit-Debug/main.qml: File not found "@

    Currently looking into settings to see if I have a mis-set path...


  • Moderators

    [quote author="jediengineer" date="1393270534"]it can't find my main.qml file for some reason:

    @file:///X:/UDP Test Program/build-test2-Desktop_Qt_5_2_1_MinGW_32bit-Debug/main.qml: File not found "@

    Currently looking into settings to see if I have a mis-set path...
    [/quote]Did you put main.qml in X:/UDP Test Program/build-test2-Desktop_Qt_5_2_1_MinGW_32bit-Debug/ ? That's the working directory for your .exe file when you launch it from Qt Creator.

    (Qt Creator won't move your .qml files automatically for you, unless you create a Qt Quick application project. In that case, it will generate the deployment script automatically.)

    P.S. You are most welcome. Your enthusiasm and perseverance is refreshing :)



  • Thanks JKSH! This is a QtQuick application, and it did move the files when it made and built the project, but it put them in a /qml/.../ directory and then complained that it couldn't find them... Not sure why...

    I have since solved most of the problems and got the application working, so to speak. Having a little trouble passing pointers to my class functions so that I can call the function and operate on the qml widgets without creating another QQuickView.... So far not having much luck, but I'm trying! If you have any advice, I'm all ears...


  • Moderators

    [quote]
    @QQuickView *view = new QQuickView(QUrl("test.qml"));@

    ...

    it put them in a /qml/…/ directory and then complained that it couldn’t find them… Not sure why…[/quote]Because you asked your program (at the top of this quote) to look in the root directory, not the /qml/.../ subdirectory. If you want to use Qt Creator's structure, then you need to change your QUrl.

    [quote]Having a little trouble passing pointers to my class functions so that I can call the function and operate on the qml widgets without creating another QQuickView…[/quote]You mean a pointer to your QQuickView?

    The following line of yours passes it correctly into the test constructor
    [quote]
    @
    test *funcs = new test(0, view);
    @
    [/quote]What does your constructor do with the pointer that it gets?



  • JKSH - IT WORKS!! Compiles and runs. MY issue before was that I was creating a new qml object with every button press. I needed to pass a pointer to the qml object for my class functions to be able to use. I have succeeded through trial and error, and a little explanation from a friend. I actually feel like a million bucks, not because it's done, but because I believe I understand all of it!! Thanks again for all YOUR help as well. I don't think I'd have understood it as well without it. One last question - how do I mark this post as [SOLVED] ? I tried changing the title line, it won't let me...

    This is my code by the way:
    main.cpp:
    @int main(int argc, char argv[]) {
    QGuiApplication app(argc, argv);
    QQuickView
    view = new QQuickView(QUrl("main.qml"));
    view->show();
    test2 *funcs = new test2(0, view);
    QQuickItem *rect = view->rootObject();

    // connect button signals to their slots:
    QObject::connect(rect, SIGNAL(onClicked_button()), funcs, SLOT(sys_button()));
    
    return app.exec();
    

    }@

    test2.h:

    @class test2 : public QObject
    {
    Q_OBJECT
    public:
    test2(QObject *parent, QQuickView *view)
    : QObject(parent), myDialog(view){}

    public slots:
    void sys_button() {
    QQuickItem* object = myDialog->rootObject();
    QObject textinput = object->findChild<QObject>("textinput");
    if (textinput) {
    if (!switched) {
    textinput->setProperty("text", QString("Goodbye World"));
    switched = !switched;
    }
    else {
    textinput->setProperty("text", QString("Hello World"));
    switched = !switched;
    }
    }
    return;
    }

    private:
    QQuickView* myDialog;
    bool switched;

    };@


  • Moderators

    Congratulations! :) You are doing very well, especially since you only started developing desktop software a few weeks ago.

    You can add "[SOLVED]" by editing your original post. Change the title there.

    A few more tips:

    1. Get into the habit of initializing all of a class' variables in its constructor (i.e. you should initialize your switched variable to a known value). A boolean is relatively harmless, but if you forget to initialize an integer or a pointer, you might get strange behaviour and even crashes.

    [quote]
    @
    // test2.h
    test2(QObject *parent, QQuickView *view)
    @
    @
    // main.cpp
    test2 *funcs = new test2(0, view);
    @
    [/quote]2) Pass the QQuickItem pointer instead of the QQuickView pointer. That will make the rest of your code cleaner, as you won't need to call rootObject() all the time anymore.

    1. By convention (not a hard-and-fast rule, but a useful convention), the "parent" parameter is usually last in the list and is given a "default parameter":http://www.learncpp.com/cpp-tutorial/77-default-parameters/. That way, if you don't want your object to have a parent, just omit the argument:
      @
      // test2.h
      test2(QQuickItem *item, QObject *parent = 0)
      @
      @
      // main.cpp
      test2 *funcs = new test2(item);
      @


  • Thanks JKSH!! Always good to know the convention! Also, tried the [SOLVED] edit, it wouldn't let me change the title...



  • Apparently, this time it worked...


Log in to reply
 

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