Namespaces
Variants
Actions
Revision as of 04:14, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:How to make a customized widget in Qt

Jump to: navigation, search
Archived.png
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.

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

#ifndef CUSTOMWIDGET_H
#define CUSTOMWIDGET_H
 
#include <QWidget>
 
class CustomWidget : public QWidget
{
Q_OBJECT
 
public:
CustomWidget(QWidget *parent = 0);
};
 
#endif // 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);
}

Output Image

Customnormal.jpg

532 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved