Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set a Label on top of all others

Set a Label on top of all others

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danga96
    wrote on last edited by
    #1

    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 :)

    J.HilkJ 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      1 Reply Last reply
      1
      • D Offline
        D Offline
        danga96
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • D danga96

            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 :)

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @danga96

            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
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              danga96
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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)));
                
                D 1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @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)));
                  
                  D Offline
                  D Offline
                  danga96
                  wrote on last edited by
                  #8

                  @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?

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved