PDA

View Full Version : MainWindow Button



waynew
5th December 2009, 23:11
I have a pushbutton on the main window that is set to be the default.
Now, how can I make it click with the enter key?

spirit
7th December 2009, 07:34
you can install event filter on you button or subclass QPushButton and reimplement keyPressEvent.

JD2000
7th December 2009, 15:11
Have you connected its 'clicked()' signal to anything?

Assuming you used the designer to create the mainwindow, right click on the button and go to its clicked slot and implement what you want the button to do here. Being the default button, it should automatically receive focus when the main window opens.

the following code will pop up a Hello World messagebox.



void MainWindow::xxxButton_clicked()
{
QMessageBox::critical(0, "HELLO MSG","Hello World");
}

I hope this helps.

spirit
7th December 2009, 16:22
Have you connected its 'clicked()' signal to anything?

Assuming you used the designer to create the mainwindow, right click on the button and go to its clicked slot and implement what you want the button to do here. Being the default button, it should automatically receive focus when the main window opens.

the following code will pop up a Hello World messagebox.



void MainWindow::xxxButton_clicked()
{
QMessageBox::critical(0, "HELLO MSG","Hello World");
}

I hope this helps.
author wants handle enter key press, how does it connect with clicked?

JD2000
7th December 2009, 16:49
I may be misunderstanding the documentation, but the following has been cut and pasted directly from the QPushButton class reference notes:

"A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button's action. Push buttons also provide less commonly used signals, for example, pressed() and released().

Command buttons in dialogs are by default auto-default buttons, i.e. they become the default push button automatically when they receive the keyboard input focus. A default button is a push button that is activated when the user presses the Enter or Return key in a dialog. You can change this with setAutoDefault(). Note that auto-default buttons reserve a little extra space which is necessary to draw a default-button indicator. If you do not want this space around your buttons, call setAutoDefault(false).

Being so central, the button widget has grown to accommodate a great many variations in the past decade. The Microsoft style guide now shows about ten different states of Windows push buttons and the text implies that there are dozens more when all the combinations of features are taken into consideration.

The most important modes or states are:

* Available or not (grayed out, disabled).
* Standard push button, toggling push button or menu button.
* On or off (only for toggling push buttons).
* Default or normal. The default button in a dialog can generally be "clicked" using the Enter or Return key.
* Auto-repeat or not.
* Pressed down or not."

This seems to indicate that my advice is correct.

waynew
26th December 2009, 22:36
Sorry JD2000, I probably didn't state the problem clearly.

The pushbutton works fine. I just want it clicked when the Enter key is hit.
This is automatic on the default button in a dialog, but not a mainwindow.

Here is what I have tried but it does nothing:



ui->pbSave->installEventFilter(this);

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->pbSave && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Enter) {
ui->pbSave->click();
return true;
} else
return false;
}
return false;
}


I am probably not understanding the event filter doc correctly.
Any help appreciated.

spirit
27th December 2009, 09:23
here is work example


Test::Test(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *vbl = new QVBoxLayout(this);
m_pbTest = new QPushButton(tr("test"));
m_pbTest->installEventFilter(this);
vbl->addWidget(m_pbTest);
connect(m_pbTest, SIGNAL(clicked()), SLOT(testClicked()));
}

bool Test::eventFilter(QObject *o, QEvent *e)
{
if (o == m_pbTest && e->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
m_pbTest->click();
}
return QWidget::eventFilter(o, e);
}

void Test::testClicked()
{
qDebug() << Q_FUNC_INFO << "clicked";
}

numbat
27th December 2009, 13:48
If you want the button clicked on enter when it has the focus, you can just call setAutoDefault with true. If you want it clicked when pressing enter while it doesn't have the focus, you will have to intercept events for the QMainWindow rather than the button. You can do this by overriding the event function for QMainWindow.


bool MainWindow::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
{
qDebug() << "click";
ui->pbSave->click();
return true;
}
}
return QMainWindow::event(event);
}

waynew
27th December 2009, 14:14
Thanks folks. Now I understand the possible approaches to the problem much better.

Working fine now.

Thanks to all of the experienced ones who are willing to help us learners!