setWindowFlags(Qt::WindowStaysOnTopHint) hides Qt Window - c++

I want to make my Qt Window stay on top. When setWindowFlags(Qt::WindowStaysOnTopHint) is executed, the window becomes hidden (on Windows 7).
I've also tried:
Qt::WindowFlags flags = windowFlags();
setWindowFlags(flags | Qt::WindowStaysOnTopHint);
And, it still does the same. What did I do wrong here?

Call show() after you set the flag:
Qt::WindowFlags flags = windowFlags();
setWindowFlags(flags | Qt::WindowStaysOnTopHint);
show();
Check out http://doc.qt.io/qt-5/qwidget.html#windowFlags-prop

Related

Enable maximize button in QWizard

I have a Windows application that is built on QWizard (which inherits from QDialog). It must have a working maximization button.
By default maximization button is not even visible. i have set it to show, using:
auto flags = windowFlags();
flags ^= Qt::WindowContextHelpButtonHint;
flags |= Qt::WindowMinMaxButtonsHint;
setWindowFlags(flags);
However, it shows up disabled (grayed out, non-responding).
How can i enable it?
This works for me:
setWindowFlags(windowFlags() | Qt::CustomizeWindowHint |
Qt::WindowMinimizeButtonHint |
Qt::WindowMaximizeButtonHint |
Qt::WindowCloseButtonHint);
According to the documentation, you have to use the Qt::CustomizeWindowHint to be able to change the individual hints on the min/max buttons.
Someone here says this solved his problem:
setWindowFlags(Qt::Window);
I believe that you'll get better results creating your own dialog, but if you really wanna do it, one way is use window styles (Windows only, not cross-plataform).
Wizard class example:
class wizard : public QWizard
{
public:
wizard() {}
~wizard() {}
protected:
bool event(QEvent *event)
{
#ifdef Q_OS_WIN /*Make this code Windows OS only*/
if (event->type() == QEvent::WinIdChange)
{
HWND hwnd = (HWND)winId();
LONG lStyle = GetWindowLong(hwnd, GWL_STYLE);
lStyle |= (WS_MINIMIZEBOX | WS_MAXIMIZEBOX); /*Enable minimize and maximize*/
SetWindowLong(hwnd, GWL_STYLE, lStyle);
}
#endif
return QWizard::event(event);
}
};
I have this:
QWizard *wizard = new QWizard(this, Qt::CustomizeWindowHint | Qt::WindowMaximizeButtonHint | Qt::Window);
wizard->setSizeGripEnabled(true);
Running Windows 10 on my dev-box, Qt 5.5.1, working for me.
One of my pages is a big QTableWidget that ends up being like an Excel sheet of some sorts (a big page to verify and edit-in-place a lot of data). Making the window resizable and let the user maximize it if they want makes it much easier to work with, instead of having to constantly scroll in a small dialog.
Normally you would say: If you need such a big window, it probably shouldn't be in a QWizard. But in this case it's really the middle of a workflow thing. A big 'verify, edit-if-needed and continue' page so it would be weird to stop the QWizard before and then having to start another one after or something.

QT always on top on windows7/8

I would like to know if it's possible to set my QMainWindow always on top .
I tried:
mainWindow.setWindowFlags(Qt::WindowStaysOnBottomHint);
mainWindow is a QMainWindow extended object.
But it doesn't work and my window disapear.
Yes, it is possible but there are two errors in your code:
You are clearing all flags but Qt::WindowStaysOnBottomHint which is set.
You're using Qt::WindowStaysOnBottomHint flag (which represent the opposite of what you want) instead of Qt::WindowStaysOnTopHint.
A correct way of doing that is:
Qt::WindowFlags flags = mainWindow.windowFlags();
mainWindow.setWindowFlags(flags | Qt::WindowStaysOnTopHint);
Note that on some window managers on X11 you also have to pass
Qt::X11BypassWindowManagerHint for this flag to work correctly.
In that case you should do:
Qt::WindowFlags flags = mainWindow.windowFlags();
mainWindow.setWindowFlags(flags | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
If you want to make a window as Dialog, there is another way. Just call setModal(true) or setWindowModality(), afterward show(). Unlike exec(), show() returns control to the caller instantaneously.It wont stuck as QDialog in exec().
i.e
setModel(true);//In Constructor
then while calling or invoking the new window,
MyWindow* myWindow = new MyWindow(this);
myWindow->show();

Clear a flag/hint in Qt

I'm trying to undo Stay on Top of Windows settings on my app. I thought that by performing some bitwise operations, I will be able to undo the setting, but it is still showing on top of all the other windows.
void showKioskMode(){
//if(windowFlags()&Qt::WindowStaysOnTopHint){
if(ui->pushButton_3->text().compare("No Kiosk") == 0){
//showNormal();
Qt::WindowFlags flags = windowFlags();
flags &= ~Qt::WindowStaysOnTopHint;
setWindowFlags(flags);
ui->pushButton_3->setText("Yes Kiosk");
}
else{
//showFullScreen();
Qt::WindowFlags flags = windowFlags();
setWindowFlags(flags | Qt::WindowStaysOnTopHint);
ui->pushButton_3->setText("No Kiosk");
}
show();
}
I've checked that the if-body is being executed, but the window is still always on top of all other windows even though they have focus.
Looks like that it is a bug. The solution can be found in the following thread:
https://bugreports.qt-project.org/browse/QTBUG-30359
https://stackoverflow.com/a/2860768/1129149 - See OP's (Jake Petroules) answer.
It has a separate solution for Windows via WinAPI.

I can't seem to add a close button using Qt::WindowFlags

I have the following code that is called when I insert a QMdiSubWindow into a QMdiArea:
Qt::WindowFlags flags;
flags = Qt::Widget | Qt::WindowMinimizeButtonHint | Qt::WindowTitleHint;
if(closeable)
{
qDebug("Window is closeable. %x", Qt::WindowCloseButtonHint);
flags |= Qt::WindowCloseButtonHint;
}
For some reason, even when closeable is true, the closebutton won't display on the widget's titlebar.
This is the call to insert the widget into the QMdiArea.
mdi->addSubWindow(widget, flags);
Any suggestions?
I found that playing around with the window flags example included with the sdk was a lot of help when trying to get the flags correct.
C:\QtSDK\Examples\4.7\widgets\windowflags\

Can't add minimize button to QDialog under linux

I'm trying to add a minimize button to my QDialog using this code in the constructor:
Qt::WindowFlags flags = windowFlags();
flags |= Qt::WindowMinMaxButtonsHint;
setWindowFlags(flags);
It's working on Windows but not on Linux.
Its a late answer but could be useful to others, I had the same problem and fixed like so:
Qt::WindowFlags flags = Qt::Window | Qt::WindowSystemMenuHint
| Qt::WindowMinimizeButtonHint
| Qt::WindowCloseButtonHint;
this->setWindowFlags(flags);
inside the overridden dialog constructor.