I have created two QPushButton on two different QMainWindow. I am assigning focus to them randomly at a specific interval.Here is the code.
int main(int argc, char **argv){ QApplication a(argc, argv); QMainWindow *win1= new QMainWindow(); win1->resize(567,578); win1->move(67,30); win1->show(); QMainWindow *win2= new QMainWindow(); win2->resize(567,578); win2->move(97,580); win2->show(); win1->show(); //win2->setModal(true); QPushButton *but1 = new QPushButton(win1); but1->resize(80,20); but1->move(100,100); but1->setText("1"); but1->show(); QPushButton *but2 = new QPushButton(win2); but2->resize(80,20); but2->move(100,300); but2->setText("2"); but2->show(); while(1){ if((rand()%2) == 1){ //win2->lower(); win1->raise(); win1->activateWindow(); win1->setWindowState(Qt::WindowActive); win1->setFocus(Qt::ActiveWindowFocusReason); but1->setFocus(Qt::ActiveWindowFocusReason); } else{ //win1->lower(); win2->raise(); win2->activateWindow(); win2->setFocus(Qt::ActiveWindowFocusReason); but2->setFocus(Qt::ActiveWindowFocusReason); } qApp->processEvents(0x00); sleep(2); } But the problem is the title bar of the first window is not changing color(usually putting a window back-n-forth through the visual stack changes the color of the title-bar), even when it has become the top window visually
sleepin Qt. Create a slot and useQTimerto call this slot periodically.