PDA

View Full Version : change cursor icon while program is busy



Tomasz
29th August 2010, 17:26
Hello!

I've got application which runs other application in new thread by clicking on a button:



QProcess *proc;
proc = new QProcess( this );
proc->startDetached("/home/program");


is there any simple way to change cursor icon (for example to clock or anything else) while program is loading? Because sometimes it is loading quite long and I don't know if i clicked button or not.

thanks in advance
best regards
Tomasz

Urthas
29th August 2010, 18:48
I assume you have already subclassed QCursor.

Connect the program-loading code to a slot in your custom cursor object that updates the cursor's pixmap. Then you just emit the appropriate signal when you start or finish loading.

Tomasz
30th August 2010, 23:39
I think some example code will be very useful in this case. Still have no idea how to get down to it.

thanks in advance
best regards
Tomasz

ChrisW67
31st August 2010, 06:08
QApplication::setOverrideCursor()

Tomasz
31st August 2010, 10:20
Can I make it wait with QApplication::restoreOverrideCursor() until my application is fully loaded? Because now cursor changes for a moment and application is still loading (code in first message).

thanks in advance
best reagards
Tomsz

Urthas
31st August 2010, 23:03
Before you call proc->startDetached(), connect proc's started() signal to a slot that calls QApplication::setOverrideCursor(), and connect proc's finished() signal to a slot that calls QApplication::restoreOverrideCursor()?

Tomasz
20th September 2010, 10:27
I've done it that way:



void MainWindow::runMyApp(QString parameter)
{
QProcess *proc;

proc = new QProcess( this );

connect(proc, SIGNAL(started()), this, SLOT(appIsBusy()));
connect(proc, SIGNAL(finished(int)),this, SLOT(appIsFree()));

proc->startDetached("/myapp", QStringList() << parameter);

delete proc;
}


But with no effect. Any other idea? Or maybe I should do it in different way?

thanks in advance
best regards
Tomasz

wysota
20th September 2010, 10:30
This code doesn't make any sense. You connect some signals and immediately delete the object... startDetached() is not a blocking call.

Tomasz
20th September 2010, 10:52
As I thought it's senseless. So is it possible to make what I want without deleting QProcess object? Earlier I was just changing icon for some time and then restoring it but sometimes app was loading longer or faster...

thanks in advance
best regards
Tomasz

wysota
20th September 2010, 11:05
Create a QProcess object and set it up with the data you need. Connect the started() signal to some slot of yours. Optionally connect the finished() signal with a deleteLater() slot to destroy the object once the child process exits. Change the cursor, start the process. In the slot connected to the started() signal restore the cursor.

Tomasz
24th September 2010, 10:57
So I've done something like this:



void MainWindow::runMyApp(QString parameter)
{
QProcess *proc;

proc = new QProcess( this );

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor));
connect(proc, SIGNAL(started()), this, SLOT(appIsLoaded()));

proc->startDetached("/myapp", QStringList() << parameter);

delete proc;
}


My cursor is changing, but I can't restore it back. I know that @wysota said I shouldn't delete my process byt I'm using proc->startDetached() because if I use proc->start my parent application is closing when I'm closing child application. If I use proc->startDetached() in child application cursor is normal but on parent application I still have 'WaitCursor'. Any suggestions? How can I do it?

thanks in advance
best regards
Tomasz

wysota
24th September 2010, 11:05
startDetached() does not wait until the process actually gets started. By deleting the object immediately, you don't have a chance to catch any of its signals. You should connect the started() or finished() signal to the deleteLater() slot and you'll be fine. If your parent is closing when the child closes than it probably crashes and not closes and there is something wrong with your code.

Tomasz
24th September 2010, 11:33
So, I've got something like this:



void MainWindow::runMyApp(QString parameter)
{
QProcess *proc;

proc = new QProcess( this );

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor));
connect(proc, SIGNAL(finished()), this, SLOT(appIsLoaded()));
connect(proc, SIGNAL(finished(int)),this, SLOT(deleteLater()));

proc->start("/myapp", QStringList() << parameter);
}


And console gives:



Object::connect: No such signal QProcess::finished()
Object::connect: (receiver name: 'MainWindow')
*** glibc detected *** /qt/test: free(): invalid pointer: 0xbef3db44 ***


Parent application (test) is closing when I'm closing child application. But 'ps' command says that 'test' is still working... I've tried to run 'clean' application that is doing nothing to be sure everything is OK with child application, and same thing happens.

Any ideas?

thanks in advance
best regards
Tomasz


-------------------------------------------
Edit:
Of course in first connect should be finished(int) then console gives only:



*** glibc detected *** /qt/test: free(): invalid pointer: 0xbee37b44 ***

wysota
24th September 2010, 11:41
You are probably deleting the same object twice somewhere.

Tomasz
24th September 2010, 12:06
Ok. Maybe this will be stupid question, but I need to ask - when child process is exiting what is happening with the pointer and all reserved memory for that process? You said that I'm probably deleting same object twice, so I removed one line to see what will happen:



connect(proc, SIGNAL(finished(int)),this, SLOT(deleteLater()));


And now it seems everything looks fine but I don't know if I'm doing some mess in memory. So I need to know Is everything being delete when I'm closing child application.

thanks in advance
best regards
Tomasz

wysota
24th September 2010, 12:16
Ok. Maybe this will be stupid question, but I need to ask - when child process is exiting what is happening with the pointer and all reserved memory for that process?
You mean the QProcess object? Nothing is happening to it. It's not deleted automatically if that's what you're asking.

Tomasz
24th September 2010, 12:52
So I have no idea why my app is crashing if I try to delete my QProcess object:



connect(proc, SIGNAL(finished(int)), this, SLOT(appIsLoaded()));
connect(proc, SIGNAL(finished(int)), this, SLOT(deleteLater())); //without it works fine, no errors
proc->start("/myapp", QStringList() << parameter);


It happens when I run process by start() If I use startDetached() I can delete QProcess object without any error... I've checked if I'm deleting something twice, and I'm not. QProcess object is the only thing I'm deleting.

thanks in advance
best regards
Tomasz

wysota
24th September 2010, 13:03
Maybe you are doing it in appIsLoaded()? Anyway, you can reuse the object again if you need to spawn the process again so you don't have to create and delete it every time.

Tomasz
24th September 2010, 13:13
appIsLoaded() only restores cursor nothing more. But I's good idea to reuse that pointer. I'll declare it in header file and use every time I need. Should I clean It or something after every use?

But I'm still curious about why it crashes...

thanks in advance
best regards
Tomasz

wysota
24th September 2010, 13:24
No, you don't have to do anything with it.