How to USe QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Convert ExpressionEngine links)
Line 15: Line 15:
== QPushButton Overview ==
== QPushButton Overview ==


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


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


== Signals ==
== Signals ==
Line 40: Line 40:
=== 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 [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop setText()]. To get the current text of the button use [http://doc.qt.io/qt-5.0/qtwidgets/qabstractbutton.html#text-prop text()].


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


=== 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 [http://doc.qt.io/qt-5.0/qtwidgets/qwidget.html#geometry-prop setGeometry()]. 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()].


=== Handle Button ===
=== Handle Button ===
Line 145: Line 145:
== See also ==
== See also ==


"Qt Buttons":http://developer.qt.nokia.com/wiki/Qt_Buttons
[http://developer.qt.nokia.com/wiki/Qt_Buttons Qt Buttons]

Revision as of 08:43, 4 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

[toc align_right="yes" depth="3"]

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

How to Use QPushButton

QPushButton 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 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()));<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 ===
  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <QtGui/QMainWindow>
  2. 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;

};

  1. endif // MAINWINDOW_H
=== mainwindow.cpp ===
  1. include "mainwindow.h"
  1. 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 ===
  1. include "mainwindow.h"
  1. include <QtGui/QApplication>

//#include <QApplication> //For Qt5

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

QApplication app(argc, argv);

MainWindow mainWindow;

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

}

See also

Qt Buttons