Confidential © 2008 Teleca AB Creating Custom Widgets Author: Patricia Jiang Date: July 29, 2010 Version: 1.0 Teleca Chengdu.

Slides:



Advertisements
Similar presentations
QT UI Widgets and Layout_2S
Advertisements

Confidential © 2008 Teleca AB MultiThreads of Qt Teleca Chengdu 26 July 2010 Author: Tom Chen.
Sven Johannsen C++ User Group Aachen 01/08/15
QT GUI Programming CS340 – Software Design © 2009 – Jason Leigh University of Illinois at Chicago.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Hands-On Microsoft Windows Server 2003 Administration Chapter 5 Administering File Resources.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Views Dwight Deugo Nesa Matic
ICS 665 Jesse Abdul. jQuery UI Overview  jQuery UI javascript library Includes all UI component functionality  jQuery UI CSS framework Includes standard.
L8: Qt4 Concept Qt Module QObject QObject Model Signal and Slot Qt Event Loop.
Eclipse IDE. 2 IDE Overview An IDE is an Interactive Development Environment Different IDEs meet different needs BlueJ and DrJava are designed as teaching.
Lesley Bross, August 25, 2010 ArcGIS 10 Add-In Components and Controls.
Qt Igor November 8, 2002 Friday’s HENP Group Meeting.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Blanchette and Summerfield, Ch. 2
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
Working Out with KURL! Shayne Koestler Kinetic Data.
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
Bertrand Bellenot ROOT Users Workshop Mar ROOT GUI Builder Status & Plans ROOT & External GUI World MFC, FOX, Qt, PVSS… Snapshot of the Future.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
Visual Studio.NET Part 2 dbg --- Using the VS.NET Integrated Development Environment.
Introduction to Matlab & Data Analysis
Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Visual Studio.NET Part 2 dbg --- Using the Integrated Development Environment.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Using Qt for GUI development Brad Whitlock March 2002.
Installing CompuCell3D from source Maciej Swat Biocomplexity Institute, Indiana University.
Are you getting the benefits from ASP.NET and AJAX? Introduction to the CTC ASP.NET Webforms Generator.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
WaveMaker Visual AJAX Studio 4.0 Training Basics: Building Your First Application Designer Basics.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Click your mouse to continue. The Office Shortcut Bar The Office Shortcut Bar contains program buttons that, when clicked, start new documents or start.
ATN GIS Support ArcGIS: ArcCatalog.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
QT – Dialog C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3.
Creating a Qt Widget Based Application From Qt Creator Manual.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
QT Programming QT Programming Ruku Roychowdhury. Background QT is a cross platform application framework. Widely used to develop GUI applications. Originally.
Page Designer Storyboard J. A. Fitzpatrick December 2004.
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
Lecture 10 Using Interface Builder to create Mac Applications.
Open project in Microsoft Visual Studio → build program in “Release” mode.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
QT – Windows, menus, and such C++ GUI Programming with Qt 4 Qt 4.5 Reference Documentation Blanchette and Summerfield, Ch. 3.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Developing GUIs With the Eclipse Visual Editor, SWT Edition David Gallardo.
Compound Data Types Part13dbg. 2 Point A Point is a simple built-in struct that stores a pair of screen coordinates. Instantiate a Point: Point aPoint.
Object-Oriented Programming: Inheritance and Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Making Billboards By Deborah Nelson Duke University, Under the direction of Professor Susan Rodger, July 14, 2008.
Creating ActiveX Controls at runtime If you need to create an ActiveX Control at runtime without a resource template entry, follow the programming steps.
September 24th 2006, aKademy The Design and Implementation of KJSEmbed Richard Moore,
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Mobile Application Development with MeeGo™ - Programming with SDK
Multi-form applications and dialogs
Java Programming Language
Custom Widgets & Events
Object-Oriented Programming: Inheritance and Polymorphism
Presentation transcript:

Confidential © 2008 Teleca AB Creating Custom Widgets Author: Patricia Jiang Date: July 29, 2010 Version: 1.0 Teleca Chengdu

Confidential © 2008 Teleca AB 2 Agenda 1. Customizing Qt Widgets 2. Subclassing QWidget 3. Integrating Custom Widgets with Qt Designer

Confidential © 2008 Teleca AB 3 1. Customizing Qt Widgets A Qt widget requires more customization than is possible by setting its properties in Qt Designer. A simple and direct solution is to subclass the relevant widget class and adapt it to suit our needs. Let’s have an example --- HexSpinBox You can do like this: Subclass the relevant widget, Implement some virtual functions, Add some special method.

Confidential © 2008 Teleca AB 4 2. Subclassing QWidget Simply a combination of existing widgets, but need to add some new code, for example, new signal and slot. 1 Create a new form using the "Widget" template. 2 Add the necessary widgets to the form, and lay them out. 3 Set up the signals and slots connections. If behavior beyond what can be achieved through signals and slots is required, write the necessary code in a class that is derived from both QWidget and the uic-generated class. If the widget has no signals and slots of its own and doesn't reimplement any virtual functions, it is even possible to simply assemble the widget by combining existing widgets without a subclass. When none of Qt's widgets are suitable for the task at hand. We must write a new one, subclassing QWidget and reimplementing a few event handlers to paint the widget and to respond to mouse clicks. Eg:QLabel, QPushButton, and QTableWidget.

