Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Issues aligning elements through the use of anchors
QtWS25 Last Chance

Issues aligning elements through the use of anchors

Scheduled Pinned Locked Moved Qt Creator and other tools
7 Posts 3 Posters 2.0k 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.
  • U Offline
    U Offline
    unixfan
    wrote on last edited by
    #1

    Coming from a wxwidget background, I recently decided to try Qt and see whether it would get me anywhere.
    Unfortunately, I'm already stuck. Using Qtcreator, I modified the "Hello World" example to keep the text anchored near the top left corner.

    Thinking this was enough, I resized the window. This, however, moved the text near the center of the window.

    Am I doing something wrong? I tried to do the same via grid + column + row and it wouldn't work either.

    Library is Qt Quick 1.1 and I'm using HsQML to load the UI into my Haskell project.

    Here's the QML code:

    @import QtQuick 1.1

    Rectangle {
    id: rectangle1
    width: 360
    height: 360
    Text {
    width: 69
    height: 15
    text: "Hello World"
    anchors.left: parent.left
    anchors.leftMargin: 8
    anchors.top: parent.top
    anchors.topMargin: 8
    }
    MouseArea {
    anchors.right: parent.right
    anchors.bottom: parent.bottom
    anchors.top: parent.top
    anchors.left: parent.left
    onClicked: {
    Qt.quit();
    }
    }
    }@

    EDIT: Is there any way to use more elements (stuff like tabs, spacers, etc) from within the Qt Quick project suite or will I have to write it in code? A regular Qt Gui project seems to contain quite a number of additional options compared to the Qt Quick Project.

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on last edited by
      #2

      Hi,
      The code you posted should do the work. You can try to check where your rectangle is drawn to make sure it's not the rectangle that is not placed in the place you expect by giving it a color.

      As for more advanced controls like tabs and others you can have a look at QtQuick.Controls but this will require to switch your code to Qt 5 and QtQuick 2.

      Best regards.

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

        Thanks. Got it now.

        I was under the, apparently quite wrong, impression that the root (i. e.the rectangle, in this case) object would always fill the screen. Turned out that the whole rectangle moved to the middle, rather than scaling with the window.

        What's would be the best way to make sure elements like the rectangle/grid/etc always scales with the window? From a design view, is it a good idea to have the font inflate as well or should the whole layout just reshuffle in a way that keeps elements tightly bound to each other?

        QtQuick.Controls looks interesting. Qt5/QtQuick isn't part of HsQML yet, though. Probably will be a few months before it's release, unfortunately.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unixfan
          wrote on last edited by
          #4

          This has to be the most frustrating experience in a long time.

          Is there no QML specific way to set an element to always fill the screen or, at the very least, to disable resizing on a window?

          All infos I can find online deal with C++, which I don't have at my disposal for this project.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            unai_i
            wrote on last edited by
            #5

            I don't know Haskell at all but from the screenshots shown in HsQML website I guess that you can fill your window if you find a means to pass width and height of your window to QML.
            At first you can simply try something along:
            @
            import QtQuick 1.1

            Item { // unless you need to explicitly change background color you don't need Rectangle
            id: root Item
            anchors.fill: parent
            Text {
            width: 69
            height: 15
            text: "Hello World"
            anchors.left: parent.left
            anchors.leftMargin: 8
            anchors.top: parent.top
            anchors.topMargin: 8
            MouseArea {
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.top: parent.top
            anchors.left: parent.left
            onClicked: {
            Qt.quit();
            }
            }
            }
            }

            @

            If you can fill your window, then you can do the layout as you want.
            However, I have no clue how you can do if your need is to resize the window to your content.
            Hope it helps.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              oakes
              wrote on last edited by
              #6

              I started using HsQML and have run into the same problem; I can't figure out how to make the content update when the window is resized. I tried adding "anchors.fill: parent" to the root item, but that causes the window to be a small square, ignoring the width/height I set for it. Is there no way to make the root item follow the window size via QML?

              1 Reply Last reply
              0
              • O Offline
                O Offline
                oakes
                wrote on last edited by
                #7

                I found that the solution is to make the root element a Window, and add "visible: true" to it. I can now resize the window and the contents resize with it.

                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