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 switch between horizontal and vertical layout in Qt

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

Contents

Introduction

This example is useful if you want to switch the layout of your widgets i.e. from horizontal to vertical and vice versa.

Here two pushbuttons are displayed in the vertical layout first. When user clicks on the horizontal button, widgets are arranged horizontally.

Code

#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
QWidget* win = new QWidget();
QWidget* hwin = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(win);
QHBoxLayout* hlayout = new QHBoxLayout(hwin);
QPushButton* but1 = new QPushButton("Horizontal");
but1->resize(70,20);
layout->addWidget(but1);
QPushButton* but2 = new QPushButton("Vertical");
but2->resize(70,20);
layout->addWidget(but2);
QPushButton* but3 = new QPushButton("Horizontal");
but3->resize(70,20);
hlayout->addWidget(but3);
QPushButton* but4 = new QPushButton("Vertical");
but4->resize(70,20);
hlayout->addWidget(but4);
QObject::connect(but1, SIGNAL(clicked()), hwin, SLOT(show()));
QObject::connect(but1, SIGNAL(clicked()), win, SLOT(hide()));
QObject::connect(but2, SIGNAL(clicked()), win, SLOT(show()));
QObject::connect(but2, SIGNAL(clicked()), hwin, SLOT(hide()));
QObject::connect(but3, SIGNAL(clicked()), hwin, SLOT(show()));
QObject::connect(but3, SIGNAL(clicked()), win, SLOT(hide()));
QObject::connect(but4, SIGNAL(clicked()), win, SLOT(show()));
QObject::connect(but4, SIGNAL(clicked()), hwin, SLOT(hide()));
 
win->show();
 
return app.exec();
}

Screen Shot with Vertical Layout

Vertical1.jpg

Screen Shot with Horizontal Layout

Horizontal1.jpg

This page was last modified on 11 October 2012, at 01:17.
89 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.

×