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. Label Positioning is off
Forum Updated to NodeBB v4.3 + New Features

Label Positioning is off

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.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.
  • D Offline
    D Offline
    Daepilin
    wrote on last edited by
    #1

    Hi :)

    I wanted to have 2 labels, each for half the width of the window with text centered. Below that 3 buttons, each a third of the screens. The Buttons work, but the text is kinda of...

    Code is:
    @ StartPopulationLabel = new QLabel(this);
    StartPopulationLabel->setAlignment(Qt::AlignCenter);
    StartPopulationLabel->setGeometry(0, 530, 225, 20);
    StartPopulationLabel->setText("Populaton:");

    RefreshTimeLabel = new QLabel(this);
    RefreshTimeLabel->setAlignment(Qt::AlignCenter);
    RefreshTimeLabel->setGeometry(225, 530, 225, 20);
    RefreshTimeLabel->setText("Time between Refreshs:");
    
    StartButton = new QPushButton(this);
    StartButton->setGeometry(0, 580, 183, 20);
    StartButton->setText("Start");
    
    StopButton = new QPushButton(this);
    StopButton->setGeometry(183, 580, 184, 20);
    StopButton->setText("Stop");
    
    ResetButton = new QPushButton(this);
    ResetButton->setGeometry(367, 580, 183, 20);
    ResetButton->setText("Reset");@
    

    A screenshot:

    !http://s14.directupload.net/images/130205/v7wsplbq.png(Screenshot)!

    the window itself is 550px wide, so normaly it should work.

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

      Why are you calculating those positions by hand in the first place? Imagine some poor fella will have to add another label to that code someday. Ouch. Qt has layouts for that.

      This code does what you need:
      @
      StartPopulationLabel = new QLabel("Populaton:", this);
      StartPopulationLabel->setAlignment(Qt::AlignCenter);

      RefreshTimeLabel = new QLabel("Time between Refreshs:", this);
      RefreshTimeLabel->setAlignment(Qt::AlignCenter);
      
      StartButton = new QPushButton("Start", this);
      StopButton  = new QPushButton("Stop",  this);
      ResetButton = new QPushButton("Reset", this);
      
      QGridLayout* layout = new QGridLayout();
      layout->addWidget(StartPopulationLabel, 0, 0, 1, 3);
      layout->addWidget(RefreshTimeLabel,     0, 3, 1, 3);
      layout->addWidget(StartButton,          1, 0, 1, 2);
      layout->addWidget(StopButton,           1, 2, 1, 2);
      layout->addWidget(ResetButton,          1, 4, 1, 2);
      
      yourParentWidget->setLayout(layout);
      

      @
      and the result (with red borders to see better): !http://img339.imageshack.us/img339/108/layoutsp.jpg(layout example)!

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

        Problem with layouts( or at least because i haven't used layouts before and because of that no idea how to use them):

        above those labels i have a 500px*500px area where i want to paint a grid(this whole thing is going to become a GameofLife), and i haven't found a way to position a layout starting at a fixed position.

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

          That's no problem, just use this:
          @
          layout->addWidget(Your500x500Widget, 0, 0, 1, 6);

          layout->addWidget(StartPopulationLabel, 1, 0, 1, 3);
          layout->addWidget(RefreshTimeLabel, 1, 3, 1, 3);
          layout->addWidget(StartButton, 2, 0, 1, 2);
          layout->addWidget(StopButton, 2, 2, 1, 2);
          layout->addWidget(ResetButton, 2, 4, 1, 2);
          @
          A tip: learn to use layouts. They are what every UI programmer should know and use. They take care of proper sizing, spacing and handle window resizing. Thanks to them you can concentrate on what your app is doing instead of calculating +/- 1px shifting of 200 ui elements when you decide to add a border somewhere :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Daepilin
            wrote on last edited by
            #5

            yeah, but that 500500px grid is no widget, at least not in its current state.

            But i guess i could make it into one by painting into a seperate widget and adding that to the layout.

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

              For the sake of readability and modularity you should probably do it anyway. But if you just want some extra space, layouts have flexible margins. Just use layout->setContentsMargins(9,500,9,9) and you'll get a space for the drawings.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Daepilin
                wrote on last edited by
                #7

                yea, got it working, even though i had to find out how the heck to draw in a child Widget without making it into an own class... (need an array from the main class to control the drawing)

                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