QT always on top on windows7/8 - c++

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();

Related

How can i stop QT Widget from closing?

I have a class (Window) that inherits from QWidget (Not QMainWindow).
This is the important part of my main function.
QApplication app (argc, argv);
QMainWindow *window = new QMainWindow;
QMenuBar *tool_bar = new QMenuBar (window);
Window *graph_area = new Window (arguments);
window->setMenuBar (tool_bar);
window->setCentralWidget (graph_area);
window->show ();
app.exec ();
I thought that i would just override "closeEvent", but for some reason it doesn't get called when pressing the closing button.
I want to stop the user from closing the application if some process is still working.
Any ideas about how i might achieve that?
You can either:
Subclass QMainWindow, instead of QWidget
Override QMainWindow::closeEvent, instead of QWidget::closeEvent
Allocate your QMainWindow subclass on the stack, instead of on the heap, i.e.:
MyMainWindowSubclass window;
or:
Change your code to:
QApplication app(argc, argv);
Window w;
window->show();
app.exec();
and Window::closeEvent will be called.
Make your Window class an event filter and install it on the main window object. You’ll reimplement filterEvent method in Window. Do not forget to install it on the main window! And make sure that the filter filters the event out - the boolean result indicates whether you let the result through or filter it out. Look up the exact value needed to indicate either one - it’s a bad api and I never remember whether true or false does what I intend. It’s a classical case where an enumeration return value would work better, like Qt::Accept/Qt::Reject or some such.
I actually found another solution to my problem, that I find more pleasant. I can create a custom class mainwindow that inherits from QMainWindow and use it instead of QMainWindow. It might be more appropriate, because I will probably need some other changes to QMainWindow as well.

How to access QMainWindow from a QWidget which parent is not QMainWindow

I am programming a GUI with Qt Creator (Qt5.10) under OSX. I have a QMainWindow for the main app's window. Inside this I have a QTab widget, in which I have entire widget (let's call it CaptureTabWidget which contains some additional widgets AND a QFrame (the parent of this QFrame is the QTab). In this last widget I will be drawing some stuff. The point is that I would like to write in the statusBar of the QMainWindow some values related to the position of the mouse in that QFrame, but I do not have any idea about how to access the main window from a method inside the CaptureTabWidget which contains the QFrame.
The whole picture goes like this:
X
+-----------+ XXX
|MainWindow | XXXXXXXXX
+-----+-----+ XXX XXXXXX
| X XXX
+-----v------------+ XX
|QTabWidget | XX
|No parent | XX
+-----+------------+ XX
| X
+-----v-------------+ ?? X
|CaptureTabQWidget | X
|Parent:QTabWidget | X
+-----+-------------+ X
| X
+-----v-------------+ XX
|QFrame | XXXXXX
|Parent:CaptureTab | XXX
+-------------------+
I have tried modifying the CaptureTabWidget's constructor in order to receive a QMainWindow* which points to the main window, but it seems Qt Creator doesn't allow custom constructors when promoting widgets in the Designer. I am wondering what is the right way to do this.
If you just aim to write to the statusBar of the parent QMainWindow, you can post a QStatusTipEvent to any child of your QMainWindow, and the event will keep propagating to the parent QWidgets until it reaches the main window where it gets handled by by changing the content in the status bar. In other words, something like this in your QFrame does the job:
QCoreApplication::postEvent(this, new QStatusTipEvent("your tip"));
// ^^^^
// assuming this is a pointer to your QFrame object
As a side note, this is the same way views in Qt (QTableView, QTreeView, QListView, ...) are able to change status tips (when their models provide a custom value for the role Qt::StatusTipRole) while they might be buried deep down in the QObject tree...
For purposes other than just writing to the statusBar of a parent QMainWindow, I would try to generalize the above approach by having a custom QEvent (i.e. one that has a value between QEvent::User and QEvent::MaxUser) and handle that event in my QMainWindow subclass appropriately. I find this way much cleaner and more maintainable than other solutions that depend on global variables/singletons (that keep track of your supposedly one QMainWindow instance).

Floating sub QMainWindow (QMainWindow as child widget of main QMainWindow)

I use a QMainWindow as child of my main QMainWindow. By that I get an other area which I can use for dockable widgets (QDockWidget).
According to the following posts this is OK, it also works perfectly for me.
https://qt-project.org/forums/viewthread/17519
http://www.qtcentre.org/threads/12569-QMainWindow-as-a-child-of-QMainWindow
To make the QMainWindow behaving as a normal widget, I unset the window flag, this trick is mentioned in one of the posts above.
Now I also want to be able to float this child QMainWindow with all its docked widgets. In other words, I want to revert the step "making it a normal widget". Unfortunately, this does does not work. It is gone from the main window, but not visible at all.
Any way to resolve it?
// this is the child QMainWindow
if (this->m_infoAreaFloating)
{
// this should give me a floating window besides the main window
this->setWindowFlags(Qt::Desktop);
this->show();
}
else
{
// make this compliant as QWidget
this->setWindowFlags(this->windowFlags() & ~Qt::Window);
}
Related: a , b
The Qt::Desktop flag is not something you are supposed to set by yourself.
You need to set the Qt::Window flag:
setWindowFlags(m_infoAreaFloating ? Qt::Window : Qt::Widget);
show();
There's no point to this->windowFlags() & ~Qt::Window: you've cleared all other window flags when setting the lone Qt::Window flag. You're in full control of the flags, there's no need to preserve some "other" flags: there are none.

Setting Windows Flag on a QDialog hides the form

I currently have a form that inherits from QDialog.
Now in order to hide the ? icon on the form I am doing something like this in the constructor.
foo::foo(QWidget *parent): QDialog(parent)
{
.....
this->setWindowFlags(Qt::WindowTitleHint);
}
The problem with this is that the Dialog does not show up. If I ommit the flags line it shows up. I am using QT 5.1.1
To answer the question, here the solution:
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
If you want the minimize and maximize options, do the following:
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint) | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
Eventually you want to call
this->setWindowFlags(this->windowFlags() | Qt::WindowTitleHint);
To get this to work on linux I had to use both options described above:
setFixedSize(width(), height());
setWindowFlags(Qt::Drawer);
The result is a dialog with only a close button.

setWindowFlags(Qt::WindowStaysOnTopHint) hides Qt Window

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