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

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.9k Views 2 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.
  • G gabor53
    11 Feb 2016, 13:39

    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.

    K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 11 Feb 2016, 15:08 last edited by kshegunov 2 Nov 2016, 15:08
    #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 11 Feb 2016, 16:03 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.

      K 1 Reply Last reply 11 Feb 2016, 16:26
      0
      • G gabor53
        11 Feb 2016, 16:03

        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.

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 11 Feb 2016, 16:26 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 12 Feb 2016, 04:45 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.

          K 1 Reply Last reply 12 Feb 2016, 05:50
          0
          • G gabor53
            12 Feb 2016, 04:45

            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.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 12 Feb 2016, 05:50 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 12 Feb 2016, 12:48 last edited by
              #9

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

              K 1 Reply Last reply 12 Feb 2016, 15:09
              0
              • N Offline
                N Offline
                Ni.Sumi
                wrote on 12 Feb 2016, 13:12 last edited by Ni.Sumi 2 Dec 2016, 14:17
                #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
                  12 Feb 2016, 12:48

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

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 12 Feb 2016, 15:09 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
                  • N Offline
                    N Offline
                    Ni.Sumi
                    wrote on 12 Feb 2016, 15:14 last edited by Ni.Sumi 2 Dec 2016, 15:17
                    #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 12 Feb 2016, 19:55 last edited by
                      #13

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

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        Ni.Sumi
                        wrote on 12 Feb 2016, 21:04 last edited by
                        #14

                        @gabor53

                        Please it as Solved .

                        1 Reply Last reply
                        1

                        13/14

                        12 Feb 2016, 19:55

                        • Login

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