KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?

Development/Tutorials/Qt4 Ruby Tutorial/Chapter 2

< Development | Tutorials | Qt4 Ruby Tutorial

Contents

  • 1 Calling it Quits
    • 1.1 Overview
    • 1.2 Line by Line Walkthrough
    • 1.3 Running the Application
    • 1.4 Exercises

[edit] Calling it Quits

Files:

  • t2.rb

[edit] Overview

Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.

We will also use a font that is more exciting than the default one.

require 'Qt4'
 
app = Qt::Application.new(ARGV)
 
quit = Qt::PushButton.new('Quit')
quit.resize(75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
 
Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))
 
quit.show()
app.exec()

[edit] Line by Line Walkthrough

quit = Qt::PushButton.new('Quit')

This time, the button says Quit and that's exactly what the program will do when the user clicks the button.

quit.resize(75, 30)

We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used Qt::FontMetrics to set right size, or let Qt::PushButton choose a reasonable default.

quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using Qt::Application::setFont().

Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))

Qt::Object::connect() is perhaps the most central feature of Qt. Note that connect() in this context is a static function in Qt::Object. Do not confuse it with the connect() function in the Berkeley socket library.

This connect() call establishes a one-way connection between two Qt objects (objects that inherit Qt::Object, directly or indirectly). Every Qt object can have both signals (to send messages) and slots (to receive messages). All widgets are Qt objects, since they inherit Qt::Widget, which in turn inherits Qt::Object.

Here, the clicked() signal of quit is connected to the quit() slot of app, so that when the button is clicked, the application quits.

The Signals and Slots documentation describes this topic in detail.

[edit] Running the Application

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

[edit] Exercises

Try to resize the window. Click the button to close the application.

Are there any other signals in Qt::PushButton you can connect to quit? [Hint: The Qt::PushButton inherits most of its functionality from Qt::AbstractButton.]

Retrieved from "http://techbase.kde.org/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_2"

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • Log in / create account
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. Qt® and Trolltech® are registered trademarks of Trolltech ASA. Linux® is a registered Trademark of Linus Torvalds. | Legal