How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (misc cleanup)
 
(13 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
{{LangSwitch}}
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
== Overview ==


[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
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.


'''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|فارسی]]
QPushButton inherits {{DocLink|QAbstractButton}} which in turn inherits {{DocLink|QWidget}}.
 
= How to Use QPushButton =
 
== QPushButton Overview ==
 
Using &quot;QPushButton&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qpushbutton.html 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 &quot;QAbstractButton&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html which inherits &quot;QWidget&amp;quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html.


== Signals ==
== Signals ==
Line 17: Line 13:
=== Inherited from QAbstractButton ===
=== Inherited from QAbstractButton ===


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


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


* void customContextMenuRequested ( const QPoint &amp; pos )
* <tt>void customContextMenuRequested(const QPoint &pos)</tt>


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


* void destroyed ( QObject * obj = 0 )
* <tt>void destroyed(QObject *obj = nullptr)</tt>


== Basic Usage ==
== Basic Usage ==
Line 34: Line 30:
=== Text ===
=== Text ===


The text of QPushButton can be set upon creation or using &quot;setText()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop. To get the current text of the button use &quot;text()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop.
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>.


=== Icon ===
=== Icon ===


The icon of QPushButton can also be set upon creation. After creation the icon can be changed using &quot;setIcon()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop To get the current icon of the button use &quot;icon()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop
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>


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


To set the position and the size of the button use &quot;setGeometry()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#geometry-prop. If you want just to modify the size of the button use &quot;resize()&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#size-prop.
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>


=== Handle Button ===
=== Handle Button ===
Line 48: Line 44:
QPushButton emits signals if an event occurs. To handle the button connect its appropriate signal to a slot:
QPushButton emits signals if an event occurs. To handle the button connect its appropriate signal to a slot:


<code>connect(m_button, SIGNAL (released()),this, SLOT (handleButton()));<code>
<code>
connect(m_button, &QPushButton::released, this, &MainWindow::handleButton);</code>


== Example ==
== Example ==
Line 54: Line 51:
The following simple code snippet shows how to create and use QPushButton. It has been tested on Qt Symbian Simulator.
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.
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.


=== mainwindow.h ===
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>


</code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
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.


#include &lt;QtGui/QMainWindow&amp;gt;<br />#include &lt;QtGui/QPushButton&amp;gt;<br />//#include &lt;QMainWindow&amp;gt;//For Qt5<br />//#include &lt;QPushButton&amp;gt;//For Qt5
=== mainwindow.h ===
 
<nowiki>
namespace Ui {<br /> class MainWindow;<br />}
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT
 
public:<br /> explicit MainWindow(QWidget *parent = 0);
 
private slots:<br /> void handleButton();


private:<br /> QPushButton *m_button;<br />};
#include <QMainWindow>
#include <QPushButton>


#endif // MAINWINDOW_H<br /><code>
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>


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


</code><br />#include &quot;mainwindow.h&amp;quot;
MainWindow::MainWindow(QWidget *parent)
 
  : QMainWindow(parent)
#include &lt;QtCore/QCoreApplication&amp;gt;<br />//#include &lt;QCoreApplication&amp;gt;//For Qt5
{
 
  // Create the button, make "this" the parent
MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent)<br />{<br /> // Create the button, make &quot;this&amp;quot; the parent<br /> m_button = new QPushButton(&quot;My Button&amp;quot;, this);<br /> // set size and location of the button<br /> m_button-&gt;setGeometry(QRect(QPoint(100, 100),<br /> QSize(200, 50)));
  m_button = new QPushButton("My Button", this);
 
  // set size and location of the button
// Connect button signal to appropriate slot<br /> connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));<br />}
  m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
 
void MainWindow::handleButton()<br />{<br /> // change the text<br /> m_button-&gt;setText(&quot;Example&amp;quot;);<br /> // resize button<br /> m_button-&gt;resize(100,100);<br />}<br /><code>
  // 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>


=== main.cpp ===
=== 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>


</code><br />#include &quot;mainwindow.h&amp;quot;
=== PushButtonExample.pro ===
 
<nowiki>
#include &lt;QtGui/QApplication&amp;gt;<br />//#include &lt;QApplication&amp;gt; //For Qt5
QT      += core widgets
 
TARGET = PushButtonExample
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);
TEMPLATE = app  
 
SOURCES += main.cpp mainwindow.cpp 
MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}
HEADERS += mainwindow.h
 
</nowiki>
<code>
 
== See also ==
 
&quot;Qt Buttons&amp;quot;:http://developer.qt.nokia.com/wiki/Qt_Buttons

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