How to switch between horizontal and vertical layout in Qt
Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): Qt
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();
}




This article is the basic article of QT.This article shows the use of QVBoxLayout and QHBoxLayout for accordingly vertical layout and Horizontal layout.
But hear difference in this article is it shows changing of layout.so There will be use of QPushButton for the changing of layout.There is signal and slots given to the article But considering point is that for changing the layout they used Hide SLOT.
This article may be used in different applications.This article is well-explained and may be useful for beginner For changing the layout.
--nayan_trivedi 12:31, 14 September 2009 (UTC)
This article explains how you can change the layout of the widget through code. In the Code Snippet, Mainly used APIs are QVBoxLayout, and QHBoxLayout. Buttons are added into vertical and horizontal layouts. Buttons are created through QPushButton API.
Connect method of the QObject is used to set click event of that buttons.This article is well explained with snapshots and code snippet. Here in this code snippet if user press on Horizontal button then objects are arranged into horizontal layout of widget. And vise versa.
This article also helps to beginners because the code snippet is having some basic APIs like QPushButton, QWidget, QVBoxLayout, QHBoxLayout etc..
--vkmunjpara 21:24, 26 September 2009 (UTC)