How to Use QPushButton: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]] | [[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]] | ||
[toc align_right= | [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|Ελληνικά]]<br />[[How_to_Use_QPushButton_Russian|Русский]][[How_to_Use_QPushButton_Persian|فارسی]] | '''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|فارسی]] | ||
Line 9: | Line 9: | ||
== QPushButton Overview == | == QPushButton Overview == | ||
Using | 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 | 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 34: | Line 34: | ||
=== Text === | === Text === | ||
The text of QPushButton can be set upon creation or using | 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 === | === Icon === | ||
The icon of QPushButton can also be set upon creation. After creation the icon can be changed using | 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 === | === Set Position and Size === | ||
To set the position and the size of the button use | 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 === | === Handle Button === | ||
Line 60: | Line 60: | ||
</code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H | </code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H | ||
#include | #include <QtGui/QMainWindow><br />#include <QtGui/QPushButton><br />//#include <QMainWindow>//For Qt5<br />//#include <QPushButton>//For Qt5 | ||
namespace Ui {<br /> class MainWindow;<br />} | namespace Ui {<br /> class MainWindow;<br />} | ||
Line 76: | Line 76: | ||
=== mainwindow.cpp === | === mainwindow.cpp === | ||
</code><br />#include | </code><br />#include "mainwindow.h" | ||
#include | #include <QtCore/QCoreApplication><br />//#include <QCoreApplication>//For Qt5 | ||
MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent)<br />{<br /> // Create the button, make | MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent)<br />{<br /> // Create the button, make "this" the parent<br /> m_button = new QPushButton("My Button", this);<br /> // set size and location of the button<br /> m_button->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 />} | // 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- | void MainWindow::handleButton()<br />{<br /> // change the text<br /> m_button->setText("Example");<br /> // resize button<br /> m_button->resize(100,100);<br />}<br /><code> | ||
=== main.cpp === | === main.cpp === | ||
</code><br />#include | </code><br />#include "mainwindow.h" | ||
#include | #include <QtGui/QApplication><br />//#include <QApplication> //For Qt5 | ||
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv); | int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv); | ||
MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec | MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec();<br />} | ||
<code> | <code> | ||
Line 100: | Line 100: | ||
== See also == | == See also == | ||
"Qt Buttons":http://developer.qt.nokia.com/wiki/Qt_Buttons |
Revision as of 14:09, 24 February 2015
[toc align_right="yes" depth="3"]
English Български Spanish 简体中文 Ελληνικά
Русскийفارسی
How to Use QPushButton
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.
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
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
- 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();
}
See also
"Qt Buttons":http://developer.qt.nokia.com/wiki/Qt_Buttons