Qt: How do I get the currently running window? - c++

I'm writing a test app which simulates key presses and I would like to get what window is displayed after each key presses. Here's the code block.
std::auto_ptr<MyForm> pForm(new MyForm(3,3));
QTest::keyPress(pForm.get(), Qt::Key_0);
After pressing 0 here, A window is gonna show up and I would like to check what window it is so I could QCompare/evaluate it later.
Any Ideas?
Updated:
I'm getting a segmentation fault when I use
std::auto_ptr<MyForm> pForm(new MyForm(3,3));
QTest::keyPress(pForm.get(), Qt::Key_0);
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(pWin->windowTitle(), QString("My Second Menu"));

If all your windows have been created through your application, you can use the QApplication class.
By example, the activeWindow() function returns the widget that have the input focus. But there's a lot of other functions that can help you.
Hope that helps

Related

Qt OS X hiding MainWindow freezes GUI

I've found that when pressing CMD+H on my Mac and then tabbing back into the application my GUI is frozen until i minimize and maximize the window.
Any ideas what could be causing this?
I've tried:
Removing QTimer
Removing eventFilter
I'm out of ideas on what to try here.
Edit
I tried to create a new simply application that just contained a form and a text input. I deployed it using macdeployqt and I get the exact same behavior. The GUI freezes after a hit CMD+H.

Qt 5.1 - QMessageBox Bug? Program Exits(0) if QMessageBox is called while QDialog is hidden

I seem to have discovered an annoying issue with Qt 5.1.
Let's say for example you have a system tray icon (QSystemTrayIcon) and you hide your form (QDialog), so:
this->hide();
Then, while the form is hidden, your app displays a message box:
QMessageBox::information(0, "Test", "Test");
Once the user hits Ok to close the dialog, the program exits with exit code 0. So, it doesn't crash, but it politely exits.
The only work around that I know off is to use the WIN32 API on Windows and the MessageBox function. This is not what I want to do.
Is this a bug?
By default, a Qt application closes when the last window is closed (in your case, when you close the QMessageBox).
You can add this code to keep your application running:
qApp()->setQuitOnLastWindowClosed(false);

How to disable user interaction in a Qt application when a DialogBox shown?

I have a modeless QDialog box that popup on errors/warning in my Qt application, I want to force the user to only focus on that dialog box and not click anything in the application until they clicked Ok on the dialog box.
I need the dialog box to remain modeless. A solution like hiding the main window or covering it up is not acceptable.
At the moment I'm using setModal(true); to solve my problem. But I think this method might be stopping the main application from executing.
From the documentation:
If you use show() and setModal(true) together to perform a long
operation, you must call QApplication::processEvents() periodically
during processing to enable the user to interact with the dialog.
Instead of using a QDialog box, try using qDebug statements in your code or a log file using qInstallMsgHandler.
You could also show a QTextEdit and post your log/error messages there in real time, too.
http://qt-project.org/doc/qt-4.8/debug.html
http://qt-project.org/doc/qt-4.8/qdebug.html#details
http://qt-project.org/doc/qt-4.8/qtglobal.html#qInstallMsgHandler
http://qt-project.org/doc/qt-4.8/qtextedit.html#details
If you still really want to debug using a QDialog box for errors, in a pseudo modal dialog but not modal dialog, you could try using eventFilters to prevent mouse and keyboard events from arriving at any other window, but it would be tricky to allow the exception to end up only at QDialog, but it is do-able.
You could also go to the one or two widgets that accept mouse and keyboard input, and ignore the input if the a QDialogBox is visible. But both of these ways of showing an error, but limiting input without making it Modal is really hacky, and would probably be error prone.

QT unit-testing : qtestlib Segmentation fault

I'm writing a test app that would test if the displayed form is the correct form. This is after pressing a key on a menu. Here's a code block where I'm getting a segmentation fault.
std::auto_ptr<MyForm> pForm(new MyForm(3,3));
QTest::keyPress(pForm.get(), Qt::Key_0);
QWidget *pWin = QApplication::activeWindow();
QCOMPARE(pWin->windowTitle(), QString("My Second Menu"));
Questions:
Is it really possible to get the currently active window when you are just simulating key presses?
I'm getting a null pointer when using activeWindow, is there anyway you could get the handle of the window that's supposed to show on the screen upon a keypress?
Thanks...
Frank was right. The window has not been acitivated synchronously. Adding a delay which is a parameter of keyPress did resolve the problem. Thanks Frank!

Show window in Qt without stealing focus

I'm using the Qt library to show a slideshow on the second monitor when the user isn't using the second monitor. An example is the user playing a game in the first monitor and showing the slideshow in the second monitor.
The problem is that when I open a new window in Qt, it automatically steals the focus from the previous application. Is there any way to prevent this from happening?
It took me a while to find it but I found it: setAttribute(Qt::WA_ShowWithoutActivating);
This forces the window not to activate. Even with the Qt::WindowStaysOnTopHint flag
If you want to make floating preview box/ any other widget just use below
thumbnail = new QLabel;
thumbnail->setAttribute(Qt::WA_ShowWithoutActivating);
thumbnail->setParent(0);
thumbnail->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
Qt::Tool is important flag to make it work. I mean not stealing focus.
Widgets don't accept focus by default but presumably you haven't created a plain widget? Which subclass was it? QMainWindow or something else?
It's possible the window subclasses default to accepting focus so try explicitly calling QWidget::setFocusPolicy with Qt::NoFocus before calling QWidget::show().
Also, make sure you're not calling QWidget::activateWindow() on the window or any of its widgets at any point.