QPushButton is pressed automatically in nested QDialog



  • #include <QApplication>
    #include <QDebug>
    #include <QDialog>
    #include <QPushButton>
    #include <QTreeWidget>
    #include <QVBoxLayout>
    
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
    
        QDialog dialog;
        QVBoxLayout layout(&dialog);
    
        QTreeWidget treeWidget;
        treeWidget.insertTopLevelItem(0, new QTreeWidgetItem(&treeWidget));
        QObject::connect(&treeWidget, &QTreeWidget::activated, [&treeWidget]() {
            auto secondDialog = new QDialog(&treeWidget);
            auto layout = new QVBoxLayout(secondDialog);
            auto button = new QPushButton();
            QObject::connect(button, &QPushButton::clicked, []() {
                qDebug() << "button clicked";
            });
            layout->addWidget(button);
            secondDialog->show();
        });
        layout.addWidget(&treeWidget);
    
        dialog.show();
    
        return app.exec();
    }
    

    Select an item in the QTreeWidget, then press Enter. Second dialog opens and the button in it is immediately clicked. Why is this happen?


  • Qt Champions 2016

    Hi
    Win 10, mingw compiler , Qt 5.7
    I only get "button clicked" when i click button in dialog.
    Its not auto triggered.



  • @mrjj said in QPushButton is pressed automatically in nested QDialog:

    Hi
    Win 10, mingw compiler , Qt 5.7
    I only get "button clicked" when i click button in dialog.
    Its not auto triggered.

    Do you open second dialog by pressing Enter button or mouse double-click? I have this issue on Arch Linux with Qt 5.7, Debian with Qt 5.3 and Windows 8.1 with Qt 5.7.


  • Qt Champions 2016

    Hi
    DBclick and dont get issue.
    But I can reproduce with Enter.
    Seems like the key get from treeWidget to the new dialog and trigger the default button



  • @mrjj said in QPushButton is pressed automatically in nested QDialog:

    Hi
    DBclick and dont get issue.
    But I can reproduce with Enter.
    Seems like the key get from treeWidget to the new dialog and trigger the default button

    Also it will not happen if I create second dialog without parent or replace root dialog with QWidget. I have no idea how this is connected.


  • Qt Champions 2016

    Im a bit surprised why activated dont eat the enter key.



  • I made another exaple to better illustrate the issue:

    #include <QApplication>
    #include <QDebug>
    #include <QDialog>
    #include <QPushButton>
    #include <QTreeWidget>
    #include <QVBoxLayout>
    
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
    
        QDialog dialog;
        QVBoxLayout layout(&dialog);
        QTreeWidget treeWidget;
        treeWidget.insertTopLevelItem(0, new QTreeWidgetItem(&treeWidget));
        layout.addWidget(&treeWidget);
        dialog.show();
    
        QDialog dialog2(&dialog);
        QVBoxLayout layout2(&dialog2);
        QPushButton button;
        QObject::connect(&button, &QPushButton::clicked, []() {
            qDebug() << "button clicked";
        });
        layout2.addWidget(&button);
        dialog2.show();
    
        return app.exec();
    }
    

    I you select an item of QTreeWidget in the first dialog and press Enter, it presses the button in the second dialog. This issue can be resolved by subclassing QTreeWidget and accepting all Qt::Key_Enter/Qt::Key_Return events, but it looks like a bug.

    UPD: I figured this out. Button in second dialog is pressed because QTreeWidget ignores enter key event and passes it to treeWidget's parent, first dialog. Then dialog searches recursively in its children for default buttons and presses first found. Maybe QTreeWidget (more precisely, QAbstractItemView) should accept this event?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.