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. First item in gridlayout is in the middle of the Dialog
QtWS25 Last Chance

First item in gridlayout is in the middle of the Dialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
gridlayout
14 Posts 3 Posters 4.8k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    The following code puts the Title label in the middle of the age:

        QLabel  *Title	= new QLabel;
        QFont f("Arial", 18, QFont::Bold);
        Title->setFont (f);
        Title->setAlignment (Qt::AlignCenter);
        Title->setText ("ADDING A FRIEND TO THE DATABASE");
        //Title->setFixedWidth (800);
    
    	// Creating the scroll area
    
        QScrollArea *scroll = new QScrollArea(this);
        scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
        scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
    
        QWidget *viewport  = new QWidget(this);
        scroll->setWidget (viewport);
        scroll->setWidgetResizable (true);
    
        // Adding layout
    
        QGridLayout *grid = new QGridLayout;
        grid->addWidget (scroll);
        Additem::setLayout (grid);
    
    	grid->addWidget (Title,0,0,0,4);
    

    How can I move it to the top of the page (or to the top left corner)?
    Thank you.

    kshegunovK 1 Reply Last reply
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by Ni.Sumi
      #2

      Hi @gabor53 ,

      void QGridLayout::addWidget(QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0)

      check Calculator example for gridlayout.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        Hi,
        Unfortunately```
        grid->addWidget (Title,0,0,1,4);

        still puts the title in the middle.
        1 Reply Last reply
        0
        • G gabor53

          Hi,
          The following code puts the Title label in the middle of the age:

              QLabel  *Title	= new QLabel;
              QFont f("Arial", 18, QFont::Bold);
              Title->setFont (f);
              Title->setAlignment (Qt::AlignCenter);
              Title->setText ("ADDING A FRIEND TO THE DATABASE");
              //Title->setFixedWidth (800);
          
          	// Creating the scroll area
          
              QScrollArea *scroll = new QScrollArea(this);
              scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
              scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
          
              QWidget *viewport  = new QWidget(this);
              scroll->setWidget (viewport);
              scroll->setWidgetResizable (true);
          
              // Adding layout
          
              QGridLayout *grid = new QGridLayout;
              grid->addWidget (scroll);
              Additem::setLayout (grid);
          
          	grid->addWidget (Title,0,0,0,4);
          

          How can I move it to the top of the page (or to the top left corner)?
          Thank you.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @gabor53
          What is age and why are you doing this:

          Additem::setLayout (grid);
          

          that way?

          If I'm understanding you correctly you either want to have a QVBoxLayout and use it like this:

          QVBoxLayout * layout = new QVBoxLayout(widgetHavingTheLayout);
          layout->addWidget(Title);
          layout->addWidget(scroll);
          

          Or maybe something like this with a grid layout:

          QGridLayout * grid = new QGridLayout(widgetHavingTheLayout);
          grid->addWidget(Title, 0, 0);
          grid->addWidget(scroll, 1, 0);
          grid->addItem(new QSpacerItem(1, 1), 2, 1);
          

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            The code gave me the following layout:

            	grid->addWidget (Title,0,0);
                grid->addWidget (scroll,1,0);
                grid->addWidget (test,2,0);
            
            

            Image of layout
            How can I place the "test" text into to scroll area, 0,0 position?
            Thank you.

            kshegunovK 1 Reply Last reply
            0
            • G gabor53

              The code gave me the following layout:

              	grid->addWidget (Title,0,0);
                  grid->addWidget (scroll,1,0);
                  grid->addWidget (test,2,0);
              
              

              Image of layout
              How can I place the "test" text into to scroll area, 0,0 position?
              Thank you.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @gabor53
              Honestly I don't understand what exactly you're trying to achieve, but to put "test" into the scroll area you add it to its widget, not next to the scroll area. Something like this:

              QVBoxLayout * scrollLayout = new QVBoxLayout(viewport);
              scrollLayout->addWidget(test);
              

              Kind regards.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                Hi,
                The problem is, that the original code

                    QScrollArea *scroll = new QScrollArea(this);
                    scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
                    scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
                
                    QWidget *viewport  = new QWidget(this);
                    scroll->setWidget (viewport);
                    scroll->setWidgetResizable (true);
                
                    // Adding layout
                
                    QLabel *test = new QLabel;
                    test->setText ("test");
                
                    QGridLayout *grid = new QGridLayout;
                    grid->addWidget (scroll);
                    Additem::setLayout (grid);
                
                	grid->addWidget (test);
                

                places the test label to the bottom of the Dialog while the scroll area is right above it. I would like to place the test label to the top left corner of the scroll area. I appreciate any ideas.
                Thank you.

                kshegunovK 1 Reply Last reply
                0
                • G gabor53

                  Hi,
                  The problem is, that the original code

                      QScrollArea *scroll = new QScrollArea(this);
                      scroll->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
                      scroll->setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOn);
                  
                      QWidget *viewport  = new QWidget(this);
                      scroll->setWidget (viewport);
                      scroll->setWidgetResizable (true);
                  
                      // Adding layout
                  
                      QLabel *test = new QLabel;
                      test->setText ("test");
                  
                      QGridLayout *grid = new QGridLayout;
                      grid->addWidget (scroll);
                      Additem::setLayout (grid);
                  
                  	grid->addWidget (test);
                  

                  places the test label to the bottom of the Dialog while the scroll area is right above it. I would like to place the test label to the top left corner of the scroll area. I appreciate any ideas.
                  Thank you.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @gabor53
                  Right, and I count three posts proposing possible solutions. Have you checked them out?

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    Yes. I checked them and none of them does what I need. The last one crashed Qt.

                    kshegunovK 1 Reply Last reply
                    0
                    • Ni.SumiN Offline
                      Ni.SumiN Offline
                      Ni.Sumi
                      wrote on last edited by Ni.Sumi
                      #10

                      Hi @gabor53 ,

                      I made solution for you. Download this and try this code. Dropboxhere
                      @kshegunov has already given you the solution.

                      Result image . You want "Test " inside the topleft corner of "Scroll" area. But you have coded it in different way. you have made Gridlayout and added "scroll & test" to it. Then how can you get "Test" inside the "scroll" area. Make gridlayoout inside the Scrollarea and add widgets.

                      1 Reply Last reply
                      2
                      • G gabor53

                        Yes. I checked them and none of them does what I need. The last one crashed Qt.

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #11

                        @gabor53
                        Well, I'm sorry that none of them worked for you. I currently have no more ideas, maybe try what @Ni-Sumi is suggesting?

                        Kind regards.

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • Ni.SumiN Offline
                          Ni.SumiN Offline
                          Ni.Sumi
                          wrote on last edited by Ni.Sumi
                          #12

                          @kshegunov , You have given the 100% correct solution.

                          @gabor53 seems missed layout inside the Scrollarea. Instead of making layout inside the Scrollarea, @gabor53 has made layout outside the scrollarea and expecting that layout to be inside.

                          Caution: My point will valid only If gabor53 wants "Test " label inside the scroll area.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            Thank you all. I got it now and it works nicely.

                            1 Reply Last reply
                            0
                            • Ni.SumiN Offline
                              Ni.SumiN Offline
                              Ni.Sumi
                              wrote on last edited by
                              #14

                              @gabor53

                              Please it as Solved .

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved