Results 1 to 7 of 7

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,540
    Thanked 284 Times in 279 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
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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,
    _

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
  •  
Qt is a trademark of The Qt Company.