0

I wanted a screen in my application which should look like this:

enter image description here

This means the button in the screen should appear in the centre for screen both horizontally and vertically.

Does anyone how to do it. I have written the following code but its not working.

Container *contentContainer = new Container();
contentContainer->setLayout(StackLayout::create());  
//contentContainer->setVerticalAlignment(VerticalAlignment::Center);
contentContainer->setHorizontalAlignment(HorizontalAlignment::Center);

Button* submitButton =  new Button();
submitButton->setText("Submit");

Button* cancelButton =  new Button();
cancelButton->setText("Cancel");

contentContainer->add(submitButton);
contentContainer->add(cancelButton);

Page * testPage = new Page();
testPage ->setContent(contentContainer);

Sheet *testSheet = new Sheet();
testSheet->setContent( LoginSheetPage );

testSheet->open();
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
Mayank
  • 1,099
  • 4
  • 17
  • 44

2 Answers2

2

To get it center , First you have to create container with docklayout() and inside this container, you have to create stacklayout() with buttons and set docklayout() container as a content of Page.

Container *contentContainer = new Container();
contentContainer->setLayout(StackLayout::create());  
contentContainer->setVerticalAlignment(VerticalAlignment::Center);
contentContainer->setHorizontalAlignment(HorizontalAlignment::Center);

Button* submitButton =  new Button();
submitButton->setText("Submit");

Button* cancelButton =  new Button();
cancelButton->setText("Cancel");

contentContainer->add(submitButton);
contentContainer->add(cancelButton);

Container *rootContainer = new Container();
rootContainer->setLayout(DockLayout::create());  
rootContainer->setVerticalAlignment(VerticalAlignment::Fill);
rootContainer->setHorizontalAlignment(HorizontalAlignment::Fill);
rootContainer->add(contentContainer);

Page * testPage = new Page();
testPage ->setContent(rootContainer);
Mayank
  • 1,099
  • 4
  • 17
  • 44
Ankur
  • 1,385
  • 11
  • 21
0

When you need to specify where your content needs to be docked you should use DockLayout. Then you set the vertical and horizontal alignment properties on it's children depending on where you want them to be docked. In your case:

Container *sheet_container = Container::create().layout(DockLayout::create());
Container *button_container = Container::create()
            .layout(StackLayout::create())
            .horizontal(HorizontalAlignment::Center)
            .vertical(VerticalAlignment::Center);
Button *submit_button = new Button();
submit_button->setText("Submit");
Button *cancel_button = new Button();
cancel_button->setText("Cancel");
button_container->add(submit_button);
button_container->add(cancel_button);
sheet_container->add(button_container);

Page *test_page = new Page();
test_page->setContent(sheet_container);
Sheet *sheet = new Sheet();
sheet->setContent(test_page);
sheet->open();

This answers your question about making the layout but i would suggest you to take a look at UI guidelines and use something like Dialog for this. Also I would suggest to use QML for layout design instead of C++ if you don't have specific requirements that are hard to in QML.

pajaja
  • 2,164
  • 4
  • 25
  • 33