PDA

View Full Version : GUI locking problem



Benjamin
11th November 2008, 09:36
Hi all,

I am having problems with my GUI interface. It constantly keeps locking for a few seconds. My application is not a pure cpp application it has event handling mechanism which is implemented in C. From main function in C code I create guimain thread which than runs and send events to C code.

Main function in C looks like this:

int main(int argc, char *argv[])
{
.......do some stuff......
gui_init(argc, argv, (void*)&gui_sync);

while(!device_shutdown)
{
process_message_queues(5);
}
pthread_join( gui_t, NULL);
return 0;
}

CPP code

EXT_C int gui_init( int argc, char *argv[], void *gui_sync)
{
myargc = argc;
myargv = argv;
pthread_attr_init(&gui_attr);
pthread_create(&gui_t, &gui_attr, guimain, (void *)gui_sync);
return 0;
}

void *guimain( void* lpParam )
{
char *defaultargs[2];
struct gui_sync_t *sync = (struct gui_sync_t*)lpParam;

QApplication app( myargc, myargv );
main1=new formMain;
main1->setGeometry(0,0,240,320);
main1->show();
setTargetWidget(main1);
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );

sync->ready=1;
return (void*)app.exec();
}

When I run this application on my device I get following warning:
WARNING: QApplication was not created in the main() thread.

Does anyone has any idea?

Tnx,
Benjamin

caduel
11th November 2008, 10:05
The warning is quite clear, I think.
You do create the QApplication object in a thread that is not the main thread of the application. (I don't know if that will cause trouble. But probably it will, otherwise the Trolls would not have put that warning in, I guess.)

Any reason in particular why you want to put the gui not in the main thread?

Benjamin
11th November 2008, 10:10
Well I am not quite sure how to create QApplication object from C code.

caduel
11th November 2008, 10:19
from C code? you can't really - except by calling C++ code.

I see two ways to go:
i) make main.c into a main.cpp (should be no problem, right?)
ii) call from main.c setupAndRunGui()

where

// gui.h
extern "C" int setupAndRunGui();

// gui.cpp
int setupAndRunGui(int &argc, char **argv)
{
QApplication app(argc, argv);
// whatever else you need to setup
return app.exec(); // note that this is a blocking call
}


iii) I would suggest you create a "normal" Qt app.
* a C++ main that creates a QApplication object
* create a QThread that does your C event polling/handling
* send Qt custom events for that events from your C code (if the gui needs to act upon those)

HTH

Benjamin
11th November 2008, 10:42
Thank you:)

nguahoang205
30th July 2012, 05:28
Hi,
I am new qt programmer. I make a program run qt_browser demos. I use this program to call this part code:

Q_INIT_RESOURCE(data);
BrowserApplication application(qtArgc, (char**)qtArgv);
if (!application.isTheOnlyBrowser())
return 0;
application.newMainWindow();
exit = application.exec();

I call the first, it is OK, but when I close mainwindows and call again, it do not run and display "WARNING: QApplication was not created in the main() thread"
, then system is quited.
Anyone help me?
Thanhs

ChrisW67
30th July 2012, 05:48
Please do not double-post and/or resurrect long dead threads.
This question has its own thread