How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Corrected Layout after Wiki Conversion)
Line 1: Line 1:
[[Category:Developing with Qt::General]]
__NOEDITSECTION__
<div style="float:left;padding:14px;">__TOC__</div>
 
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Snippets]]
[[Category:Tutorial]]
[[Category:Tutorial]]
[toc align_right="yes" depth="3"]


'''English''' [[How_to_Use_QPushButton_Bulgarian|Български]] [[How_to_Use_QPushButton_Spanish|Spanish]] [[How_to_Use_QPushButton_SimplifiedChinese|简体中文]] [[How_to_Use_QPushButton_Greek|Ελληνικά]]
'''English''' [[How_to_Use_QPushButton_Bulgarian|Български]] [[How_to_Use_QPushButton_Spanish|Spanish]] [[How_to_Use_QPushButton_SimplifiedChinese|简体中文]] [[How_to_Use_QPushButton_Greek|Ελληνικά]]
[[How_to_Use_QPushButton_Russian|Русский]][[How_to_Use_QPushButton_Persian|فارسی]]
[[How_to_Use_QPushButton_Russian|Русский]][[How_to_Use_QPushButton_Persian|فارسی]]


= How to Use QPushButton =
== Overview ==


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


Using "QPushButton":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 [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html QAbstractButton] which in turn inherits [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html QWidget].
 
QPushButton inherits "QAbstractButton":http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html which inherits "QWidget":http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html.


== Signals ==
== Signals ==
Line 21: Line 19:
=== 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 = 0 )</tt>


== Basic Usage ==
== Basic Usage ==
Line 38: Line 36:
=== Text ===
=== 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.
The text of QPushButton can be set upon creation or using <tt>setText()</tt>. To get the current text of the button use <tt>text()</tt>.


=== Icon ===
=== 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
The icon of QPushButton can also be set upon creation. After creation the icon can be changed using <tt>setIcon()</tt> To get the current icon of the button use <tt>icon()</tt>


=== Set Position and Size ===
=== 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.
To set the position and the size of the button use <tt>setGeometry()</tt>. If you want just to modify the size of the button use <tt>resize()</tt>


=== Handle Button ===
=== Handle Button ===
Line 52: Line 50:
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, SIGNAL (released()),this, SLOT (handleButton()));
</code>


== Example ==
== Example ==
Line 58: Line 58:
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 ===
=== mainwindow.h ===


</code>
<code>
#ifndef MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#define MAINWINDOW_H
Line 90: Line 90:


#endif // MAINWINDOW_H
#endif // MAINWINDOW_H
<code>
</code>


=== mainwindow.cpp ===
=== mainwindow.cpp ===


</code>
<code>
#include "mainwindow.h"
#include "mainwindow.h"


Line 120: Line 120:
  m_button->resize(100,100);
  m_button->resize(100,100);
}
}
<code>
</code>


=== main.cpp ===
=== main.cpp ===


</code>
<code>
#include "mainwindow.h"
#include "mainwindow.h"


Line 138: Line 138:
  return app.exec();
  return app.exec();
}
}
 
</code>
<code>
 
== See also ==
 
"Qt Buttons":http://developer.qt.nokia.com/wiki/Qt_Buttons

Revision as of 12:07, 1 March 2015

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

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

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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
//#include <QMainWindow>//For Qt5
//#include <QPushButton>//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;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

#include <QtCore/QCoreApplication>
//#include <QCoreApplication>//For Qt5

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, SIGNAL (released()), this, SLOT (handleButton()));
}

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

main.cpp

#include "mainwindow.h"

#include <QtGui/QApplication>
//#include <QApplication> //For Qt5

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

MainWindow mainWindow;
 mainWindow.showMaximized();
 return app.exec();
}