c++ - How to set Focus on a specific widget -
i'm trying implement gui various widgets opengl project. have main widget drawing scene. opengl widget associated key , mouse events, therefore focus should on it. i've noticed if click on push button, focus moved button means focus no longer associated opengl widget. clicking widget mouse not changing focus. 1 of solution turn off focus widgets except opengl widget in gui follows
ui->processbutton->setfocuspolicy(qt::nofocus); ui->quitbutton->setfocuspolicy(qt::nofocus); ui->clearbutton->setfocuspolicy(qt::nofocus); ui->textedit->setfocuspolicy(qt::nofocus); ui->groupbox->setfocuspolicy(qt::nofocus);
if have many widgets, solution annoying if add widgets later on. question is there solution set focus on specific widget?
your solution fine, shouldn't enumerating widgets manually:
// c++11 (auto widget : findchildren<qwidget*>()) if (! qobject_cast<qopenglwidget*>(widget)) widget->setfocuspolicy(qt::nofocus); // c++98 foreach (qwidget * widget, findchildren<qwidget*>()) if (! qobject_cast<qopenglwidget*>(widget)) widget->setfocuspolicy(qt::nofocus);
Comments
Post a Comment