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

How to Use QPushButton

QPushButton Overview

Using QPushButton [qt-project.org] 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-project.org] which inherits QWidget [qt-project.org].

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-project.org]. To get the current text of the button use text() [qt-project.org].

Icon

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

Set Position and Size

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

Handle Button

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

  1. connect(m_button, SIGNAL(released()),this, SLOT(handleButton()));

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

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui/QMainWindow>
  5. #include <QtGui/QPushButton>
  6.  
  7. namespace Ui {
  8.     class MainWindow;
  9. }
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13.     Q_OBJECT
  14.  
  15. public:
  16.     explicit MainWindow(QWidget *parent = 0);
  17.  
  18. private slots:
  19.     void handleButton();
  20.  
  21. private:
  22.     QPushButton *m_button;
  23. };
  24.  
  25. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <QtCore/QCoreApplication>
  4.  
  5. MainWindow::MainWindow(QWidget *parent)
  6.     : QMainWindow(parent)
  7. {
  8.     // Create the button, make "this" the parent
  9.     m_button = new QPushButton("My Button", this);
  10.     // set size and location of the button
  11.     m_button->setGeometry(QRect(QPoint(100, 100),
  12.                                  QSize(200, 50)));
  13.  
  14.     // Connect button signal to appropriate slot
  15.     connect(m_button, SIGNAL(released()), this, SLOT(handleButton()));
  16. }
  17.  
  18. void MainWindow::handleButton()
  19. {
  20.     // change the text
  21.     m_button->setText("Example");
  22.     // resize button
  23.     m_button->resize(100,100);
  24. }

main.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <QtGui/QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication app(argc, argv);
  8.  
  9.     MainWindow mainWindow;
  10.     mainWindow.showMaximized();
  11.     return app.exec();
  12. }

See also

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

Categories: