How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
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|فارسی]]
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]


=How to Use QPushButton=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


==QPushButton Overview==
'''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|فارسی]]


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.
= How to Use QPushButton =


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]''.
== QPushButton Overview ==


==Signals==
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.


===Inherited from QAbstractButton===
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 ==
 
=== Inherited from QAbstractButton ===


* void clicked ( bool checked = false )
* void clicked ( bool checked = false )
Line 18: Line 22:
* void toggled ( bool checked )
* void toggled ( bool checked )


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


* void customContextMenuRequested ( const QPoint &amp; pos )
* void customContextMenuRequested ( const QPoint &amp; pos )


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


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


==Basic Usage==
== Basic Usage ==


===Text===
=== Text ===


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 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.


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


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]''
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


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


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]''.
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.


===Handle Button===
=== Handle Button ===


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:


==Example==
<code>connect(m_button, SIGNAL (released()),this, SLOT (handleButton()));<code>
 
== Example ==


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.
Line 50: Line 56:
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 '''released()''' is connected to slot '''handleButton()''' which changes the text and the size of the button.


===mainwindow.h===
=== mainwindow.h ===
 
</code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
 
#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
 
namespace Ui {<br /> class MainWindow;<br />}
 
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 />};
 
#endif // MAINWINDOW_H<br /><code>
 
=== mainwindow.cpp ===
 
</code><br />#include &quot;mainwindow.h&amp;quot;
 
#include &lt;QtCore/QCoreApplication&amp;gt;<br />//#include &lt;QCoreApplication&amp;gt;//For Qt5
 
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)));
 
// Connect button signal to appropriate slot<br /> connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));<br />}
 
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>
 
=== main.cpp ===
 
</code><br />#include &quot;mainwindow.h&amp;quot;


===mainwindow.cpp===
#include &lt;QtGui/QApplication&amp;gt;<br />//#include &lt;QApplication&amp;gt; //For Qt5


===main.cpp===
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);


==See also==
MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}


[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]''
<code>


===Categories:===
== See also ==


* [[:Category:Developing-with-Qt|Developing with Qt]]
&quot;Qt Buttons&amp;quot;:http://developer.qt.nokia.com/wiki/Qt_Buttons
** [[:Category:Developing-with-Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:snippets|snippets]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 14:10, 23 February 2015




[toc align_right="yes&quot; depth="3&quot;]

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

How to Use QPushButton

QPushButton Overview

Using "QPushButton&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 "QAbstractButton&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html which inherits "QWidget&quot;:http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html.

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()":http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop. To get the current text of the button use "text()":http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop.

Icon

The icon of QPushButton can also be set upon creation. After creation the icon can be changed using "setIcon()":http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop To get the current icon of the button use "icon()":http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#icon-prop

Set Position and Size

To set the position and the size of the button use "setGeometry()":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#geometry-prop. If you want just to modify the size of the button use "resize()":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#size-prop.

Handle Button

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

connect(m_button, SIGNAL (released()),this, SLOT (handleButton()));<code>

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


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

  1. include <QtGui/QMainWindow&gt;
    #include <QtGui/QPushButton&gt;
    //#include <QMainWindow&gt;//For Qt5
    //#include <QPushButton&gt;//For Qt5

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);

private slots:
void handleButton();

private:
QPushButton *m_button;
};

  1. endif // MAINWINDOW_H
    === mainwindow.cpp ===
    

    #include "mainwindow.h&quot;
  1. include <QtCore/QCoreApplication&gt;
    //#include <QCoreApplication&gt;//For Qt5

MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent)
{
// Create the button, make "this&quot; the parent
m_button = new QPushButton("My Button&quot;, 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, SIGNAL (released()), this, SLOT (handleButton()));
}

void MainWindow::handleButton()
{
// change the text
m_button->setText("Example&quot;);
// resize button
m_button->resize(100,100);
}

=== main.cpp ===


#include "mainwindow.h&quot;

  1. include <QtGui/QApplication&gt;
    //#include <QApplication&gt; //For Qt5

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec&amp;#40;&#41;;
}

See also

"Qt Buttons&quot;:http://developer.qt.nokia.com/wiki/Qt_Buttons