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. How to set a layout in the center of a Widget?
Forum Updated to NodeBB v4.3 + New Features

How to set a layout in the center of a Widget?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 12.1k 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.
  • S Offline
    S Offline
    siaswar
    wrote on last edited by
    #1

    I have a widget (stackedWidget). I put all buttons and labels for a page in a layout and sat the layout as widget's layout. But I want may buttons and labels (actually the layout) appear in the center or special location of widget.
    how can I set this?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      you can use the horizontal spacers to do this. When the window is resized it will keep the button in the middle.

      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        You don't position layouts, they fill their parent widgets. You can either use spacers, or simply calculate the center of your parent widget and move your child widget there manually.

        You have a geometry() function which returns the QRect of the parent widget, for QRect you have a center() function, which returns a QPoint which is the very center of the rectangle, which you can use for the child widget's move(QPoint&) function to move it to those coordinates.

        @// move child widget to its parent's geometry center
        yourWidget.move(yourWidget.parentWidget()->geometry().center());
        @

        note - parentWidget() is a function that returns a pointer to the parent widget, not the actual name of the parent widget

        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