Results 1 to 7 of 7

Thread: How to attach closeEvent (QCloseEvent *event) to a Button

  1. #1

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

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2.  
    3. QMainWindow(parent),
    4. ui(new Ui::MainWindow)
    5. {
    6. ui->setupUi(this);
    7.  
    8.  
    9. }
    10. void MainWindow::on_exit_clicked()
    11. {
    12. closeEvent( );
    13. }
    14.  
    15. void MainWindow::closeEvent(QCloseEvent *event) {
    16. QMessageBox::StandardButton resBtn = QMessageBox::question( this, "Images",
    17. tr("Are you sure to exit?\n"),
    18. QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
    19. QMessageBox::Yes);
    20. if (resBtn != QMessageBox::Yes) {
    21. event->ignore();
    22. } else {
    23. event->accept();
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to attach closeEvent (QCloseEvent *event) to a Button

    Replace line 12 with :
    Qt Code:
    1. close();
    To copy to clipboard, switch view to plain text mode 
    Of course, the button name is "exit", right?

  3. #3

    Default 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
    Qt Code:
    1. <item row="6" column="3">
    2. <widget class="QPushButton" name="exit">
    3. <property name="sizePolicy">
    4. <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
    5. <horstretch>0</horstretch>
    6. <verstretch>0</verstretch>
    7. </sizepolicy>
    8. </property>
    9. <property name="minimumSize">
    10. <size>
    11. <width>50</width>
    12. <height>50</height>
    13. </size>
    14. </property>
    15. <property name="layoutDirection">
    16. <enum>Qt::RightToLeft</enum>
    17. </property>
    18. <property name="autoFillBackground">
    19. <bool>false</bool>
    20. </property>
    21. <property name="text">
    22. <string>Salir</string>
    23. </property>
    24. </widget>
    25. </item>
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
    To copy to clipboard, switch view to plain text mode 

    For example , instead appear "Yes" appear "ok" in the box, or instead "No" , "Not really"
    Last edited by Spinter; 2nd December 2014 at 10:58.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to attach closeEvent (QCloseEvent *event) to a Button

    Or directly connect the button's clicked() signal to the close() slot.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to attach closeEvent (QCloseEvent *event) to a Button

    Quote Originally Posted by Spinter View Post
    Another question I have about that, sory xD I am too noob can I modify the text display whenI use that messageBox I mean:

    Qt Code:
    1. QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6

    Default Re: How to attach closeEvent (QCloseEvent *event) to a Button

    Ok I try with no static

    Qt Code:
    1. QMessageBox msgBox;
    2. QPushButton *buttonYes = msgBox.addButton(tr("Ok"), QMessageBox::Yes);
    3. QPushButton *buttonNo = msgBox.addButton(tr("Not really"), QMessageBox::No);
    4. QPushButton *buttonCancel = msgBox.addButton(tr("Cancel"), QMessageBox::Cancel);
    5.  
    6.  
    7. msgBox.exec();
    8.  
    9. if (msgBox.clickedButton() == buttonYes) {
    10. event->accept();
    11. } else {
    12. event->ignore();
    13. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by Spinter; 2nd December 2014 at 12:29.

  7. #7
    Join Date
    Jul 2019
    Posts
    31
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

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

Similar Threads

  1. How to use QCloseEvent to change QLineEdit
    By Kaluss in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2013, 23:58
  2. Replies: 1
    Last Post: 30th October 2011, 00:44
  3. QCloseEvent
    By scrasun in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2009, 10:44
  4. mouseMoveEvent(QMouseEvent* event), event->button()
    By faraslacks in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2008, 05:22
  5. Replies: 3
    Last Post: 24th September 2007, 08:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.