COS2614 Summary PDF

Title COS2614 Summary
Author Rudi Coetzee
Course Programming: Contemporary Concepts
Institution University of South Africa
Pages 12
File Size 777.9 KB
File Type PDF
Total Downloads 96
Total Views 273

Summary

UNISA INF2611 QT Exam Revision Notes QInputDialog There are 4 variants of the QInputDialog: getInteger int i QInputDialog::getInt( this, an integer from 0 to 0, 0, 100, 1, pointer to parent container Title Bar Text Text above input box Default Value Minimum Value Maximum Value number to increment sp...


Description

UNISA INF2611 QT Exam Revision Notes QInputDialog There are 4 variants of the QInputDialog:

getInteger int i = QInputDialog::getInt( this, tr("Enter Number"), tr("Enter an integer from 0 to 100:"), 0, 0, 100, 1, &ok);

// // // // // // // //

pointer to parent container Title Bar Text Text above input box Default Value Minimum Value Maximum Value number to increment spin edit with reference to Boolean variable

getDouble double d = QInputDialog::getDouble( this, tr("Enter Double"), tr("Enter a double from 0 to 100:"), 0, 0, 100, 2, &ok);

// // // // // // // //

pointer to parent container Title Bar Text Text above input box Default value Minimum Value Maximum Value Number of decimal places reference to Boolean variable

getItem QString language = QInputDialog::getItem( this, // pointer to parent container tr("Select Language"), // Title Bar Text tr("Please select a language:"), // Text above input box languages, // QStringList containing languages 0, // Default item index false, // Specifies if user may add own input &ok); // rerence to boolean variable

getText QString name = QInputDialog::getText( this, tr("Enter Name"), tr("Please enter your name:"));

// pointer to parent container // Title Bar Text // Text above input box

QMessageBox The messagebox is shown to the user to inform the user of important issues or decisions that has to be made.

int result = QMessageBox::Question( this, tr("Perform Action?"), tr("Do you really want to perform this action?"), QMessageBox::Yes|QMessageBox::Default, QMessageBox::No|QMessageBox::Escape, QMessageBox::NoButton);

// // // // // //

pointer to parent container Title Bar Text Text to display 1st button 2nd button 3rd button

The different examples of a QMessageBox are as follows: QMessageBox::Information

QMessageBox::Question

QMessageBox::Warning

QMessageBox::Critical

When is the QInputDialog used and when is the QMessageDialog used? QInputDialog 1. Used to get information from the user: a. Dates b. Numbers c. Text 2. Usually used in simple queries

QMessageDialog 1. Used to inform the user of a specific event 2. Used to force the user to make a decision based on an event. 3. Usually used in more serious events where the user needs to be made aware of something

QWidgets used in input in a GUI QT has several widget classes that can be used in a GUI application. They are as follows:

QLineEdit QLineEdit allows you to enter a single line of text

QTextEdit QTextEdit allows you to enter multiple lines of text

QSpinBox QSpinbox allows you to enter numerical Values

QTimeEdit QTimeEdit allows you to enter the time

QDateEdit QTDateEdit allows you to enter the date

QComboBox QCombobox allows you to pick an option from a list of items

QPushButton QPushButton provides a command button

QDialogButtonBox QDialogButtonBox allows you to present standard buttons to the user that will always show up in the same order.

QRadioButton QRadioButton allows the user to pick one of several options.

Example of using the Widgets in your application:

Header #ifndef INPUTFORM_H #define INPUTFORM_H #include class class class class

Source #include "inputform.h"

QLineEdit; QDateEdit; QPushButton; QDialogButtonBox;

class InputForm : public QDialog { Q_OBJECT

InputForm::InputForm(QWidget* parent = 0) { // instantiate widgets m_name = new QLineEdit; m_birthday = new QDateEdit; m_birthday->setDisplayFormat("dd/MM/yyyy"); m_cButton = new QPushButton(tr("Choose")); m_cButton->setAutoFillBackground(true); m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

public: explicit InputForm(QWidget* parent = 0); void updateUi();

// instantiate vertical box layout and QFormLayout // for form QVBoxLayout* vbox = new QVBoxLayout; QFormLayout* lay = new QFormLayout;

protected slots: void accept(); void chooseColor();

// add the labels and widgets to the QFormLayout lay->addRow(tr("Name"), m_name); lay->addRow(tr("Birthdate"), m_birthday); lay->addRow(tr("Favorite Color"), m_cButton);

private: QColor m_color; QLineEdit* m_name; QDateEdit* m_birthday; QPushButton* m_cButton; QDialogButtonBox* m_buttons;

// add the QFormLayout to the VBoxLayout and add // the buttons vbox->addLayout(lay); vbox->addWidget(m_buttons);

}; // assert that parents are null Q_ASSERT(vbox->parent() == 0); Q_ASSERT(m_birthday->parent() == 0);

#endif // INPUTFORM_H

// set the parent of vbox to the host InputForm // object setLayout(vbox); // assert that the parents were set Q_ASSERT(vbox->parent() == this); Q_ASSERT(m_birthday->parent() == this); }

Parameters Value Parameter

Reference Parameter

Value parameters are local variables initialized by copies of the corresponding argument objects specified when the function is called. By default, C++ parameters are passed by value. Only the local copy of the argument objects is manipulated inside the function if they are passed by value. A Reference Parameter is simply a parameter that is an alias for something else. To declare a parameter to be a reference, put the ampersand character (&) between the type name and the parameter name. Declaring a reference parameter to be const tells the compiler to make sure that the function does not attempt to change that object.

Keywords static const

Declaring an object as const tells the compiler to make sure that nothing modifies the object.

Objects Object Variable Pointer reference parameter

Q_Object Macro

An object (in the most general sense) is a chunk of memory that can hold data. A variable is an object with a name recognized by the compiler. A pointer points to the object at the stored memory address. A variable preceded by an asterix (*) is a pointer. For example: *y = &x – y is a pointer to an int as per below. The unary (&) operator, also known as the address-of operator, when applied to any object, returns the memory address of that object. For example: &x returns the memory address of x. QObject supports features not normally available in C++ objects:  Signals and Slots  MetaObjects, MetaProperties, MetaMethods  qobject_cast The Meta Object Compiler (moc) generates additional functions for each QObjectderived class that uses the Q_OBJECT macro.

Each QObject can have at most one parent QObject, and an arbitrarily large number of QObject children. Each QObject stores pointers to its children in a QObjectList. The child list establishes a bidirectional association between QObjects:  Each parent object knows the address of each of its child objects.  Each child object knows the address of its parent object. Reparenting is when the pointer of an object is removed from one parent’s child list and added to another object’s child list. For example: objA->setParent(objB); Parent objects should not be confused with base classes. The parent-child relationship is meant to describe containment, or management, of objects at runtime. The base-derived relationship is a static relationship between classes determined at compile time. Example of creating and destroying of objects and its children: Person.h

Bunch.cpp

#ifndef PERSON_H #define PERSON_H

#include #include "person.h"

#include #include

static QTextStream cout(stdout);

class Person : public QObject { public: explicit Person(QString name, QObject* parent = 0); virtual ~Person(); }; #endif

Person.cpp #include "person.h" #include static QTextStream cout(stdout); Person::Person(QString name, QObject* parent) : QObject(parent) { setObjectName(name); cout...


Similar Free PDFs