How to attach closeEvent (QCloseEvent *event) to a Button
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.
Code:
MainWindow
::MainWindow(QWidget *parent
) :
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::on_exit_clicked()
{
closeEvent( );
}
tr("Are you sure to exit?\n"),
event->ignore();
} else {
event->accept();
}
}
Re: How to attach closeEvent (QCloseEvent *event) to a Button
Replace line 12 with :Of course, the button name is "exit", right?
Re: How to attach closeEvent (QCloseEvent *event) to a Button
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
Code:
<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:
For example , instead appear "Yes" appear "ok" in the box, or instead "No" , "Not really"
Re: How to attach closeEvent (QCloseEvent *event) to a Button
Or directly connect the button's clicked() signal to the close() slot.
Cheers,
_
Re: How to attach closeEvent (QCloseEvent *event) to a Button
Quote:
Originally Posted by
Spinter
Another question I have about that, sory xD I am too noob can I modify the text display whenI use that messageBox I mean:
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.
Re: How to attach closeEvent (QCloseEvent *event) to a Button
Ok I try with no static
Code:
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
Re: How to attach closeEvent (QCloseEvent *event) to a Button
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.