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

Tuesday, March 1st 2005, 4:55pm

problem with interactive GUI

hi,
i'm newbie in Qt, i'm trying to write some simple multithread program, when i press a button i start a new thread that do whatever, after that i can not completely interact with my interface, i can only move the main form, that's it, i read the forum, i have tryed processEvents() and still nothing :( any idea? thanks for your help

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Tuesday, March 1st 2005, 5:14pm

RE: problem with interactive GUI

Could you post the code of that slot connected to your button?

3

Tuesday, March 1st 2005, 5:21pm

class Train : public QThread{
...
}

Train* l = new Train(lineEdit17->text(), l2, l1, z_t, s1, s2);
l->start(QThread::LowestPriority);

the entire code is a little more complicated so i haven't posted it all, but making start new thread is like above...

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

4

Tuesday, March 1st 2005, 5:31pm

One possible cause is that you start this new thread in it's constructor and the QThead::start function waits until your thread stops.

Do you have some loops or some heavy computations in that slot? What happens when you add qDebug("xxx") at the end of the slot?

5

Tuesday, March 1st 2005, 5:49pm

i don't start the thread from its constructor, i start it from the slot of a button, it seems like the main thread waits for the termination of the new one because when i put such a thing:

Train* t = new Train(...);
t->start();
QDebug("xx");

the "xx" shows on the input after the termination of the thread, bizarre...besides even i have some heavy computations it shouldn't work like this, isn't it? the gui shall interact slower but it should, am i wright?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

6

Tuesday, March 1st 2005, 5:57pm

If your thread is time-consuming and has bad design then it can delay all other processes (including the main thread).

7

Tuesday, March 1st 2005, 6:01pm

even in each iteration i make it sleep for 0,5 sec.??? when i make it sleep then the main thread should wake up and work, isn't it? if not how could i share the time between the threads? in order the gui works properly?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

8

Tuesday, March 1st 2005, 6:04pm

You don't need sleep() to preempty a thread. Every system call is a good place to give up the cpu to another process and I guess you use a number of them there.

9

Tuesday, March 1st 2005, 6:11pm

i don't use systems calls at all, as you said i would like to preempty the thread but it doesn't work, so how could i do it in Qt?

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

10

Tuesday, March 1st 2005, 6:35pm

Try:

Source code

1
2
3
4
Train* t = new Train(...);
if( t->running() ) qDebug("yy");
t->start();
qDebug("xx");

11

Tuesday, March 1st 2005, 6:48pm

it's still the same thing, i get "xx" just after the only thread terminates, i just don't know what to do:(

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

12

Tuesday, March 1st 2005, 6:55pm

What does your thread do?

13

Tuesday, March 1st 2005, 8:04pm

hmmmm, it's very complicated :)):

void Lokomotywa::run(){
wysadzony = false;
unsigned int do_stacji = trasy[0]->get_Dystans();
long int indeks_trasy = 0;
bool mozna_do_przodu;
unsigned int przesuniecie;
bool powrot = false;
bool kierunek;

Stacja* s = kierunki[0] ? trasy[0]->get_Stacja_1() : trasy[0]->get_Stacja_2();
while(!s->zajmij_Semafor(this))
this->msleep(50);

while(!wysadzony){
kierunek = powrot ? !kierunki[indeks_trasy] : kierunki[indeks_trasy];
if(do_stacji==trasy[indeks_trasy]->get_Dystans()){
mozna_do_przodu = trasy[indeks_trasy]->rejestruj(this, kierunek);
if(mozna_do_przodu)
s->zwolnij_Semafor(this);
}
if(mozna_do_przodu){
unsigned int c_s = (speed-weigth/1000)/60;//jedna minuta
przesuniecie = trasy[indeks_trasy]->przesun_Semafor(this, kierunek, true, c_s);
do_stacji -= przesuniecie;
c_s -= przesuniecie;
trasy[indeks_trasy]->get_Stacja_1();

if(do_stacji==0&&c_s>0){//za jednym razem nie przeleci wiecej jak jedna trase
if(kierunek)
s = trasy[indeks_trasy]->get_Stacja_2();
else
s = trasy[indeks_trasy]->get_Stacja_1();
if(s->zajmij_Semafor(this)){
trasy[indeks_trasy]->usun_Semafor(this, kierunek, true);
if(!powrot&&++indeks_trasy==(long int)trasy.size()){
indeks_trasy = trasy.size()-1;
powrot = true;
}
if(powrot&&--indeks_trasy==(long int)-1){
indeks_trasy = 0;
powrot = false;
}
mozna_do_przodu = false;
do_stacji = trasy[indeks_trasy]->get_Dystans();
if(s->get_Name()!="")
this->msleep(200);
else
this->msleep(100);
}
else
this->msleep(50);
}
else
this->msleep(100);
}
else
this->msleep(50);
}
it' something like this...it's a simulation of a train, it checks does it can move forward, then it moves and sleeps, that's all(more or less)...

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

14

Tuesday, March 1st 2005, 8:32pm

Quoted

Originally posted by klops
it' something like this...it's a simulation of a train, it checks does it can move forward, then it moves and sleeps, that's all(more or less)...

Yes, we can understand Polish, you know :)

You could use semaphores for synchronisation (programming, not the train ones).

15

Tuesday, March 1st 2005, 8:41pm

everybody knows polish :), sorry but i can't translate all my code, besides i used QMutex for synchronization, nobody has enough knowledge to solve this megadifficult problem? i don't believe...i'm too green in Qt

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

16

Tuesday, March 1st 2005, 8:49pm

Quoted

Originally posted by klops
everybody knows polish :),

Maybe not everybody, but probably 50M people do :)

Quoted


sorry but i can't translate all my code, besides i used QMutex for synchronization, nobody has enough knowledge to solve this megadifficult problem? i don't believe...i'm too green in Qt


Just for a test - make a mutex, block the thread with it after doing some "calculations", do a qDebug() in the main thread and release the mutex from the main thread. Let's see what will happen.

BTW. What do you need the additional thread for? You could put the content of the "while" loop into a slot of some kind and do a normal signal/slot processing with timers or something.

17

Tuesday, March 1st 2005, 9:00pm

thanks for advice, i'll try, i would like to create a trail system with trains on it, trains ride, passengers travel, everybody is happy(besides me), and i would like to make on threads...

18

Tuesday, March 1st 2005, 9:14pm

it's very very bizzarre, i've created a new very simple thread and...it works properly, i must have some mistake in my code, have to find :D...

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

19

Tuesday, March 1st 2005, 9:15pm

Then mutexes are what you need. You need them anyway to synchronise trains otherwise each one would be travelling at different speed. You could do it on a single thread too, you know :)

20

Wednesday, March 2nd 2005, 10:13pm

can I call a non-thread method from a running thread? i mean a method that isn't a part of a thread class, because i think the problem is in this, because when i delete all non-thread calling methods it works ok...maybe there is some special way to do this?