35. When a parent object is deleted, it deletes its children QObject *parent = new QObject(); QObject *child1 = new QObject(parent); QObject *child2 = new QObject(parent); QObject *child1_1 = new QObject(child1); QObject *child1_2 = new QObject(child1); delete parent;
55. It can be called as an ordinary method public slots: void aPublicSlot(); protected slots: void aProtectedSlot(); private slots: void aPrivateSlot(); connect(src, SIGNAL(sig()), dest, SLOT(slt()));
61. A signal is emitted using the emit keyword signals: void aSignal(); emit aSignal();
62. Making the connection QObject::connect( src, SIGNAL( signature ), dest, SLOT( signature ) ); <function name> ( <arg type>... ) clicked() toggled(bool) setText(QString) textChanged(QString) rangeChanged(int,int) setTitle(QString text) setValue(42) A signature consists of the function name and argument types. No variable names, nor values are allowed. Custom types reduces reusability. setItem(ItemClass)
94. checkable / checked – if the action is checkable and the current check status
95. toolTip / statusTip – tips text for tool tips (hover and wait) and status bar tips (hover, no wait)
96. Why QAction QAction *action = new QAction( parent ); action->setText("text"); action->setIcon(QIcon(":/icons/icon.png")); action->setShortcut(QKeySequence("Ctrl+G")); action->setData(myDataQVariant); Setting properties for text, icon and keyboard short-cut Creating a new action A QVariant can be associated with each action, to carry data associated with the given operation
119. Implementing the new slot is easy – just create a new MainWindow and show it void MainWindow::on_actionNew_triggered() { MainWindow *w = new MainWindow(); w->show(); }
120. Closing and Exiting MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(close())); connect(ui->actionExit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); } Setting the WA_DeleteOnClose attribute makes the widget delete itself when it is closed All widgets has a close slot out-of-the-box The QApplication class has a slot for closing all windows The qApp pointer refers to the current QApplication instance
140. about dialogs for both the current application and Qt itself Roughly the same type of dialogs with a title, a message and one or more buttons. The green words are function names for static members. To show an information dialog, use QMessageBox::information , etc.
141. QMessageBox QMessageBox::warning(this, "Document Modified", "The document has been modified, do you want to close it?" "You will lose all your changes.", QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes Parent window Dialog title The message of the dialog. Notice the use of ' ' The buttons to use. Pick from Ok , Open , Save , Cancel , Close , Discard , Apply , Reset , Restore defaults , Help , Save all , Yes , Yes to all , No , No to all , Abort , Retry and Ignore The default button The return value is the button used for closing the dialog
142.
143. By setting a window title with the sub-string “ [*] ”, the modified indication can be activated using setWindowModified . void MainWindow::updateWindowTitle() { setWindowTitle(tr("%1 [*] ").arg(qApp->applicationName())); setWindowModified(m_modified); } This function is called from the constructor as well as from the documentModified slot
144.
145. The QApplication object keeps track of the application's name, version, producer, etc.
146. This is used for window titles, etcetera a.setApplicationName("Text Editor"); a.setApplicationVersion("0.1"); a.setOrganizationName("ExampleSoft"); a.setOrganizationDomain("example.com"); a.setWindowIcon(QIcon(":/icons/new.png")); Application icon Producer name and domain Application name and version
152. A window cannot be closed without accepting the loss of document modifications
153.
154. We add an action, add a View menu and put the action in the menu Text Name Icon Short-cut Tool-tip Select Font... actionSelectFont Select the display font New menus are always placed to the right, but they can be dragged in to place afterwards
155.
156.
157. The user expects settings to stick, i.e. use the last value when opening new windows
158.
159. By using a QSettings object, you get a cross platform interface for handing settings
160. Any QVariant can be stored – but think about readability for advanced users QSettings settings; myString = settings.value("key","default").toString(); settings.setValue("key", myString); settings.remove("key");
161.
162. Restoring the font void MainWindow::on_actionSelectFont_triggered() { bool ok; QFont font = QFontDialog::getFont(&ok, ui->textEdit->font(), this); if(ok) { QSettings settings; settings.setValue("viewFont", font); ui->textEdit->setFont(font); } } MainWindow::MainWindow(QWidget *parent) : ... { ... QSettings settings; ui->textEdit->setFont( settings.value("viewFont", QApplication::font()).value<QFont>()); ... Default value
173. The actions are added to both the tool bar and menu Right click on the tool bar to add separators Text Name Icon Short-cut Tool-tip Cut actionCut Ctrl+X Cut Copy actionCopy Ctrl+C Copy Paste actionPaste Ctrl+V Paste
174.
175. To avoid trying to copy in vain, let's add connections for enabling and disabling the actions depending on the selection connect(ui->actionCut, SIGNAL(triggered()), ui->textEdit, SLOT(cut())); connect(ui->actionCopy, SIGNAL(triggered()), ui->textEdit, SLOT(copy())); connect(ui->actionPaste, SIGNAL(triggered()), ui->textEdit, SLOT(paste())); connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCut, SLOT(setEnabled(bool))); connect(ui->textEdit, SIGNAL(copyAvailable(bool)), ui->actionCopy, SLOT(setEnabled(bool)));
193. Saving QFile file(m_fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) qFatal("Error opening file for writing"); QTextStream out(&file); out << textString; QFile file(m_fileName); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) qFatal("Error opening file for reading"); QTextStream in(&file); in >> textString;
194.
195. As a start, it is set in the constructor MainWindow::MainWindow( const QString &fileName , QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_modified(false), m_fileName(fileName) { ... if(!m_fileName.isNull()) loadFile(); updateWindowTitle(); } The name can be QString() , i.e. null, as new, unnamed documents needs this
196.
197.
198. testing.txt - Text Editor void MainWindow::updateWindowTitle() { setWindowTitle(tr("%1[*] - %2") .arg(m_fileName.isNull()?"untitled":QFileInfo(m_fileName).fileName()) .arg(QApplication::applicationName())); setWindowModified(m_modified); } Extracts the file name part of a file name with a path
210. If it fails, it sets the current filename to null void MainWindow::loadFile() { QFile file(m_fileName); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, QApplication::applicationName(), tr("Could not open file %1.%2") .arg(QFileInfo(m_fileName).fileName()) .arg(file.errorString())); m_fileName = QString(); } else { QTextStream in(&file); ui->textEdit->setText(in.readAll()); } m_modified = false; updateWindowTitle(); }
211.
212.
213. The actions are added to the File menu and tool bar Text Name Icon Short-cut Tool-tip Open... actionOpen Ctrl+O Open a document Save actionSave Ctrl+S Save the current document Save As... actionSaveAs Ctrl+Shift+S Save current document as
222. Saving documents bool MainWindow::saveFile() { if(m_fileName.isNull()) return saveFileAs(); QFile file(m_fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(...); m_fileName = QString(); updateWindowTitle(); return false; } QTextStream out(&file); out << ui->textEdit->toPlainText(); m_modified = false; updateWindowTitle(); return true; } If the name is null, we need to save as Save the document If the file cannot be opened for writing, we display a warning and set the file name to null Update the modified flag and title Return true on success
223. Saving documents bool MainWindow::saveFileAs() { QString fileName = QFileDialog::getSaveFileName(...); if(!fileName.isNull()) { m_fileName = fileName; return saveFile(); } return false; } Ask the user for a file name If a name is given, attempt to save If no name is given, the save is a failure The method saveFileAs never calls saveFile unlese !fileName.isNull() thus there is no risk for infinite recursion
224.
225.
226. Saving documents void MainWindow::closeEvent(QCloseEvent *e) { if(m_modified) { switch(QMessageBox::warning(this, "Document Modified", "The document has ...", QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel)) { case QMessageBox::Yes: if(saveFile()) e->accept(); else e->ignore(); break; case QMessageBox::No: e->accept(); break; case QMessageBox::Cancel: e->ignore(); break; } } else { e->accept(); } } The user wants to save The user do not want to save Cancel the closing Close unmodified documents Depending on the save, close or ignore
Qt, is a cross platform development framework written in C++. This does not limit the languages used. Bindings are available for Python, Ruby, C# (mono), Ada, Pascal, Perl, PHP (see: http://qt.nokia.com/products/programming-language-support ) Most people know Qt for its cross platform user interface abilities. Cross platform development is about so much more. For instance, just compare file paths between Windows and Unix. Qt provides classes for almost all conceivable tasks.
Qt supports a multitude of functions in a cross platform manner. This means that Qt is a large package. Qt is divided into modules, and when building and deploying, you can choose which module to use. This helps reducing the number of bytes needed to deploy. Also, there are a few platform specific modules (e.g. QtDBUS for inter process communication – unix only, QtAxContainer and QtAxServer for building and using ActiveX components – Windows only)
Qt extends C++ while sticking to pure C++. Examples of what you get (there is much more): “ foreach” loops Meta-information, great for casting, working with dynamic trees of classes, etc. Using meta-information, dynamic connections such as the connect example is possible.
Qt is available for all major desktop platforms. Windows XP/Vista/7 are officially supported OS X, latest version of Qt supports at least down to 10.3 (10.4 or later is required for development) Linux/Unix with X11, i.e. not tied to Linux. Official support for Linux, AIX, HPUX, Solaris. Community support for FreeBSD, OpenBSD, NetBSD, etc. Notice that the X11 support is not focused to deploying KDE on all desktops. Instead, Qt aims to integrate as a native part of all desktops, including Gnome.
Qt is also available for a number of embedded platforms. Windows CE, versions 5 and 6. Symbian S60, well tested with 3.1, 3.2 and 5.0. Maemo, so you can use it on your N900 tablets Embedded Linux, using the framebuffer directly, i.e. no X11 and a smaller footprint. Can accelerate on some platforms. A nice example is the beagleboard. Tier 3 platforms: QNX, WxWorks. Supported by partner companies.
Qt is also available for a number of embedded platforms. Windows CE, versions 5 and 6. Symbian S60, well tested with 3.1, 3.2 and 5.0. Maemo, so you can use it on your N900 tablets Embedded Linux, using the framebuffer directly, i.e. no X11 and a smaller footprint. Can accelerate on some platforms. A nice example is the beagleboard. Tier 3 platforms: QNX, WxWorks. Supported by partner companies.
Walkthrough The target of the project This will be the starting point of the exercises for this lecture.
Walkthrough The entire source, focus on: - simplicity - small code
Walkthrough Focus on includes. All Qt classes are included by name, compare with iostream, etc. No “.h” ending, capitalization.
Walkthrough One QApplication object, drives the application, manages global settings. There must always be a QApplication object. You can always access the QApplication object through the qApp pointer. You can look at this as a singleton, but the instantiation must be made explicitly from the main function.
Walkthrough QLabel, is a widget. The text is passed to the constructor before the widget is shown. Elaborate, everything is built from widgets. Widgets can be labels (as here), buttons, sliders, group boxes, windows, etc. As the label does not have a parent widget, i.e. it is not contained by another widget, it is a top-level widget. This means that it will result in a new window that is decorated by the native window manager.
Walkthrough Calling exec start the event loop. This gets everything running. The event loop ends when last window closes (can be turned off). Having started the event loop, you must change mind-set. Everything from here on is event driven, be it user interaction (keys or mouse), network or timer.
So, why cannot QChar be a QObject. QObjects are individuals! This means that you cannot write obj1 = obj2, copy is not a valid operation. Why? QObjects have names, for instance addButton, deleteButton, etc (from the first lecture's demo). How do you copy this? You don't want two addButtons? QObjects are structured in hierarchies. How do you copy that context? Do you want them at the same level? Leaf level? Root level? QObjects can be interconnected (add calls a slot) How do you copy this? Do you want to duplicate connections?
QObjects carry meta-data, that is data about the object itself. This makes it possible to add introspection to Qt, for instance to ask a class which methods it has Every QObject has a meta object which can be retreived using the metaObject method. The meta object knows about the class, its name, base class, properties, methods, slots and signals. Continues
QObjects can be used in a way that takes care of all the dirty parts of memory management. It becomes almost as easy as working with a garbage collector, but you are still in full control. The trick is that all QObjects have a parent, or an owner. When the parent is deleted, it deletes all its children. This reduces the number of objects that you need to keep track of to one, in the ideal case. Continues
The very same parent-child relationship is used to represent visual hierarchies. Refer to the tree structure, the box contains the radio buttons (option1/2). The parent contains the box and the button. Compare to the previous slide. Continues
So, how does this make memory management easy. I still need to keep track of an object and make sure that I delete it? No, not if you use the stack cleverly. First of all, the example from the previous slide would probably have been implemented in the parent's constructor, i.e. this is the top-level parent. Second, when using the dialog, you allocate it on the stack. This means that the dialog, along with all its children, will be deleted when the scope ends. Continues
These slides intend to jog the students' memory, not explain the stack vs heap decision in full. The heap is used when you allocate memory dynamically . In C++, that means new/delete. In C you have used malloc and free. Heap memory must be explicitly freed, i.e. you must call delete on everything that you allocate. If you do not do so, you will leak memory. This will, eventually, lead to memory shortage and a crash. Dynamically allocated objects live until you delete them, so you have full control of when something is constructed or destructed. Continues
The stack is used for automatic memory allocations (as opposed to dynamic memory). The stack grows and shrinks when you make function calls. It is used for local variables, function arguments and return addresses. Objects allocated on the stack are destructed when they go out of scope. The scope ends with a }, or return, or for single-line scopes, at the end of the line. Continues
To get almost automatic memory management using Qt, the trick is to allocate the outermost parents on the stack, and the rest on the heap. For instance, the main function scope will be valid for as long as the application is running, so we allocate the application and window on the stack. The window, in turn, creates a bunch of child widgets in its constructor. To avoid destruction when the scope of the constructor ends, they are allocated dynamically. But, they are given the window as their parent object and are thus also destructed when the main function scope ends.
One of the key factors of Qt is the signals and slots mechanism. It is a mechanism to dynamically and loosely tie together events and changes with reactions Dynamically = at run-time Loosely = sender and receiver do not know each other events = timer event, clicks, etc. Not to be confused with actual events (QEvent). state changes = size changed, text changed, value changed, etc reactions = source code that actually does something This is what makes a Qt application tick Continues
Looking at an example from the first lecture. The three buttons all emit the clicked signal when they are clicked by the user (or activated by other means). They do this regardless whether something is connected to them or not, and regardless of what is connected to them. Continues
We choose to connect the buttons to the list widget's clear slot, and two custom slots in our custom code. As said earlier, the buttons do not care what they are connected to, and the slots are equally independent of what triggers them. You can even call them programatically as ordinary functions. Continues
So, the add button emits clicked, ending up in the on_addButton_clicked slot resulting in the following code being run. It simply asks for input and then adds it to the list. The delete button emits clicked, ending up in the on_deleteButton_clicked slot, where all selected items are deleted. The clear button emits clicked, which ends up in the clear slot of the QListWidget, which to us is a black box that does what its documentation says.
A slot is an ordinary function, just that it can be connected to signals. They do not have to be connected, you can call a slot like any other function, and you implement it as usual. Slots are declared in one of the sections public, protected and private slots. These access restrictions work as intended when calling the function, but a private or protected slot can be connected to any other signal, so they can be triggered from outside the class. Slots can return values, but connections cannot carry return arguments. Any number of signals can be connected to a single slot. This means that a single slot can serve several sources of events – think keyboard shortcut, button, etc. Continues
Signals are defined in the signals section. This section can be considered protected, as a signal can only be emitted from within a class or its decendants. Signals always return void, and must not be implemented. Instead, moc provides function bodies that trigger the actual slot-activation-code. A signal can be connected to any number of slots, so a single event can trigger multiple reactions. It is fully possible to connect signals and slots across threads. Third party libraries such as Qxt ( http://doc.libqxt.org/0.5.0/classQxtRPCPeer.html ). Inside a signal emitting class, you use the emit keyword to emit a signal. The emit keyword is defined as nothing, what actually takes place is a call to the signal function which calls the slots. Continues
You can make signals to slots connections between any two QObjects. Qt verifies that the signatures of the signal and slot match. The signature consists of the name of the signal or slot followed by the argument types . There must be no values nor variable names in the signature. It is also recommended to stick to using standard types, e.g. the ItemClass custom type reduces the reusability and should thus be avoided. Continues
When matching signatures, Qt is very forgiving. The basic rule is that Qt cannot create or convert values, but apart from that anything is allowed (i.e. skipping arguments). The examples on the slide demonstrate this. The errors are (from the top): missing the last int (cannot create) QString does not match int (cannot convert) missing the only int (cannot create) Continues
When making connections from Designer to your own source, Qt uses the automatic connection mechanism. It lets signals automatically connect to slots with the corresponding name (structure and examples on the slide). The automatic connections are made when connectSlotsByName is called. That is done at the end of the setupUi function generated by Designer. When using this mechanism, think about reusability. Sometimes handwriting a couple of connect statements can greatly improve readability of the code. Continues
Read clockwise The application stops executing with the last window has been closed...
Mention that OS X removes the [*], instead a dot in the left-most ball is used to indicate if the document is modified or not.
windowtitles should be interpreted as automatic window titles...