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