Details
-
Task
-
Resolution: Done
-
P2: Important
-
5.15.2, 6.2.0 RC
-
None
-
macOS Catalina v10.15.7
-
-
223066d4319d611e724da69089c8f49d525a2566 (qt/qtbase/dev) 92d2d31e1a8a03ed56750b5c1c32b5cd22216b99 (qt/qtbase/6.2)
Description
When QDialog::open() is used, the focus is given always to the first widget in the dialog even though another widget was explicitly set to be focused.
After many trials and errors, I found workaround: Setting setWindowModality(Qt::WindowModal) for the dialog solves the problem and focus is set properly. Anyway this is a bug to fixed.
See snippet. Try it as it is and button b1 will be focused, which is wrong. Uncomment the line and button b2 will be focused, which is correct.
#include <QApplication> #include <QDialog> #include <QPushButton> #include <QVBoxLayout> class Widget : public QDialog { public: Widget(QWidget *parent = nullptr) : QDialog(parent) { setLayout(new QVBoxLayout()); auto b1 = new QPushButton("A"); layout()->addWidget(b1); auto b2 = new QPushButton("B"); layout()->addWidget(b2); b2->setFocus(); // Setting focus to b2 !!! } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; // When this line is commented out, button b1 gets focus !!! //w.setWindowModality(Qt::WindowModality::WindowModal); w.open(); return a.exec(); }