You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, May 21st 2004, 11:36pm

qapplication help

hi

is there a neccesity for the Qapplication(....) to be in the main .cpp.
Wht happens in the case of using a non gui based application where in the bootom layer starts the
entire operation.
u want the Qapplication running but it cannot be the main.cpp.

Is there a work around, if so plz let me know.
It would be of great help if somebody could paste the code for this kind of scenario.

Currently the Qapplication is declared in say class UI{
public : Qapplication(...)

The error is : request for setMainwidget in this->UI::a->(int , char**) which is of the non aggregate <unknown type>

One more thing wht exactly does the a->exec() function do.
Is it a must and can it be done away with

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Saturday, May 22nd 2004, 12:12am

Your problem here is that you have an instance of QApplication as an object, but you try to reference it as a pointer (hence you do -> instead of . ).

QApplication is a class that represents your whole GUI part of the program, manages data flow within Qt and so on...

exec() is a method, that starts the GUI of you application (starts an event loop) and doesn't return until the main widget of it closes.

You don't have to use QApplication if you don't want to... if your program is not GUI-based, you don't need it. If it is GUI-based, you better leave it alone as it is in main.cpp (or similar). You can safely do all you non-GUI stuff within the GUI code. GUI is basicaly event-driven, that is user triggers events that perform some tasks. If you want to, you can run a thread that will do some tasks in parallel to the thread running the GUI (but it should be possible to live without it).

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

3

Saturday, May 22nd 2004, 6:10pm

Just to make that clear, having a QApplication instance does not mean GUI application.

You'll need a QApplication instance whenever you want to use Eventloop based processing, like GUI, QSocket, QProcess, QTimer.

You can create the instance and start the event loop whenever you like, but usually this is in main() because you want to start event processing at application startup.

Cheers,
_
Qt/KDE Developer
Debian User

4

Sunday, May 23rd 2004, 8:36am

could u kindly post a sample code

of how u go a bout creating an instance of qapplication in a place other than main.cpp file.

i have been trying to do it, i haven't succeded.

thanks in advance

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

5

Sunday, May 23rd 2004, 12:39pm

foo.h

Source code

1
int foo();


foo.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
// Qt includes
#include <qapplication.h>

// local includes
#include "foo.h"

int foo()
{
    QApplication app(0, 0, false); // false -> without GUI

    return app.exec();
}


main.cpp

Source code

1
2
3
4
5
6
7
// local includes
#include "foo.h"

int main()
{
    return foo();
}


Cheers,
_
Qt/KDE Developer
Debian User

6

Sunday, May 23rd 2004, 7:27pm

ur code compiles but doesn't run

code which u have pasted above compiled all right but gave a segmentation fault when i ran it.
It said error in parameters passed to qapp(0,0,false).

Then wht i did was change the prototype of the foo function and passed the main command line arguments to the function.

FOO.CPP
// Qt includes
#include <qapplication.h>
# include<iostream>

using namespace std;
// local includes
#include "foo.h"

int foo(int v,char ** c)
{



QApplication app(v,c,TRUE); // false -> without GUI
cout<<"Qpp starting"<<endl;
app.exec();
return 0;

}


FOO.H
int foo(int,char **);



MAIN.CPP
#include <qapplication.h>
#include<iostream>
#include "form1.h"

#include <qapplication.h>



#include "foo.h"
using namespace std;
int main(int argc,char**argv)
{
cout<<"main starting:";
cout<<foo(argc,argv);

return 0;
}


I get output as
main starting: qpp starting (these are the 2 outputs which i had given to debug the prog, refer code)
then code hangs , i have to press control C to exit the program.


is there a prob exec();

i use qt version 3.2.3 , is it a stable release or is there something wrong i am doing int he way
i have implemented the code.

kindly help

Also by non gui based application i meant a middle layer which would instantiate the gui layer which has a class called GUIMANAGER, say which manages the gui screens, connection between screens etc .
by non gui i didn't refer to a console applicaiton. i need the screen shots to ??
Is ther a work around??
in the above code i have wantedly set the parameter as TRUE in instantiation of qapplication
because of the above reason.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Sunday, May 23rd 2004, 7:48pm

RE: ur code compiles but doesn't run

Probably argv shouldn't be null, it should at least contain the name of the executable file or at least be a two dimensional array (I mean a char ** - maybe even pointing to null)

newtoqt: What exactly do you want to achieve?

This post has been edited 1 times, last edit by "wysota" (May 23rd 2004, 7:49pm)


Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

8

Sunday, May 23rd 2004, 8:21pm

RE: ur code compiles but doesn't run

Quoted

Originally posted by newtoqt
I get output as
main starting: qpp starting (these are the 2 outputs which i had given to debug the prog, refer code)
then code hangs , i have to press control C to exit the program.


How do you see if it hangs?
There is no code to process until you stop the event loop with CTRL+C

Cheers,
_
Qt/KDE Developer
Debian User

9

Sunday, May 23rd 2004, 11:51pm

RE: ur code compiles but doesn't run

hi
argv[0] i printed i get the file name(./qap is my prog name)
argc returns a value pf 1
But when i try to print argv[1] as well as argv[2] (They don't have anyting stored in them) before the call to foo(.. , ..)
My prog hangs.it doesn't even call the foo function.

I tried the same thing in another example prog, the prog doesn't hang but anywhere else i use cout , i don't get the output in the console.


SO wht could be the problem with my code if not for NULL pointer.



************************************************************************************************
Wht i am trying to do is to build a minimalistic GUI with 4 forms, (which are working fine).
I have a lower layer from which i have to call my gui prog, and as i navigate through the forms
each command say a push button in the form would probably transfer execution to the lower layer.

Basically the main should lie in the lower layer and the GUIfunctionality is separate.


I have a UIMAIN class whose interfaces the lower layer calls.
I don't want the GUI to start the entire applicaiton, basically code should run even without the GUI jus remove it and show in console.
I hope i have made my self clear.

Basically qapplication shouldn't be in the main.cpp

****************************************************************************************************

I encountered a peculiar problem when i ran the make command(all of a sudden)
I don't even know if i should tell it here, hmm its relevance??

This is wht i got as an output jus before the compilation got over.

g++ -o qap .obj/foo.o .obj/main.o .obj/form1.o .obj/moc_form1.o -L/usr/lib/qt-3.2/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
**You have new mail in /var/spool/mail/root**

The statement enclosed within the ** ........**

is it because of a virus, linux OS prob, qt, qmake ............

i got this when i did make for both my prog and an example prog from the tutorial.

for 3 to 4 makes this came as an output
and all of a sudden it didn't reappear.
******************************************************************************************************
I am new to Linux as well as Qt. so i am sorry if i have posted irrelevant messages.
kindly reply.

10

Sunday, May 23rd 2004, 11:52pm

RE: ur code compiles but doesn't run

hi
argv[0] i printed i get the file name(./qap is my prog name)
argc returns a value pf 1
But when i try to print argv[1] as well as argv[2] (They don't have anyting stored in them) before the call to foo(.. , ..)
My prog hangs.it doesn't even call the foo function.

I tried the same thing in another example prog, the prog doesn't hang but anywhere else i use cout , i don't get the output in the console.


SO wht could be the problem with my code if not for NULL pointer.



************************************************************************************************
Wht i am trying to do is to build a minimalistic GUI with 4 forms, (which are working fine).
I have a lower layer from which i have to call my gui prog, and as i navigate through the forms
each command say a push button in the form would probably transfer execution to the lower layer.

Basically the main should lie in the lower layer and the GUIfunctionality is separate.


I have a UIMAIN class whose interfaces the lower layer calls.
I don't want the GUI to start the entire applicaiton, basically code should run even without the GUI jus remove it and show in console.
I hope i have made my self clear.

Basically qapplication shouldn't be in the main.cpp

****************************************************************************************************

I encountered a peculiar problem when i ran the make command(all of a sudden)
I don't even know if i should tell it here, hmm its relevance??

This is wht i got as an output jus before the compilation got over.

g++ -o qap .obj/foo.o .obj/main.o .obj/form1.o .obj/moc_form1.o -L/usr/lib/qt-3.2/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
**You have new mail in /var/spool/mail/root**
(this was not a valid directory)

The statement enclosed within the ** ........**

is it because of a virus, linux OS prob, qt, qmake ............

i got this when i did make for both my prog and an example prog from the tutorial.

for 3 to 4 makes this came as an output
and all of a sudden it didn't reappear.
******************************************************************************************************
I am new to Linux as well as Qt. so i am sorry if i have posted irrelevant messages.
kindly reply.

11

Monday, May 24th 2004, 12:01am

RE: ur code compiles but doesn't run

i know tht the code is hung because i output the return value of the "foo funciton"
which apparently never is not printed because the foo function never executes the return
statement as the prog is getting struck in the app.exec()

/* the foo.cpp*/
QApplication app(v,c,TRUE); // false -> without GUI
cout<<"Qpp starting"<<endl;
app.exec();
return 0;

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

12

Monday, May 24th 2004, 10:03am

RE: ur code compiles but doesn't run

The message you get during compiling is just an info that a new mail came for your user. I don't think that it has anything to do wit your program, but you can check the mail and see (using for example "mail" or "pine").

So you want two layers: one that does all the calculations, and the second, that handles all the GUI.

I see it done in two ways:

1. Make a normal graphical application and have another thread (using QThread) for the lower layer

or

2. Make two programs - one for calculations only and second one having a graphical interface for giving commands to the calculating process. You can communicate through pipes or sockets or IPC mechanizms if you're using System V compatible system. If all elses fail, you can always use files for communication (but it is a lame way to do it) - one proccess writes the file and then the other periodically checks it and reads if necessary.

The first approach is easier (you don't have to program all sync mechanisms and Inter Proccess Communication), but you'll probably always need GUI (I think there will be a necessity to have a connection to X server). The second one is more professional (let's say), more difficult to achieve, but you can have a console only app.

13

Monday, May 24th 2004, 10:30am

RE: ur code compiles but doesn't run

Implementing the 2nd way is something way beyond wht i can achieve in the time i have which is about a week.

Thats y i wanted to instantiate qapplication else where and have a middle layer main.cpp which instantiates the UIManager, and hence qapplication.
if at all i need to run gui the only separately change i would have to do is
to write a main for the gui alone.

More ever i feel this is a much easier way than the other 2 suggested becoz of the learning curve involved( i am new to threads also)
The first way of having the gui run in a thread and the bottom layer run in a another - i ll look into it.



I am still curious, i would like to know wht went wrong with the

code posted by Mr anda_skoa- it gave me a segmentation fault.
The modified code(changes which i incorporated), y does it hang when

app.exec() is called.

I would be more satisfied if the problem is found out and then a suitable solution arrived at rahter than have a work around.

if anybody has tried instantiating qapplication else where than the main file
please post a working code.
thanks in advance.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

14

Monday, May 24th 2004, 3:58pm

RE: ur code compiles but doesn't run

1. exec() doesn't hang your program, it starts an event loop and proccesses every event that happens, waiting for an event to stop proccessing and return the control to the caller.

2. This '2nd' approach is not as difficult as you think.

3. Concerning threads - have a look at QThread

4. main.cpp is just a file, it doesn't matter where you create the QApplication instance, main() is a function like any other, its only 'uniqueness' is that it is called by the loader when the code is executed. You can have a QApplication in every class you want and in every file you want. It is up to you to decide where and when (and if) you call the QApplication::exec(). You can put it in any function you want and call it anytime you want, just remember, if you do, the proccessing of your "main" (aka lower layer) program will cease unless you have a second thread (but here it would be better to use native threads instead of QThread to be sure of uninterruptable flow of the proccessing) to do the job. Also remember that you won't have a Qt GUI (well... any modern GUI) if you don't start an event loop.

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

15

Monday, May 24th 2004, 9:18pm

wysota already explained that the program didn't stuck in the exec() call but was executing the eventloop as expected.

However, I don't see the need to have a separate control flow in your lower layer, be it a thread or a child process.

Lets assume you have a class which provides all the methods fr your applications instances.
You can decide at startup if you want to call this methods from slots of a GUI or from a function in hardcoded or script controlled order.

For example lets assume I have a class like this

Source code

1
2
3
4
5
6
class Test
{
public:
    void hello() { cout << "hello" << endl; }
    void world() { cout << "world" << endl; }
};


I could then have a main.cpp that either starts in event mode, allowing to output the text in any order by calling the methods from slots connected to buttons or timers, or in a normal straight control flow, outputting "hello world"

Source code

1
2
3
4
5
6
7
int main(int argc, char** args)
{
    if (argc >= 2 && strncmp(args[1], "--gui", 5) == 0)
        return startGUI(argc, args);
    else
        return startNormal(argc, args);
}


startNormal would just instantiate Test and call hello() and world(), startGUI would initalize the QApplication and the GUI and so on.

Cheers,
_
Qt/KDE Developer
Debian User

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

16

Monday, May 24th 2004, 10:17pm

Don't you need then to double your code for "lower layer" for console approach (as a uninterrupted proccess) and gui approach (based on timers)? I don't mean doubling the functions but rather their calls and saving the state every moment to go back to event loop (it's not necessary in "console approach" to save the state of calculations).

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

17

Tuesday, May 25th 2004, 4:32pm

The lower layer only needs to save its internal state, just like any class does that has operations on the same data.

If there is more state information necessary when running in an event based system, that's the responsibility of the event based control module.

Cheers,
_
Qt/KDE Developer
Debian User