Qt is activating window without my consent after using QFileDialog - qt

Qt 5.12.6 MSVC2017 64-bit on Windows 10, Qt Creator 4.10.1.
I have a top level window derived from QMainWindow, which has a QMdiArea.
From the first main window I use a file open dialog to open a new document in a QMdiSubWindow and place it in the QMdiArea. After the file dialog returns but before I do the logic of opening the document, I have to call QApplication::setActiveWindow(this), to set it back to the main window, because the opening logic depends on the active window. The need for this somewhat appears related to https://www.qtcentre.org/threads/2950-ActiveWindow-changes-after-closing-QFileDialog in that the activeWindow becomes null, regardless of how I parent the file dialog.
I then open a second identical top level window and open another document using the same method.
What's very weird is that after the second QMdiSubWindow in the second QMainWindow is activated, the first QMainWindow gets activated again, and thus various follow-on operations affect the document in the first window rather than the second window which is the intended one. If I click back and forth on the main windows, the activation is correct.
If I create a new document directly from the second QMainWindow instead of using the file open dialog, then it works fine. The new QMdiSubWindow is created and shown in the second QMainWindow, without the first one getting activated. So this implicates the file dialog somehow.
When I put a breakpoint on a function triggered by the QMainWindow::activated signal, the stack trace shows this signal is coming from within Qt on its own accord, not from someplace in my code directly.
How can I find out why Qt is activating the first window when using the file dialog from the second window? At this point the code is too large to try to fit a minimal example here on SO.
--EDIT--
I added a one-shot QTimer at the end of the function that creates the QMdiSubWindow. Zero milliseconds after in creates the QMdiSubWindow it activates the first QMainWindow, then activates the current QMainWindow. That workaround seems to work, and the second QMainWindow becomes the active one. But only if I select away from it first. The fact that the hack works with 0 ms timer is also interesting. I'll be posting minimal example when I can.
thanks

Related

QtQuick lower() and raise() does not work while close() works properly

I have 5 windows which i push and pop via a stackview. In the emulator the new window called by push gets on top as it should. However on my android it spawns behind the starting window. I tried using lower() to set the starting window behind the new window with no success
//start page
Button{myStack.push(page_2); lower()}
I also tried rasing the new window on completion with no success
//new page
Component.OnCompleted:raise()
However close() works properly closing the start page and thus making the new page visible.
//start page
Button{myStack.push(page_2); close()}
Qt doc goes as follows for lower()
lower()
Lowers the window in the windowing system.
Requests that the window be lowered to appear below other windows.
and for raise()
raise()
Raises the window in the windowing system.
Requests that the window be raised to appear above other windows.
Is my code correct? why does close() work and the others dont't
First of all, I don't think that Qt supports multi window on Android. One single window only.
Second - what you posted is not even valid QML code.
Third - in that case you use the the stack view within a single window, and push different GUI elements onto it. Refer to the documentation.

Destroy QWidget returned from Plugin in Qt

My app loads a plugin which creates a window (QWidget), but I can not destroy it when I exit from QMainWindow, obviously because widget returned from plugin is not a child of QMainWindow. The issue is that if I make that window to be a child of mainwindow, I get a window on another window. But I need them both to be separated. I did them separated (but main window has no control over window from plugin), in my case I do not know how to close window from plugin when app quits. How can I achieve that?
Just delete it. If you don't assign it a parent, no other widget has ownership. So you should simply be able to destroy it yourself on exit.

Qt signals and slots working

I have created a Main Window which consists of menu bar with the first menu being "file". Now I am trying to open another window if I click on the "file" menu item. But I am not getting the receiver object of the other window while designing. How can I establish a link?
I am using Qt 4.7
I don't think you can do it by manual linking - this time you will have to write it yourself :P
Simply add a new slot to your window's class, add include for second's window header. Then in slot implementation create new window if necesary and simply call show on it.
Secondly you have to connect file meny to your signal - best is to add connect call in your window constructor AFTER intializing ui.
Also note that if your file option contains actually any submenu, then it's signal will never be emmited, so you need to think about something else in such case.

Qt show() function after hide(), worked when the window inactive but not active

I have made a simple qt program that grabs a picture and display it using QPainter/QPaintEvent widget. I make it on QPainter so that I can put a X mark on the picture from a user input. I have made the program window to hide and show based on an external button input. It works well if I set the running program window inactive. But if I set the window to active and triggered the show/hide function, it crashes with error:
Fatal IO error: client killed
Also, when I tried to use show() function after hide(), it gives 2-3 of a same error (applies when the window either active and inactive):
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in another thread
Is this a bug in qt? Any suggestion on workaround this problem?

Closing new nonchild window in Qt

i'm trying to make 2 windows. 2nd should be called in 1st. I don't connect them child->parent.
But when i called 2nd window and closed it 1st window closed too. What should i do?
Both windows are inhereted from QWidget.
C++ and Qt
Sorry for my poor describe.
I have a main window. Class inherited from QMainWindow. That i created 2nd window. Class inherited from QWidget.
In first (main window) i'm creating and calling 2nd window
ConfigWindow *ConfWindow = new ConfigWindow();
ConfWindow->show();
Without giving link to parent. Everything works fine, but when i close 2nd window (config-window) my main window is closing too. I needn't in this. What should i do to block closing main window after config-window closing.
Hope describe a little better.
My first window has this flags:
this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
Without them everything is fine. Could i change something if i need that flags in my window?
You need something like:
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
Here is the test program: http://pastebin.com/f5903c5f4.
Beware, now you need to explicitly call quit() in the destructor of your main window.
If you read QApplication::quitOnLastWindowClosed documentation, you will find out that:
If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except for sub-windows
Because your main window is a (frameless) tool window, it does count. That leaves ConfWindow as the only non sub-windows top-level widget. Thus, if you close ConfWindow, it provokes the application instance to quit.
If this is the code, then there is a huge bug in Qt.
The code above should never close your first Windows, there must be something else wrong.
Is the application closed or does it crash?
Remark
Who is deleting configWindow? There is a Qt::WA_DeleteOnClose attribute that deletes the window after it is closed.
ConfigWindow *confWindow = new configWindow();
configWindow->setAttribute(Qt::WA_DeleteOnClose, true);
confWindow->show();

Resources