First item in gridlayout is in the middle of the Dialog



  • 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.





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

    still puts the title in the middle.

  • Qt Champions 2016

    @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.



  • 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.


  • Qt Champions 2016

    @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.



  • 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.


  • Qt Champions 2016

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



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



  • 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.


  • Qt Champions 2016

    @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.



  • @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.



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



  • @gabor53

    Please it as Solved .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.