Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPushButton is pressed automatically in nested QDialog
Forum Updated to NodeBB v4.3 + New Features

QPushButton is pressed automatically in nested QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    equeim
    wrote on last edited by
    #1
    #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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      E 1 Reply Last reply
      1
      • mrjjM mrjj

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

        E Offline
        E Offline
        equeim
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          E 1 Reply Last reply
          0
          • mrjjM mrjj

            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

            E Offline
            E Offline
            equeim
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                equeim
                wrote on last edited by equeim
                #7

                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?

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved