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. Pushbutton problem
Forum Updated to NodeBB v4.3 + New Features

Pushbutton problem

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 858 Views 2 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.
  • DriftwoodD Offline
    DriftwoodD Offline
    Driftwood
    wrote on last edited by Driftwood
    #1

    I have a lineEdit next to a Save button and Cancel button. After entering text in the lineEdit, for some elusive reason, I can click the button with my mouse and it works. But, when I tab to the button and press enter, it doesn't work.

    I don't use Qt enough to know why it sees a problem with me pressing enter instead of mouse clicking it. this is what I have:

    void MainWindow::on_pushButton_SaveNewName_pressed()
    {
        on_pushButton_SaveNewName_clicked();
    }
    
    void MainWindow::on_pushButton_SaveNewName_clicked()
    {
        QSqlQuery q(db);
        if (!db.open()) {
            ui->statusbar->showMessage( db.lastError().text());
        } else {
            q.exec("PRAGMA foreign_keys = 1");
            q.prepare("INSERT INTO STUDIERS( STUDIER ) VALUES(" + quoteSQL(ui->lineEdit_AddNewName->text()) + ")");
            if(!q.exec()) qDebug() << q.lastError().text();
        }
        db.close();
    
        ui->lineEdit_AddNewName->setText("");
        ui->lineEdit_AddNewName->setVisible(false);
        ui->pushButton_SaveNewName->setVisible(false);
        ui->pushButton_CancelNewName->setVisible(false);
        ui->pushButton_AddNewName->setEnabled(true);
        ui->comboBox_SelectName->setEnabled(true);
        ui->menu_File->actions().at(0)->setEnabled( true);
    
        ui->comboBox_SelectName->clear();
        loadNames();
        ui->comboBox_SelectName->setFocus();
    }
    

    Why won't pressed work?

    Happy Thanksgiving, everyone!

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

      Hi,

      From the documentation:

      pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      DriftwoodD 1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        I think you need to set your Save button as the default one,
        see QPushButton::setDefault(bool)

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          From the documentation:

          pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button
          
          DriftwoodD Offline
          DriftwoodD Offline
          Driftwood
          wrote on last edited by
          #4

          @SGaist - Thanks for the information. But how do I make it so the button can be pressed when it has focus and the enter key's pressed?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            What you want to do is the standard behavior of QDialog (you're using a window/widget)
            Have a look at QDialogButtonBox Class and QDialog examples in the Qt docs.

            DriftwoodD 1 Reply Last reply
            0
            • M mpergand

              What you want to do is the standard behavior of QDialog (you're using a window/widget)
              Have a look at QDialogButtonBox Class and QDialog examples in the Qt docs.

              DriftwoodD Offline
              DriftwoodD Offline
              Driftwood
              wrote on last edited by Driftwood
              #6

              So the only way to click buttons is with the mouse? You can't tab to a button and "click" it with the <enter> key?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #7

                Look at this link

                They're talking about the space bar instead.

                In the Qt doc about setDefault()

                The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus.

                DriftwoodD 1 Reply Last reply
                1
                • M mpergand

                  Look at this link

                  They're talking about the space bar instead.

                  In the Qt doc about setDefault()

                  The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus.

                  DriftwoodD Offline
                  DriftwoodD Offline
                  Driftwood
                  wrote on last edited by
                  #8

                  @mpergand - Thank you for your help.

                  jsulmJ 1 Reply Last reply
                  0
                  • DriftwoodD Driftwood

                    @mpergand - Thank you for your help.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Driftwood pressed() is also called if button has focus and user presses space key (at least on my Linux).

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    DriftwoodD 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @Driftwood pressed() is also called if button has focus and user presses space key (at least on my Linux).

                      DriftwoodD Offline
                      DriftwoodD Offline
                      Driftwood
                      wrote on last edited by
                      #10

                      @jsulm Exact same for me. Why the <enter> key isn't mapped for this is beyond me. I know that I can toggle radio buttons and checkboxes with the spacebar. This is true for most Gui toolkits. But when I want to use the keyboard to press button, the <enter> key makes more sense than the spacebar. I come from wxWidgets, where the <enter> key works that way.

                      Thanks for responding.

                      1 Reply Last reply
                      0
                      • A.A.SEZENA Offline
                        A.A.SEZENA Offline
                        A.A.SEZEN
                        wrote on last edited by
                        #11

                        Maybe you should check qApp->focusWidget() when handling Qt::Key_Enter and Qt::Key_Return events in keyPressEvent.

                        1 Reply Last reply
                        0

                        • Login

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