Solved Bring QFileDialog to the front
-
I use exec() call to pop a qfiledialog up and found it is minimized while it is used in a DLL and its parent is not the mainwindow. Googled it and tried all I could find: modality, flags, raise, etc and could not solve this problem. Need help. Thanks in advance
-
@JoeCFD
Can you show me your code? -
QFileDialog * dialog = new QFilieDialog( this);
dialog->setWindowTitle( "App" );
dialog->setWindowModality( Qt::ApplicationModal );
dialog->setWindowFlags( Qt::WindowStaysOnTop );
dialog->setAcceptMode(QFileDialog::AcceptSave );
....
if ( QDialog::Accepted == dialog->exec() )
{
do something
} -
@JoeCFD
What isthis
, which you pass as the parent toQFileDialog( this)
?
I'm not sure how that combines withsetWindowModality( Qt::ApplicationModal )
anyway....
I have not had to usesetWindowModality( Qt::ApplicationModal )
norsetWindowFlags( Qt::WindowStaysOnTop );
, but I admit I haven't done Windows/DLLs.I think from what you write that if you ensure you pass the main window as the parent to
new QFileDialog( this)
you will get you want. -
@JonB Right.Here "this" is another widget, not the mainwindow. If I pass my mainwindow as this, it is all right. However, I can not use it since this piece of code is in a DLL. The mainwindow is not available.
-
@JoeCFD
Then I think that is your problem, because IIRCQFileDialog
likes the main window not a widget as its parent if it's to behave modally the way you want.If that is true, the DLL is running in your program's user space, so when you say "The mainwindow is not available" you can always go get it. That's what I've done in my code (though nothing to do with DLLs/Windows). You might at least try that to see if it solves your problem?
-
@JonB I can send a signal to the mainwindow to do the popup over there and solve the problem. But it is not convienent. Thanks anyway.
-
@JoeCFD It turns out it is a virtual machine issue. On Linux within a virtual machine this problem shows up. Not sure why. No problem on a standalone Linux.
-
I can send a signal to the mainwindow to do the popup over there and solve the problem. But it is not convienent. Thanks anyway.
You could, but that wasn't what I intended, I understand that would mean re-architecting. I meant: discover it where you open the file dialog, in your DLL, without having to pass it around in code. If you're interested, I wrote:
def findMainWindow() -> typing.Union[QMainWindow, None]: # Global function to find the (open) QMainWindow in application app = QApplication.instance() for widget in app.topLevelWidgets(): if isinstance(widget, QMainWindow): return widget return None
-
@JonB Got it.This is something new for me. Thanks a lot.
something like the following works.
QWidget * widget{ nullptr };
for ( widget : QApplication::toLevelWidgets() )
{
if ( widget->objectName() == QString( "MyMainWindow" ) )
{
break;
}
} -
@JoeCFD
If this helps you I am glad.If you wanted to write the direct equivalent of Python's
if isinstance(widget, QMainWindow)
in C++ you should be able to useif (dynamic_cast<QMainWindow*>(widget))
. -
@JonB My C++ code is good enough to solve the issue. No casting is needed. Many thanks again.