Results 1 to 9 of 9

Thread: QButtonBox, QDialog should not close on "Abort" or "Cancel"

  1. #1
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Hi,

    my QDialog with a QBUttonBox close automatically of i press "Abort" or "Cancel".
    Is there a way to configure the QButtonBox NOT to close the QDialog automatically on "Abort" or "Cancel"?

    Thx

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

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Those buttons are usually connected to the dialog's reject() slot.
    This slot is also invoked when the dialog is closed via the window decoration or global shortcut (e.g. ALT+F4).

    You can easily intercept this form of closing by overwriting reject() and just returning when you don't want to close, and just calling the base class implementation if you do.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    QDialog::done(int r) will work too
    Validate-Data-in-QDialog
    it will accept Accepted() or Rejected() signal before it really executed

  4. #4
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Quote Originally Posted by anda_skoa View Post
    Those buttons are usually connected to the dialog's reject() slot.
    This slot is also invoked when the dialog is closed via the window decoration or global shortcut (e.g. ALT+F4).

    You can easily intercept this form of closing by overwriting reject() and just returning when you don't want to close, and just calling the base class implementation if you do.

    Cheers,
    _
    Tte problem is, that reject() is a signal and not a slot.
    http://doc.qt.io/qt-4.8/qdialogbuttonbox.html#rejected

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

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Quote Originally Posted by HappyCoder View Post
    Tte problem is, that reject() is a signal and not a slot.
    No, it isn't. It is a slot.

    Yes, that is a signal. With a different name. On a different class.

    Cheers,
    _

  6. #6
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Ok, my fault, i looked at a wrong class. I have now overloaded QDialog::reject() inside my dialogtest.cpp

    I have no idea how to modify the
    Qt Code:
    1. void DialogTest::reject()
    To copy to clipboard, switch view to plain text mode 
    that it only call done(Rejected) if the button "Abort" was pressed. Hope you could
    give me hint, still a Qt and C++ beginner since 3 month
    Thx


    Qt Code:
    1. #include "dialogtest.h"
    2. #include "ui_dialogtest.h"
    3.  
    4. DialogTest::DialogTest(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::DialogTest)
    7. {
    8. ui->setupUi(this);
    9. this->setModal( true );
    10.  
    11. connect( ui->buttonBox, SIGNAL(clicked(QAbstractButton*)),
    12. this, SLOT(buttonClicked(QAbstractButton*)));
    13. }
    14.  
    15. DialogTest::~DialogTest()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void DialogTest::buttonClicked( QAbstractButton *button )
    21. {
    22. QDialogButtonBox::StandardButton stdButton = ui->buttonBox->standardButton(button);
    23. switch (stdButton)
    24. {
    25. case QDialogButtonBox::Ok:
    26. qDebug() << Q_FUNC_INFO << "OK";
    27. break;
    28. case QDialogButtonBox::Discard:
    29. qDebug() << Q_FUNC_INFO << "Discard";
    30. break;
    31. case QDialogButtonBox::Abort:
    32. qDebug() << Q_FUNC_INFO << "Abort";
    33. break;
    34. case QDialogButtonBox::Cancel:
    35. qDebug() << Q_FUNC_INFO << "Cancel";
    36. break;
    37. case QDialogButtonBox::Apply:
    38. qDebug() << Q_FUNC_INFO << "Apply";
    39. break;
    40. default:
    41. qDebug() << Q_FUNC_INFO << "button not handled";
    42. break;
    43. }
    44. }
    45.  
    46. void DialogTest::reject()
    47. {
    48. qDebug() << Q_FUNC_INFO << "QDialog::reject()";
    49. done(Rejected);
    50. }
    51.  
    52. void DialogTest::accept()
    53. {
    54. qDebug() << Q_FUNC_INFO << "QDialog::reject()";
    55. done(Accepted);
    56. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    I am not sure what you want to do now.

    You asked for not closing the dialog on abort or cancel.
    The way to do that is to overwrite reject() and return until you want to close.
    If you want to close, you call the base implementation, i.e. QDialog::reject().

    Now you write that you want to close when Abort is clicked?
    That seems to be the opposite of what you wrote before.

    Maybe you should clarify what you want to happen exactly when.

    Cheers,
    _

  8. #8
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    that it only call done(Rejected) if the button "Abort" was pressed.
    That was obviously because
    Those buttons are usually connected to the dialog's reject() slot.
    AND your reject() slot is
    Qt Code:
    1. void DialogTest::reject()
    2. {
    3. qDebug() << Q_FUNC_INFO << "QDialog::reject()";
    4. done(Rejected);
    5. }
    To copy to clipboard, switch view to plain text mode 

    So if you want to not closing the dialog, you should make handler for "abort" button inside void reject() slot
    something like -- IF "abort" pressed then return, ELSE done(rejected) --

  9. #9
    Join Date
    Sep 2015
    Posts
    12
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QButtonBox, QDialog should not close on "Abort" or "Cancel"

    Quote Originally Posted by HappyCoder View Post
    Hi,

    my QDialog with a QBUttonBox close automatically of i press "Abort" or "Cancel".
    Is there a way to configure the QButtonBox NOT to close the QDialog automatically on "Abort" or "Cancel"?

    Thx
    This is odd behavior for a modal dialog. You must only choose 'accept' ?

    You could make the dialog modeless (use show/hide instead of exec) if you want to have it always visible without interfering with other parts of the user interface(?). You could also disable the 'cancel' button (?).

    To prevent 'abort' or 'cancel' from closing the dialog you can subclass the dialog closeEvant(QCloseEvent*) virtual method. If something tries to close the dialog (including clicking on the little 'x' in the title bar) you can block it here.

Similar Threads

  1. QButtonBox, how to find index of QDialogButtonBox::Ok
    By HappyCoder in forum Qt Programming
    Replies: 8
    Last Post: 4th September 2015, 11:57

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.