[SOLVED] Qt doesn't seem to be compiling my C++ code...
-
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...
-
[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...
-
[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;};@
-
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:
- 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.- 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...