How to Use QPushButton

From Qt Wiki
Revision as of 15:50, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English Български Spanish 简体中文 Ελληνικά
Русскийفارسی

How to Use QPushButton

QPushButton Overview

Using QPushButton [qt.io] developers can create and handle buttons. This class is easy to use and customize so it is among the most useful classes in Qt. In general the button displays text but an icon can also be displayed.

QPushButton inherits QAbstractButton [qt.io] which inherits QWidget [qt.io].

Signals

Inherited from QAbstractButton

  • void clicked ( bool checked = false )
  • void pressed ()
  • void released ()
  • void toggled ( bool checked )

Inherited from QWidget

  • void customContextMenuRequested ( const QPoint & pos )

Inherited from QObject

  • void destroyed ( QObject * obj = 0 )

Basic Usage

Text

The text of QPushButton can be set upon creation or using setText() [qt.io]. To get the current text of the button use text() [qt.io].

Icon

The icon of QPushButton can also be set upon creation. After creation the icon can be changed using setIcon() [qt.io] To get the current icon of the button use icon() [qt.io]

Set Position and Size

To set the position and the size of the button use setGeometry() [qt.io]. If you want just to modify the size of the button use resize() [qt.io].

Handle Button

QPushButton emits signals if an event occurs. To handle the button connect its appropriate signal to a slot:

Example

The following simple code snippet shows how to create and use QPushButton. It has been tested on Qt Symbian Simulator.

An instance of QPushButton is created. Signal released() is connected to slot handleButton() which changes the text and the size of the button.

mainwindow.h

mainwindow.cpp

main.cpp

See also

Qt Buttons [developer.qt.nokia.com]
Basic Qt Programming Tutorial [developer.qt.nokia.com]

Categories: