Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to conditionally switch between Row and Column Layout
Qt 6.11 is out! See what's new in the release blog

How to conditionally switch between Row and Column Layout

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 3.2k 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.
  • R Offline
    R Offline
    refaQtor
    wrote on last edited by
    #1

    What is a good pattern for conditionally inserting QML items?
    For example:
    as I've tried to show in pseudocode below, I wish to layout my items in a Column when the screen is narrow, but in a Row otherwise.

    if (parent.width < 400) {
    
        Column {
            Label {
                id: label
               text: "column label"
            }
            TextField {
                id: ceditText
                text: "column text edit"
            }
        }
    
    } else {
    
        Row {
            Label {
                id: rlabel
                text: "row label"
            }
            TextField {
                id: reditText
                text: "row text edit"
            }
        }
    
    }
    

    I can't put the javascripty conditional if/else around them in QML (or, can I?) Is there some attribute of every Item that I can use to include/exclude based on a conditional value?

    Any ideas would be helpful. Thanks.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      refaQtor
      wrote on last edited by
      #2

      Oh, I got it. For this limited case anyway, I'm conditionally switching the columns: between 2 and 1

         GridLayout {
              id: grid
              columns: ( parent.width < 200 ? 1 : 2 )
      ...
         }
      

      But, if anyone wanted to chime it, that would be helpful. This only covers most of my cases.

      E 1 Reply Last reply
      0
      • R refaQtor

        Oh, I got it. For this limited case anyway, I'm conditionally switching the columns: between 2 and 1

           GridLayout {
                id: grid
                columns: ( parent.width < 200 ? 1 : 2 )
        ...
           }
        

        But, if anyone wanted to chime it, that would be helpful. This only covers most of my cases.

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        @refaQtor You can just use the flow property and leave the number of columns/rows undefined:

        GridLayout {
                id: grid
                flow:  parent.width < 200 ? GridLayout.TopToBottom : GridLayout.LeftToRight
        ...
           }
        
        1 Reply Last reply
        0
        • R Offline
          R Offline
          refaQtor
          wrote on last edited by
          #4

          That seems like a more elegant solution to work in all cases. But, I must've mixed up something else, because the widgets overlay themselves with your suggestion. Maybe I'll try to figure it out later.
          Anyway, I've got it working the way I want now.
          Thanks!

          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