Confidential © 2008 Teleca AB 5 The IconEditer example(1) Let's begin by reviewing the header file. class IconEditor : public QWidget { Q_OBJECT Q_PROPERTY(QColor penColor READ penColor WRITE setPenColor) Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage) Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor) public: IconEditor(QWidget *parent = 0); …… } The IconEditor class uses the Q_PROPERTY() macro to declare three custom properties Properties may be of any type supported by QVariant. The Q_OBJECT macro is necessary for classes that define properties.

Confidential © 2008 Teleca AB The IconEditer example(2) The implementation file begins with the IconEditor's constructor: #include #include "iconeditor.h“ IconEditor::IconEditor(QWidget *parent) : QWidget(parent) { setAttribute(Qt::WA_StaticContents); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); curColor = Qt::black; zoom = 8; image = QImage(16, 16, QImage::Format_ARGB32); image.fill(qRgba(0, 0, 0, 0)); } QSizePolicy::Minimum, widget can be stretched if required, but it should never shrink below the size hint. Qt::WA_StaticContents, This attribute tells Qt that the widget's content doesn't change when the widget is resized. 6

Confidential © 2008 Teleca AB The IconEditer example(3) Void IconEditor::paintEvent(QPaintEvent *event) { QPainter painter(this); …… } There are many situations when a paint event is generated and paintEvent() is called. For example: 1. When a widget is shown for the first time, the system automatically generates a paint event to force the widget to paint itself. 2. When a widget is resized, the system generates a paint event. 3. If the widget is obscured by another window and then revealed again, a paint event is generated for the area that was hidden (unless the window system stored the area). 7

Confidential © 2008 Teleca AB 3.Integrating Custom Widgets with Qt Designer Before we can use custom widgets in Qt Designer, we must make Qt Designer aware of them. There are two techniques for doing this: The promotion approach The plugin approach 8

Confidential © 2008 Teleca AB The promotion approach(1) The promotion approach is the quickest and easiest. But, the properties that are specific to the custom widget aren't accessible in Qt Designer and that the widget isn't rendered as itself. 9

Confidential © 2008 Teleca AB The promotion approach(2) 1. Create a QSpinBox by dragging it from Qt Designer's widget box onto the form. 2. Right-click the spin box and choose Promote to Custom Widget from the context menu. 3. Fill in the dialog that pops up with "HexSpinBox" as the class name and "hexspinbox.h" as the header file. 10

Confidential © 2008 Teleca AB The plugin approach(1) The plugin approach requires the creation of a plugin library that Qt Designer can load at run-time and use to create instances of the widget. We must subclass QDesignerCustomWidgetInterface and reimplement some virtual functions. 11

Confidential © 2008 Teleca AB The plugin approach(2) #include class IconEditorPlugin : public QObject, public QDesignerCustomWidgetInterface { Q_OBJECT Q_INTERFACES(QDesignerCustomWidgetInterface) public: IconEditorPlugin(QObject *parent = 0); QString name() const; QString includeFile() const; QString group() const; QIcon icon() const; QString toolTip() const; QString whatsThis() const; bool isContainer() const; QWidget *createWidget(QWidget *parent); }; 12

Confidential © 2008 Teleca AB The plugin approach(3) Qt Designer calls the createWidget() function to create an instance of a widget class with the given parent. Q_EXPORT_PLUGIN2(iconeditorplugin, IconEditorPlugin) At the end of the source file that implements the plugin class, we must use the Q_EXPORT_PLUGIN2() macro to make the plugin available to Qt Designer. The first argument is the name we want to give the plugin; the second argument is the name of the class that implements it. 13

Confidential © 2008 Teleca AB The plugin approach(4) The.pro file for building the plugin looks like this: TEMPLATE = lib CONFIG += designer plugin release HEADERS =../iconeditor/iconeditor.h \ iconeditorplugin.h SOURCES =../iconeditor/iconeditor.cpp \ iconeditorplugin.cpp RESOURCES = iconeditorplugin.qrc DESTDIR = $$[QT_INSTALL_PLUGINS]/designer The qmake build tool has some predefined variables built into it. One of them is $$[QT_INSTALL_PLUGINS], which holds the path to the plugins directory inside the directory where Qt is installed. When you type make or nmake to build the plugin, it will automatically install itself in Qt's plugins/designer directory. Once the plugin is built, the IconEditor widget can be used in Qt Designer in the same way as any of Qt's built-in widgets. 14

Confidential © 2008 Teleca AB Practice HexSpinBox and iconediter example code in \\cnchfs301\projects$\Qt\Examples 1.Try to create a custom widget via promotion approach 2.Try to create a custom widget via plugin approach

Confidential © 2008 Teleca AB Thank you!