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. Beginner's questions about QLayout
Forum Updated to NodeBB v4.3 + New Features

Beginner's questions about QLayout

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 535 Views
  • 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
    StudyQt1
    wrote on last edited by StudyQt1
    #1

    Hi, previously I was using QML, I am new to C++ GUI and I would like to 2 questions, I would like to have sth like:
    sample.jpg

    1. How can I specify a position and an area to put a grid layout? (Red rectangle)
    2. How can I position a single text (Blue rectangle) to the right of the grid? I know in QML I should use anchor, in C++ should I use setGeometry to specify the position? (Basically I don't want to put that single text into layout)

    Thank you very much

    JonBJ 1 Reply Last reply
    0
    • S StudyQt1

      Hi, previously I was using QML, I am new to C++ GUI and I would like to 2 questions, I would like to have sth like:
      sample.jpg

      1. How can I specify a position and an area to put a grid layout? (Red rectangle)
      2. How can I position a single text (Blue rectangle) to the right of the grid? I know in QML I should use anchor, in C++ should I use setGeometry to specify the position? (Basically I don't want to put that single text into layout)

      Thank you very much

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @StudyQt1
      I have not used QML so I cannot compare against that.

      QWidgets can have a position/size/geometry, but the whole point of using QLayouts is not to specify these, rather to allow the layout manager to figure this out. Once you put widgets on a layout you do not use widget coordinate positioning. You should start by reading Layout Management.

      (Basically I don't want to put that single text into layout)

      Well, yes you do! You have two items which you want arranged horizontally, with the blue box at the top of the horizontal layout. So your layout would be a QHBoxLayout. You would first use QHBoxLayout::addWidget(redBox) to add the red box widget, and then a second QHBoxLayout::addWidget(blueBox, Qt::AlignTop) to add the blue box to the right but aligned at the top.

      You can also add (sub-)layouts to layouts, instead of/as well as widgets. If your "Area to put a grid" is not just, say, a QTableView/Widget widget for your "grid" but is actually a grid layout to hold a number of individual QWidgets etc., then the first item you would add to your outer layout would be QHBoxLayout::addLayout(QGridLayout) instead of addWidget() (or you might a dd a generic QWidget there and in turn use its setLayout(QGridLayout)). Then off you go and add widgets --- or further sub-layouts --- to the grid layout.

      You can use Designer in Qt Creator to play with adding layouts & widgets to see how they will appear and line up, etc.

      The usual recommendation would be to use layouts rather than widget coordinate positioning if you can/unless you have some particular coordinate need. It will take care of changing device & window sizing for you appropriately, leaving you to specify the layout in "logical" terms.

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @StudyQt1
        I have not used QML so I cannot compare against that.

        QWidgets can have a position/size/geometry, but the whole point of using QLayouts is not to specify these, rather to allow the layout manager to figure this out. Once you put widgets on a layout you do not use widget coordinate positioning. You should start by reading Layout Management.

        (Basically I don't want to put that single text into layout)

        Well, yes you do! You have two items which you want arranged horizontally, with the blue box at the top of the horizontal layout. So your layout would be a QHBoxLayout. You would first use QHBoxLayout::addWidget(redBox) to add the red box widget, and then a second QHBoxLayout::addWidget(blueBox, Qt::AlignTop) to add the blue box to the right but aligned at the top.

        You can also add (sub-)layouts to layouts, instead of/as well as widgets. If your "Area to put a grid" is not just, say, a QTableView/Widget widget for your "grid" but is actually a grid layout to hold a number of individual QWidgets etc., then the first item you would add to your outer layout would be QHBoxLayout::addLayout(QGridLayout) instead of addWidget() (or you might a dd a generic QWidget there and in turn use its setLayout(QGridLayout)). Then off you go and add widgets --- or further sub-layouts --- to the grid layout.

        You can use Designer in Qt Creator to play with adding layouts & widgets to see how they will appear and line up, etc.

        The usual recommendation would be to use layouts rather than widget coordinate positioning if you can/unless you have some particular coordinate need. It will take care of changing device & window sizing for you appropriately, leaving you to specify the layout in "logical" terms.

        S Offline
        S Offline
        StudyQt1
        wrote on last edited by StudyQt1
        #3

        @JonB Thank you very much for your reply! Looks like in most C++ Qt GUI, "layout" is the fundamental, it always fills the parent widget area, and I don't need to care about the layout's elements positions. This is different from QML GUI, in QML though I use layout at the fundamental layer I still use "anchor" a lot

        JonBJ 1 Reply Last reply
        0
        • S StudyQt1

          @JonB Thank you very much for your reply! Looks like in most C++ Qt GUI, "layout" is the fundamental, it always fills the parent widget area, and I don't need to care about the layout's elements positions. This is different from QML GUI, in QML though I use layout at the fundamental layer I still use "anchor" a lot

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @StudyQt1 said in Beginner's questions about QLayout:

          it always fills the parent widget area, and I don't need to care about the layout's elements positions

          Indeed, that is their point. This scales well when users have all sorts of different screen resolutions, windowing systems etc. Layouts make a Qt GUI behave somewhat like an HTML page in terms of adjusting elements to fit. There are also stretches, spacers and various alignments available on layouts to control how it fits the available area if you wish.

          You can use absolute positioning and size with widgets if you want instead of layouts, but mostly you do not want to do that.

          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