Unsolved Problem with first window hiding/closing
-
Hey there, I have been trying out some stuff with the Qt Designer and I have stumbled upon the following problem.
I called the function below from my mainWindow and the result should be that it opens the second window and hides/closes the first window. Strangely it does open the second window but refuses to hide/close the first one. Does anyone know why?void WidgetMain::get_editor() { launcher = FindWindow(NULL, TEXT("Editor")); if (!launcher) { qDebug() << "Editor not detected."; QTimer::singleShot(500, this, SLOT(get_editor())); } else { qDebug() << "Editor detected. Setting up."; widgetLauncher* launcherWindow = new widgetLauncher; launcherWindow -> show(); this -> hide(); } }
Debugger outputs the else statement but like I said doesnt hide/close the first window.
Help is really apreciated, thank you!
EDIT:
Posting the whole code of my two widgets (in case it helps):WidgetMain.cpp
#include "widgetmain.h" #include "widgetlauncher.h" #include "ui_widgetmain.h" #include "windows.h" #include "QDebug" #include "QTimer" WidgetMain::WidgetMain(QWidget *parent) : QWidget(parent) , ui(new Ui::WidgetMain) { ui->setupUi(this); // Frameless, clickthrough Widget with custom Opacity setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput); setAttribute(Qt::WA_TranslucentBackground, false); this -> setWindowOpacity(0.5); this -> setStyleSheet("background-color: black;"); // Display Widget on Desktop as long as Editor is not detected get_desktop(150, 50); // Set up a timer to check if LoL Launcher is present //QTimer* timerWindow = new QTimer(this); //connect(timerWindow, SIGNAL(timeout()), this, SLOT(get_launcher())); //timerWindow -> start(50); get_editor(); } WidgetMain::~WidgetMain() { delete ui; } void WidgetMain::get_desktop(int width, int height) { desktop = GetDesktopWindow(); if (!desktop) { qDebug() << "Desktop not detected."; } else { qDebug() << "Desktop detected. Setting position."; RECT rect; int offset = 70; if (GetWindowRect(desktop, &rect)) setGeometry(rect.right - width - offset, rect.bottom - height - offset, width, height); } } void WidgetMain::get_editor() { editor = FindWindow(NULL, TEXT("Code.txt - Editor")); if (!editor) { qDebug() << "Editor not detected."; QTimer::singleShot(500, this, SLOT(get_editor())); } else { qDebug() << "Editor detected. Setting up."; widgetLauncher* launcherWindow = new widgetLauncher; launcherWindow -> show(); this -> hide(); } }
WidgetMain.h
#ifndef WIDGETMAIN_H #define WIDGETMAIN_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class WidgetMain; } QT_END_NAMESPACE class WidgetMain : public QWidget { Q_OBJECT public: WidgetMain(QWidget *parent = nullptr); ~WidgetMain(); private: Ui::WidgetMain *ui; HWND desktop; HWND editor; private slots: void get_desktop(int width, int height); void get_editor(); }; #endif // WIDGETMAIN_H
widgetLauncher.cpp
#include "widgetlauncher.h" #include "ui_widgetlauncher.h" #include "windows.h" #include "QDebug" #include "QTimer" widgetLauncher::widgetLauncher(QWidget *parent) : QWidget(parent), ui(new Ui::widgetLauncher) { ui->setupUi(this); // Frameless, clickthrough Widget with custom Opacity setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput); setAttribute(Qt::WA_TranslucentBackground, false); this -> setWindowOpacity(0.5); this -> setStyleSheet("background-color: black;"); set_position(150, 50); } widgetLauncher::~widgetLauncher() { delete ui; } void widgetLauncher::set_position(int width, int height) { editor = FindWindow(NULL, TEXT("Code.txt - Editor")); if (!editor) { qDebug() << "Editor not detected."; } else { qDebug() << "Editor detected. Setting position."; RECT rect; int offset = 70; if (GetWindowRect(editor, &rect)) setGeometry(rect.right - width - offset, rect.bottom - height - offset, width, height); } }
widgetLauncher.h
#ifndef WIDGETMAIN_H #define WIDGETMAIN_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class WidgetMain; } QT_END_NAMESPACE class WidgetMain : public QWidget { Q_OBJECT public: WidgetMain(QWidget *parent = nullptr); ~WidgetMain(); private: Ui::WidgetMain *ui; HWND desktop; HWND editor; private slots: void get_desktop(int width, int height); void get_editor(); }; #endif // WIDGETMAIN_H
My two are a widget with a vertical layout and two labels with some text in it which look like here in the bottom right:
-
@Cion Hi!
Works for me. On which OS are you and what Qt version do you use? Do widgetLauncher and WidgetMain do anything special? -
@jsulm hey there!
Im on Windows 10 64Bit and am using Qt 5.15.2 (as of qDebug() << QT_VERSION_STR;). Everything my program does is now listed in the first post, but unfortunately for me the first window just doesnt close or hide when the second one is started up. -
@Cion
The code looks OK. As @jsulm has confirmed it works for him, if I were you I would try cutting your program down. Remove all your other code, get rid of your "special" widgets, get rid of the time. Just have one simple window/widget open another and see how you go. Assuming that works, build back up to your actual case. -
@Cion said in Problem with first window hiding/closing:
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput);
Try to remove this and see whether it changes the behaviour.
This is one example for "special" I was talking before. -
@jsulm I tried removing that part but still the same problem
@JonB
I tried it from scratch using two widgets (main and sub) which both have a button. On button click I can successfully hide/close mainwidget and open subwidget and vice-versa for the subwidget.
The next thing I tried was using the QTimer singeShot inside the buttonClicked function like this and this:void WidgetMain::on_subWindowButton_clicked() { editor = FindWindow(NULL, TEXT("Code.txt - Editor")); if (!editor) { qDebug() << "Editor not detected."; QTimer::singleShot(500, this, SLOT(on_subWindowButton_clicked())); } else { WidgetSub* subWindow = new WidgetSub; subWindow -> show(); this -> close(); } }
As a result the debugger throws out "Editor not detected." every .5 seconds until I open the file. Then the first window closes and the second one appears. So works like a charm.
After implementing my function without a buttonClicked function like in my first post I found out that as long as the "Editor" is not open and I start my application the debugger outputs "Editor not detected." and once I start the editor the first window disappears and the second one appears.
The problem is in the case when my "Editor" is open already for some reason my first widget doesnt close but the second one opens up.Any ideas why that is happening? Thanks for your help till now!
-
@Cion said in Problem with first window hiding/closing:
editor = FindWindow(NULL, TEXT("Code.txt - Editor"));
Why do you need this editor? What is your use case?
-
@jsulm
It really isnt about this editor. It's more about in general. I want to create some sort of overlay for some games I am playing but in those cases the gameclient isnt necessarily open et every given time. That's why I want the application to check whether the client is open or not and if it is I want to position the overlay at a specific position in there hence the QTimer and findWindow calls.