How can I prevent QDialog class from closing after "Ok" button was pressed? I need to close the window only if the some actions were performed correctly on this dialog, in other cases I don't need to close this window.
相关问题
- Sorting 3 numbers without branching [closed]
- QML: Cannot read property 'xxx' of undefin
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
The same as from @kuba-ober, but using custom property. Change one function:
Now
styleSheet...
functions are not necessary but we need to add constant style sheet for the dialog or application:Or if use Qt designer, add root widget property:
Generally speaking, it's a bad habit to lie to the user. If a button is not disabled, then it better work when the user clicks on it.
So, the obvious solution is to disable the button until the necessary preconditions are met. For buttons that finish the dialog, you should be using
QDialogButtonBox
instead of discrete buttons, since on different platforms those buttons will be arranged differently within the box - based on the roles/types of the buttons.Below is an example of how it might be done. Works with Qt 4 and 5.
Care has been taken for the code to interoperate with existing stylesheets.
First, let's have some stylesheet manipulation helpers:
And a recursive parent search:
The
DialogValidator
manages validators for a single dialog box. First, the slots invoked when the contents of aQLineEdit
and a generic widget change:The validators are added to the dialog validator using the
add
methods. If we desire special handling for a dynamically typed widget, theaddPoly
method should be used - it'll dispatch to type-specific overloads, if any:Then, the static-typed
add
methods:And finally, the convenience method that constructs the validator:
Our dialog with validation:
While you probably shouldn't make a button available that won't do anything when the user clicks it, if you absolutely must override the default close behavior, then you need to override
QDialog::accept()
.