Archived:How to make a customized widget in Qt
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.
This (archived) article shows the procedure for customising aQWidget.
Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): Qt
Article
Keywords: Customized Widget
Created: james1980
(30 Dec 2008)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Introduction
Create a new Qt project of type GUI widget following the procedure given Getting started with Qt.
Then replace the three files named main.cpp, customwidget.h and customwidget.cpp with the code given below.
Main.cpp
#include <QApplication>
#include "customwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomWidget w;
w.show();
return a.exec();
}
customwidget.h
customwidget.cpp
#include <QPushButton>
#include <QVBoxLayout>
#include <QCoreApplication>
#include "customwidget.h"
CustomWidget::CustomWidget(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
// This button show the top level widget in a normal size
QPushButton* normalSizeButton = new QPushButton("Normal size");
QObject::connect(normalSizeButton, SIGNAL(clicked()), this, SLOT(showNormal()));
layout->addWidget(normalSizeButton);
// This button maximize the widget size
QPushButton* maximzeButton = new QPushButton("Maximize");
QObject::connect(maximzeButton, SIGNAL(clicked()), this, SLOT(showMaximized()));
layout->addWidget(maximzeButton);
// This button exit the application
QPushButton* exitButton = new QPushButton("Exit");
QObject::connect(exitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
layout->addWidget(exitButton);
}


