Set a Label on top of all others
-
hello to the whole community Qt Forum,
I have a Label that must necessarily be placed above all the windows of my editor.
These are all the combinations I've tried:QLabel *label = new QLabel(myEditor); label->topLevelWidget(); label->setWindowFlags(Qt::WindowStaysOnTopHint); label->raise();
but none of these, either individually or all together, work;
Thanks in advance :) -
topLevelWidget()
is a getter, it sets nothing so is useless for what you're trying to do
setWindowFlags(Qt::WindowStaysOnTopHint)
is the right flag, but your widget is not a window, so it has no effect.
raise()
is also used on windows and your label is not.First of all make your label a window and then you can use that
WindowStaysOnTopHint
:QLabel *label = new QLabel(myEditor, Qt::Window | Qt::WindowStaysOnTopHint); label->show();
If you don't want the window frame around the label add another flag to that constructor:
Qt::FramelessWindowHint
. -
@Chris-Kawa
Thanks for the reply; but if I put the Qt :: Windows flag I can no longer use my editor as a reference for the Label, because now I do it outside the window. -
Ok, I'm confused about what you're trying to do then. You said you want it above all other windows in your app. For that it needs to be a window too.
Can you describe better what you imagine the result would look like? -
raise() actually should work fine. As it should raise the widget on the top of the parent widget's stack.
But it should also be not needed, the last created child/sibling widget is by default on top of all others.
My guess is, your label has no size, as it's not part of a layout.
QLabel *label = new QLabel(myEditor); label->resize(myEditor->size()); label->raise(); label->show(); //just in case, should be unnecessary
-
@Chris-Kawa ,@J-Hilk
Thank you for your reply.
I try to explain myself better: I have a Label that I must have in my texteditor in a certain position, using the "label-> move (x, y)" instruction, these x and y are not absolute but relative to the editor; so if I can't use these coordinates, how can I convert them absolutely? -
@danga96 said in Set a Label on top of all others:
so if I can't use these coordinates, how can I convert them absolutely?
You can use mapToGlobal() on the widget these coordinates are relative to, so something like
label->move(textEditor->mapToGlobal(QPoint(x, y)));
-
@Chris-Kawa
Thanks, but now every time I move the Label it is like updating the window, and the editor cursor loses its position forcing the user to reposition it with a click of the mouse; is there any flag that avoids this?