Docsity

Visual Programming - Advanced Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Visual Programming - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Introduction to Visual Programming

Event-driven Programming

  • Event-driven programming is the standard approach to

creating graphical user interfaces (GUIs)

  • An event-driven program is object-oriented
    • Object-oriented programming was originally developed to implement graphical objects within GUI operating systems
    • Although object-oriented, flow-of-control is sufficiently different that we are going to classify it as a different paradigm that extends OOP.
  • However, top-level control is expressed differently
    • The user is the top-level loop
      • Think of Word, or a game program
    • Every action in your program is a reaction to the user
      • Decompose program in terms of “what will I do if the user does…”
      • User inaction may trigger background actions (e.g. games)

Method #1 Polling

Interaction is governed by a simple loop:

Loop forever: { read input respond to input }

What limitations does this have?

Does it have any advantages?

Method #2 Interrupt-driven Processing

  1. Enable device, then
  2. proceed with “background” processing until an

interrupt is received, at which point

  1. Save state (context-switch)
  2. Respond to input
  3. Restore state and go to step #2.

What advantages/disadvantages does this have?

Events / Messages / Signals

• Any event-driven graphics package has devices

that can signal events

  • In old standards, this was limited to hardware

devices

  • In newer packages (e.g. Qt), any widget can signal

events; the (hardware) mouse is the same as a

(software) slider or button.

• Generally, the event tells you

  • Which device/widget signaled the event
  • Some “measure” giving the new state
    • E.g., whether a mouse button was depressed or released Warning: old systems tend to use the term “events” while newer systems may call them messages or signals

Event handlers

• Event handlers are functions invoked in

response to an event.

• Predominant approach is through the use of

“call-back” functions.

  • Functions are registered with the event
  • Signature of function most conform with language standard for the event

Call-back function: Motif

quit = XtVaCreateManagedWidget(……);

tAddCallback(

quit,

XmNactivateCallback,

QuitCallback,

NULL);

void QuitCallback(

Widget w,

XtPointer,

clientData,

XtPointer callData);

Call-back function: Java

btnJava.addActionListener(this); btnWishJava.addActionListener(this); btnNoJava.addActionListener(this);

public void actionPerformed(ActionEvent e) { Object source = e.getSource(); lblInstructionLabel.setText("Got it!"); if (btnJava.equals(source)) lblFeedback.setText("Yes"); else if (btnWishJava.equals(source)) lblFeedback.setText("No"); else if (btnNoJava.equals(source)) lblFeedback.setText("Maybe so"); }//end action performed

Signals and Slots

  • All that is required to connect signals and slots is that the signatures match.
  • Developers may use predefined or user-defined member functions as signals and slots.

clicked_method()

Making Connections in Qt

#include <qapplication.h> #include <qpushbutton.h> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton *button = new QPushButton("Quit", 0); QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit())); app.setMainWidget(button); button->show(); return app.exec(); }

  • Note that the connect function is a static member function of QOBJECT. How do you know? quit.exe (requires qt-mtnc321.dll)

1. 2. #include <qapplication.h>#include <qhbox.h>Making Connections with Qt

    1. #include <qslider.h>#include <qspinbox.h>
    1. int main(int argc, char *argv[]){
  1. QApplication app(argc, argv);
    1. QHBox *hbox = new QHBox(0);hbox->setCaption("Enter Your Age");
    1. hbox->setMargin(6);hbox->setSpacing(6);
    1. QSpinBox *spinBox = new QSpinBox(hbox);QSlider *slider = new QSlider(Qt::Horizontal, hbox);
    1. spinBox->setRange(0, 130);slider->setRange(0, 130);
    1. QObject::connect(spinBox, SIGNAL(valueChanged(int)),slider, SLOT(setValue(int)));
    1. QObject::connect(slider, SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int)));
  2. spinBox->setValue(35);
    1. app.setMainWidget(hbox);hbox->show();
    1. (^) } return app.exec();
    • Lines 1 – 4: You must include header files for each Qt object referenced in this file. • Line 5 - 7: Declaring argc and argv and passing it through to the QApplication constructor is standard procedure. This allows all Qt programs to specificy the presentation style onthe command line. Examples are age – style=windows, age
    • • Lines 8style=motif, and age – 11: Declare a – style=cde. Layout Manager. QHBox automatically arranged controls horizontally from left to right. Layout managers are used to alleviate the need forprogrammers to calculate actual coordinates for each control in the window. setCaption set the Title Bar. setMargincontrols spacing around and in between child widgets.
    • Lines 12 – 13: Create a spin box and slider with QHBox as the parent. • Lines 14 – 15: Set the valid range for the spin box and the slider. • Lines 16 – 19: Ensure that the spin box and slider are sychronized. Changing one always changes the other.
    • that this causes the spin box to emit a valueChanged(int)Line 20 – Sets the initial value of the spin box to 35. Note signal, which is connected to slider’s setValue(int) slot. The slider will emit a valueChanged(int) signal, which is connected to the spin box’s setValue(int) slot. Since the value is already 35, the spin box does not emit anothervalueChanged(int) signal and avoids and infinite recursion loop.Lines 21 – 23: Sets the main widget to the hbox, makes the age.exe hbox visible, and starts the window’s main event loop.

(requires qt-mtnc321.dll) Docsity.com

Widget Styles

  • Qt supports dynamic widget styles because of it’s GUI emulation approach.
  • age – style=windows
  • age – style=motif
  • age – style=cde
  • age – style=platinum
  • The Windows XP and Mac styles are platform specific because they rely on the platform’s theme engine.
  • Enabled via argc and argv.