How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (misc cleanup)
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''English''' [[How to Use QPushButton Bulgarian|Български]] [[How to Use QPushButton Spanish|Spanish]] [[How to Use QPushButton SimplifiedChinese|简体中文]] [[How to Use QPushButton Greek|Ελληνικά]]<br />[[How to Use QPushButton Russian|Русский]][[How to Use QPushButton Persian|فارسی]]
{{LangSwitch}}
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
== Overview ==


=How to Use QPushButton=
Using {{DocLink|QPushButton}} 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 Overview==
QPushButton inherits {{DocLink|QAbstractButton}} which in turn inherits {{DocLink|QWidget}}.


Using [http://doc.qt.io/qt-5.0/qtwidgets/qpushbutton.html 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.
== Signals ==


QPushButton inherits [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html QAbstractButton] ''[qt.io]'' which inherits [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget] ''[qt.io]''.
=== Inherited from QAbstractButton ===


==Signals==
* <tt>void clicked(bool checked = false)</tt>
* <tt>void pressed()</tt>
* <tt>void released()</tt>
* <tt>void toggled(bool checked)</tt>


===Inherited from QAbstractButton===
=== Inherited from QWidget ===


* void clicked ( bool checked = false )
* <tt>void customContextMenuRequested(const QPoint &pos)</tt>
* void pressed ()
* void released ()
* void toggled ( bool checked )


===Inherited from QWidget===
=== Inherited from QObject ===


* void customContextMenuRequested ( const QPoint &amp; pos )
* <tt>void destroyed(QObject *obj = nullptr)</tt>


===Inherited from QObject===
== Basic Usage ==


* void destroyed ( QObject * obj = 0 )
=== Text ===


==Basic Usage==
The text of QPushButton can be set upon creation or using <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#text-prop setText()]</tt>. To get the current text of the button use <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#text-prop text()]</tt>.


===Text===
=== Icon ===


The text of QPushButton can be set upon creation or using [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop setText()] ''[qt.io]''. To get the current text of the button use [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop text()] ''[qt.io]''.
The icon of QPushButton can also be set upon creation. After creation the icon can be changed using <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#icon-prop setIcon()]</tt> To get the current icon of the button use <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#icon-prop icon()]</tt>


===Icon===
=== Set Position and Size ===


The icon of QPushButton can also be set upon creation. After creation the icon can be changed using [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop setIcon()] ''[qt.io]'' To get the current icon of the button use [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop icon()] ''[qt.io]''
To set the position and the size of the button use <tt>[http://doc.qt.io/qt-5/qwidget.html#geometry-prop setGeometry()]</tt>. If you want just to modify the size of the button use <tt>[http://doc.qt.io/qt-5/qwidget.html#geometry-prop resize()]</tt>


===Set Position and Size===
=== Handle Button ===


To set the position and the size of the button use [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#geometry-prop setGeometry()] ''[qt.io]''. If you want just to modify the size of the button use [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#size-prop resize()] ''[qt.io]''.
QPushButton emits signals if an event occurs. To handle the button connect its appropriate signal to a slot:


===Handle Button===
<code>
connect(m_button, &QPushButton::released, this, &MainWindow::handleButton);</code>


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.


==Example==
An instance of QPushButton is created. Signal <tt>released()</tt> is connected to slot <tt>handleButton()</tt> which changes the text and the size of the button.


The following simple code snippet shows how to create and use QPushButton. It has been tested on Qt Symbian Simulator.
To build and run the example:
# Create an empty folder
# Create a file for each of the below code snippets and add the example code to them (the name of the file should match the name above the snippet).
#* All 4 files must be in the same folder.
# Using command line, navigate into the folder with the 4 files.
# run qmake on the project file: <code>qmake PushButtonExample.pro</code>
#* If successful it will not print any output.
#* This should create a file with the name <tt>Makefile</tt> in the folder.
# Build the application: <code>make</code>
#* The application should compile without any issues.
# Run the application: <code>./PushButtonExample</code>


An instance of QPushButton is created. Signal '''released()''' is connected to slot '''handleButton()''' which changes the text and the size of the button.
The above steps are for linux but can easily be followed on other systems by replacing <tt>make</tt> with the correct make call for the system.  


===mainwindow.h===
=== mainwindow.h ===
<nowiki>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


===mainwindow.cpp===
#include <QMainWindow>
#include <QPushButton>


===main.cpp===
namespace Ui {
  class MainWindow;
}
class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  explicit MainWindow(QWidget *parent = nullptr);
private slots:
  void handleButton();
private:
  QPushButton *m_button;
};
#endif // MAINWINDOW_H
</nowiki>


==See also==
=== mainwindow.cpp ===
<nowiki>
#include "mainwindow.h"


[http://developer.qt.nokia.com/wiki/Qt_Buttons Qt Buttons] ''[developer.qt.nokia.com]''<br />[http://developer.qt.nokia.com/wiki/Basic_Qt_Programming_Tutorial Basic Qt Programming Tutorial] ''[developer.qt.nokia.com]''
MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
{
  // Create the button, make "this" the parent
  m_button = new QPushButton("My Button", this);
  // set size and location of the button
  m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
  // Connect button signal to appropriate slot
  connect(m_button, &QPushButton::released, this, &MainWindow::handleButton);
}
void MainWindow::handleButton()
{
  // change the text
  m_button->setText("Example");
  // resize button
  m_button->resize(100,100);
}
</nowiki>


===Categories:===
=== main.cpp ===
<nowiki>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  MainWindow mainWindow;
  mainWindow.showMaximized();
  return app.exec();
}
</nowiki>


* [[:Category:Developing-with-Qt|Developing with Qt]]
=== PushButtonExample.pro ===
** [[:Category:Developing-with-Qt::General|General]]
<nowiki>
* [[:Category:HowTo|HowTo]]
QT      += core widgets
* [[:Category:snippets|snippets]]
TARGET = PushButtonExample
* [[:Category:Tutorial|Tutorial]]
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp 
HEADERS += mainwindow.h
</nowiki>

Latest revision as of 16:12, 29 November 2020

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Overview

Using QPushButton 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 which in turn inherits QWidget.

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 = nullptr)

Basic Usage

Text

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

Icon

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

Set Position and Size

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

Handle Button

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

connect(m_button, &QPushButton::released, this, &MainWindow::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.

To build and run the example:

  1. Create an empty folder
  2. Create a file for each of the below code snippets and add the example code to them (the name of the file should match the name above the snippet).
    • All 4 files must be in the same folder.
  3. Using command line, navigate into the folder with the 4 files.
  4. run qmake on the project file:
    qmake PushButtonExample.pro
    
    • If successful it will not print any output.
    • This should create a file with the name Makefile in the folder.
  5. Build the application:
    make
    
    • The application should compile without any issues.
  6. Run the application:
    ./PushButtonExample
    

The above steps are for linux but can easily be followed on other systems by replacing make with the correct make call for the system.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>

namespace Ui {
  class MainWindow;
}
 
class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  explicit MainWindow(QWidget *parent = nullptr);
private slots:
  void handleButton();
private:
  QPushButton *m_button;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
{
  // Create the button, make "this" the parent
  m_button = new QPushButton("My Button", this);
  // set size and location of the button
  m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
 
  // Connect button signal to appropriate slot
  connect(m_button, &QPushButton::released, this, &MainWindow::handleButton);
}
 
void MainWindow::handleButton()
{
  // change the text
  m_button->setText("Example");
  // resize button
  m_button->resize(100,100);
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
 
int main(int argc, char *argv[]) 
{
  QApplication app(argc, argv);
  MainWindow mainWindow;
  mainWindow.showMaximized();
  return app.exec();
}
 

PushButtonExample.pro

QT       += core widgets
TARGET = PushButtonExample
TEMPLATE = app 
SOURCES += main.cpp mainwindow.cpp  
HEADERS += mainwindow.h