Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to use QVBoxLayout and QHBoxLayout in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QVBoxLayout,QHBoxLayout
Created: james1980 (27 Dec 2008)
Last edited: hamishwillee (11 Oct 2012)

Contents

Introduction

Qt supports several basic layouts in which you can arrange your widgets for display on the screen:

  1. Vertical layout
  2. Horizontal layout
  3. Grid layout
  4. Form layout

This article shows how to use two of the most useful layouts: QVBoxLayout and QVBoxLayout.

How to use QVBoxLayout

The QVBoxLayout class is used for vertical layouts.

Source code

#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* win = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(win);
QPushButton* but1 = new QPushButton("Horizontal");
but1->resize(70,20);
layout->addWidget(but1);
QPushButton* but2 = new QPushButton("Vertical");
but2->resize(70,20);
layout->addWidget(but2);
 
win->show();
 
return app.exec();
}

Screen Shot

Vertical.jpg


How to use QHBoxLayout

The class QHBoxLayout is used to arrange elements horizontally.

Source code

#include <QApplication>
#include <QPushButton>
#include <QHBoxLayout>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* win = new QWidget();
QHBoxLayout* layout = new QHBoxLayout(win);
QPushButton* but1 = new QPushButton("Horizontal");
but1->resize(70,20);
layout->addWidget(but1);
QPushButton* but2 = new QPushButton("Vertical");
but2->resize(70,20);
layout->addWidget(but2);
win->show();
 
return app.exec();
}

Screen Shot

Horizontal.jpg

This page was last modified on 11 October 2012, at 01:17.
269 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×