Qt5 Tutorial Layouts without Designer - 2020
In this tutorial, we will learn Layouts of Qt. We will add layouts to a form and add widgets to the layout programmatically instead of using Designer as was done in the Layouts.
We will create an empty project and will create 6 PushButtons. Then, we will arrange 3 buttons with horizontal layout, and the other three with vertical layout. The two layouts will be added to the the outer most layout, and then we pass the outer layout to our widget using setLayout().
File->Other Project->Empty Qt Project
There is none in the Project explorer, and even the .pro has nothing in it. So, we need to create a file main.cpp by Add New...
#include <QApplication> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> int main(int argc, char* argv[]) { QApplication app(argc, argv); // Horizontal layout with 3 buttons QHBoxLayout *hLayout = new QHBoxLayout; QPushButton *b1 = new QPushButton("A"); QPushButton *b2 = new QPushButton("B"); QPushButton *b3 = new QPushButton("C"); hLayout->addWidget(b1); hLayout->addWidget(b2); hLayout->addWidget(b3); // Vertical layout with 3 buttons QVBoxLayout *vLayout = new QVBoxLayout; QPushButton *b4 = new QPushButton("D"); QPushButton *b5 = new QPushButton("E"); QPushButton *b6 = new QPushButton("F"); vLayout->addWidget(b4); vLayout->addWidget(b5); vLayout->addWidget(b6); // Outer Layer QVBoxLayout *mainLayout = new QVBoxLayout; // Add the previous two inner layouts mainLayout->addLayout(hLayout); mainLayout->addLayout(vLayout); // Create a widget QWidget *w = new QWidget(); // Set the outer layout as a main layout // of the widget w->setLayout(mainLayout); // Window title w->setWindowTitle("layouts"); // Display w->show(); // Event loop return app.exec(); }
We also need to edit the .pro file:
QT += core gui QT += widgets SOURCES += \ main.cpp
Note that we could have taken out the line:
QT += core gui
and instead, we could have added include statements in the main.cpp:
#include <QtCore> #include <QtGui>
Run the code:
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization