PDA

View Full Version : How to attach closeEvent (QCloseEvent *event) to a Button



Spinter
2nd December 2014, 10:08
I have the next doubt, I have an exit button created on the graphic interface, and I want to attach the closeEvent to him I have the next code but I couldnt make it work.



MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);


}
void MainWindow::on_exit_clicked()
{
closeEvent( );
}

void MainWindow::closeEvent(QCloseEvent *event) {
QMessageBox::StandardButton resBtn = QMessageBox::question( this, "Images",
tr("Are you sure to exit?\n"),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resBtn != QMessageBox::Yes) {
event->ignore();
} else {
event->accept();
}
}

Lesiok
2nd December 2014, 10:43
Replace line 12 with :
close();
Of course, the button name is "exit", right?

Spinter
2nd December 2014, 10:58
I suppose, it work but where I could find the name of buttons I mean I only use the mainwindow.ui to define all of them.

If it is that what you are referring to yes the name is exit


<item row="6" column="3">
<widget class="QPushButton" name="exit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>50</width>
<height>50</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string>Salir</string>
</property>
</widget>
</item>


Thank you.

Added after 5 minutes:

Another question I have about that, sory xD I am too noob can I modify the text display whenI use that messageBox I mean:



QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes;



For example , instead appear "Yes" appear "ok" in the box, or instead "No" , "Not really"

anda_skoa
2nd December 2014, 11:11
Or directly connect the button's clicked() signal to the close() slot.

Cheers,
_

Lesiok
2nd December 2014, 11:21
Another question I have about that, sory xD I am too noob can I modify the text display whenI use that messageBox I mean:



QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes;



For example , instead appear "Yes" appear "ok" in the box, or instead "No" , "Not really"

Yes You can but not with static method question(). Read more about QMessageBox.

Spinter
2nd December 2014, 12:29
Ok I try with no static



QMessageBox msgBox;
QPushButton *buttonYes = msgBox.addButton(tr("Ok"), QMessageBox::Yes);
QPushButton *buttonNo = msgBox.addButton(tr("Not really"), QMessageBox::No);
QPushButton *buttonCancel = msgBox.addButton(tr("Cancel"), QMessageBox::Cancel);


msgBox.exec();

if (msgBox.clickedButton() == buttonYes) {
event->accept();
} else {
event->ignore();
}



But I am obtaining next error

error: no matching function for call to 'QMessageBox::addButton(QString, QMessageBox::StandardButton)'
QPushButton *buttonYes = msgBox.addButton(tr("Ok"), QMessageBox::Yes);
^

Added after 55 minutes:

Well I solved it the problem was I needed a buttonRole not Yes, but YesRole

INeedADollar
10th April 2020, 19:09
I know it's a bit too late, but just call static function QApplication::exit() . This function will call the close event function with suitable QCloseEvent object.