Archived:How to use QWidget
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Article
Keywords: QWidget
Created: kamaljaiswal
(17 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Introduction
The QWidget class is the base class of all user interface objects.It receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen.Every widget is rectangular, and are sorted in a Z-order.
A Widget is called a window if it is not embedded in a parent widget. Non window widgets are used as a child widgets. Most of the Widgets in Qt are used as a child widgets.
When a widgets is used as a container to group a number of child widgets, it is known as a composite widget.
Various Function
- The size of a QWidget can be set or modified with the method QWidget::resize().
QWidget window; window.resize(100,50);
- Qt provide a system of layout to organize the size and position of children widgets. Here is an example using QVBoxLayout to align two buttons vertically:
QVBoxLayout *layout=new QVBoxLayout();
layout->addWidget(new QPushButton("Hello"));
layout->addWidget(new QPushButton("Bye Bye"));
window.setLayout(layout);
- To make Widget visible we have to use QWidget::show()
window->show();
Code Snippet
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(100,50);
QVBoxLayout *layout=new QVBoxLayout();
layout->addWidget(new QPushButton("Hello"));
layout->addWidget(new QPushButton("Bye Bye"));
window.setLayout(layout);
window.show();
return app.exec();
}


(no comments yet)