How to Use QPushButton/fa

From Qt Wiki
Jump to navigation Jump to search

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

بررسی اجمالی

با استفاده از QPushButton توسعه دهندگان می‌توانند دکمه‌ها را ایجاد و به کار گیری کنند. این کلاس به راحتی قابل استفاده و شخصی‌سازی است، بنابراین در میان پر استفاده‌ترین کلاس‌ها در Qt قرار دارد. به صورت کلی دکمه متن را نشان می‌دهد، اما همزمان می‌توان برای نمایش یک آیکون هم از آن استفاده کرد.

QPushButton از QAbstractButton ارث‌بری شده است که به نوبه خود از کلاس QWidget هم ارث‌بری می‌کند.


سیگنال‌های ارث برده شده از QAbstractButton

  • void clicked ( bool checked = false )
  • void pressed ()
  • void released ()
  • void toggled ( bool checked )

سیگنال‌های ارث برده شده از QWidget

  • void customContextMenuRequested ( const QPoint &pos )

سیگنال‌های ارث برده شده از QObject

  • void destroyed ( QObject * obj = 0 )

استفاده پاییه


The text of QPushButton can be set upon creation or using setText(). To get the current text of the button use text().


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()));


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:
    • If successful it will not print any output.
    • This should create a file with the name Makefile in the folder.
  5. Build the application:
    • The application should compile without any issues.
  6. Run the application:

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.


 #include <QMainWindow>
 #include <QPushButton>
 namespace Ui {
    class MainWindow;
 class MainWindow : public QMainWindow
    explicit MainWindow(QWidget *parent = 0);
 private slots:
    void handleButton();
    QPushButton *m_button;
 #endif // MAINWINDOW_H


 #include "mainwindow.h"
 #include <QCoreApplication>
 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
    // resize button


 #include "mainwindow.h"
 #include <QApplication>
 int main(int argc, char *argv[]) 
     QApplication app(argc, argv);
     MainWindow mainWindow;
     return app.exec();

 QT       += core gui  
 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 TARGET = PushButtonExample TEMPLATE = app 
 SOURCES += main.cpp \ mainwindow.cpp  
 HEADERS += mainwindow.h