How should I best go about forcing a QDialog to stay open when the di开发者_开发百科alog's accept()
slot is called? I was thinking of reimplementing that function to get the dialog's exec to return without hiding the dialog, but was wondering if there were any better ways to accomplish this.
Rather than use a QDialog, I would accomplish the effect with a QDockWidget.
- Remove the feature that allows the dock to be moved (QDockWidget::DockWidgetMovable)
- Set the dock widget to floating (setFloating(true))
- Connect items on the dock widget to the appropriate signals and slots on the main window
References
- Dock Widgets Example
- QDockWidget Documentation
You need to make your QDialog
modeless, by calling show
instead of exec
, and using a custom signal instead of accept
, because accept
closes the window. And you connect that signal to a slot in the main window with the code you had after the exec
call.
And in case that wasn't already the case, you should keep a reference/pointer to your QDialog somewhere (as a member in your main window class, or a static variable within the function that opens it) to be able to avoid creating multiple instances of the dialog, and you need to make sure you only connect the signals/slots once.
精彩评论