Making GUIs with Qt 5

Besides making clickable programs, learning to program GUIs will give you several other skills with C++:

  • Event-based programming
  • Working with a (very) large library
  • Managing memory in more complicated programs

Getting Started

Building Qt Applications

  • Qt has its own preprocessor, the Meta Object Compiler (moc)
  • qmake manages Qt projects and generates Makefiles automatically
    • qmake -project will generate a project file (ends in .pro) that configures the Makefile
    • qmake generates a Makefile
  • So, to build a Qt project: qmake ; make
    • For this lab, we already have .pro files!
    • We don't have to run qmake -project

Qt Overview

  • There is one, and only one, QApplication
  • qApp is a global pointer to the QApplication

Qt Overview

  • Everything clickable is called a widget
  • Widgets can hold other widgets
  • A widget with no parent becomes a window

A Terrible Notepad

int main(int argc, char** argv)
{
  QApplication app(argc,argv);

  QTextEdit te;
  te.setWindowTitle("Not Vim");
  te.show();

  return app.exec();
}

Laying out your App

Composite Objects

  • Widgets can be added to another widget with the addWidget() function.
  • You can use a layout to specify how the widgets are organized
  • Memory Management note: addWidget() takes a pointer and is responsible for cleaning up all its children.

Layout Example

int main(int argc, char** argv)
{
  QApplication app(argc,argv);

  QTextEdit* te = new QTextEdit;
  QPushButton* quit = new QPushButton("&Quit");

  QVBoxLayout* layout = new QVBoxLayout;
  layout->addWidget(quit);
  layout->addWidget(te);

  QWidget window;
  window.setLayout(layout);

  window.show();

  return app.exec();
}

Signals and Slots

Making Buttons Do Things

  • Qt is event-driven: QApplication monitors what the user does and sends events to widgets when something happens
  • Signal: An event a widget causes: button click, key press, etc.
  • Slot: An action a widget takes when a signal is sent
  • connect() connects signals to slots

Connecting Signals to Slots

  • The connect() function takes four parameters:
    • The object that is sending the signal (Source)
    • The signal sent by that object
    • The object that is receiving the signal (Destination)
    • The slot that is receiving the signal

Actually Quitting

int main(int argc, char** argv)
{
  QApplication app(argc,argv);

  QTextEdit* te = new QTextEdit;
  QPushButton* quit = new QPushButton("&Quit");

  QObject::connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

  QVBoxLayout* layout = new QVBoxLayout;
  layout->addWidget(quit);
  layout->addWidget(te);

  QWidget window;
  window.setLayout(layout);

  window.show();

  return app.exec();
}

Writing your own slot

  • In order to make your own slots, you need to make a custom QWidget class
  • In addition to public and private functions and members, QObjects have public and private slots
  • A slot is just a function that gets called whenever a signal connected to it is sent

Example: ask-quit

Getting Fancy with Signals

Creating your own Signal

  • You can declare your own signals in the signals: class section
    • Just like public or private are class sections
  • For example, QPushButton has void clicked() in its signals

Sending your Signal

  • To send a signal, use emit signal_name()

More about Signals

  • You don’t actually implement signals, just declare, emit, and connect them
  • You can send data over signals by adding parameters to your signals!
  • Connect that signal to a slot that takes the same arguments
  • The slot will be called with the data you use when you emit the signal

Example: title