Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to Use QPushButton/fa: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (→‎بررسی اجمالی: minor changes.)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=<font face="tahoma">توضیح مختصر</font>=
{{LangSwitch}}
{| style="direction:rtl;align:left"
|
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
== بررسی اجمالی ==


<font face="tahoma"> یکی از کنترل های موجود در محیط توسعه کیوت [http://doc.qt.nokia.com/latest/qpushbutton.html QPushButton] ''[doc.qt.nokia.com]''است که در اکثر محیط های برنامه نویسی موجود است<br /> QPushButton از کلاس [http://doc.qt.nokia.com/4.7/qabstractbutton.html QAbstractButton] ''[doc.qt.nokia.com]'' ارث برده که هر دو وارث [http://doc.qt.nokia.com/4.7/qwidget.html QWidget] ''[doc.qt.nokia.com]'' هستند.</font>
با استفاده از {{DocLink|QPushButton}} توسعه دهندگان می‌توانند دکمه‌ها را ایجاد و به کار گیری کنند. این کلاس به راحتی قابل استفاده و شخصی‌سازی است، بنابراین در میان پر استفاده‌ترین کلاس‌ها در Qt قرار دارد. به صورت کلی دکمه متن را نشان می‌دهد، اما همزمان می‌توان برای نمایش یک آیکون هم از آن استفاده کرد.


=<font face="tahoma">سیگنال ها</font>=
QPushButton از {{DocLink|QAbstractButton}} ارث‌بری شده است که به نوبه خود از کلاس {{DocLink|QWidget}} هم ارث‌بری می‌کند.


===<font face="tahoma"> ارث بری از QAbstractButton</font>===
== Signals(سیگنال‌ها) ==


<font face="tahoma">
===  سیگنال‌های ارث برده شده از QAbstractButton ===
* void clicked ( bool checked = false )
{| style="direction:ltr;align:left;display:flex;"
* <font face="tahoma">void pressed ()</font>
|
* <font face="tahoma">void released ()</font>
* <tt>void clicked ( bool checked = false )</tt>
* <font face="tahoma">void toggled ( bool checked )</font></font>
* <tt>void pressed ()</tt>
* <tt>void released ()</tt>
* <tt>void toggled ( bool checked )</tt>
|}
=== سیگنال‌های ارث برده شده از QWidget ===
{| style="direction:ltr;align:left;display:flex;"
|
* <tt>void customContextMenuRequested ( const QPoint &pos )</tt>
|}
=== سیگنال‌های ارث برده شده از QObject ===
{| style="direction:ltr;align:left;display:flex;"
|
* <tt>void destroyed ( QObject * obj = 0 )</tt>
|}
== استفاده پاییه ==


===<font face="tahoma"> ارث بری از QWidget</font>===
=== Text ===


<font face="tahoma">
The text of QPushButton can be set upon creation or using <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#text-prop setText()]</tt>. To get the current text of the button use <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#text-prop text()]</tt>.
* void customContextMenuRequested ( const QPoint &amp; pos )</font>


===<font face="tahoma"> ارث بری از QObject</font>===
=== Icon ===


<font face="tahoma">
The icon of QPushButton can also be set upon creation. After creation the icon can be changed using <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#icon-prop setIcon()]</tt> To get the current icon of the button use <tt>[http://doc.qt.io/qt-5/qabstractbutton.html#icon-prop icon()]</tt>
* void destroyed ( QObject * obj = 0 )</font>


==<font face="tahoma"> استفاده ساده</font>==
=== Set Position and Size ===


===<font face="tahoma"> تنظیم متن</font>===
To set the position and the size of the button use <tt>[http://doc.qt.io/qt-5/qwidget.html#geometry-prop setGeometry()]</tt>. If you want just to modify the size of the button use <tt>[http://doc.qt.io/qt-5/qwidget.html#geometry-prop resize()]</tt>


<font face="tahoma">برای تنظیم متن از [http://doc.qt.nokia.com/4.7/qabstractbutton.html#text-prop setText()] ''[doc.qt.nokia.com]'' استفاده می شود.برای دریافت متن کنونی از [http://doc.qt.nokia.com/4.7/qabstractbutton.html#text-prop text()] ''[doc.qt.nokia.com]''</font>
=== Handle Button ===
 
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>
 
== 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 <tt>released()</tt> is connected to slot <tt>handleButton()</tt> which changes the text and the size of the button.
 
To build and run the example:
# Create an empty folder
# 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.
# Using command line, navigate into the folder with the 4 files.
# run qmake on the project file: <code>qmake PushButtonExample.pro</code>
#* If successful it will not print any output.
#* This should create a file with the name <tt>Makefile</tt> in the folder.
# Build the application: <code>make</code>
#* The application should compile without any issues.
# Run the application: <code>./PushButtonExample</code>
 
The above steps are for linux but can easily be followed on other systems by replacing <tt>make</tt> with the correct make call for the system.
 
=== mainwindow.h ===
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
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
</code>
=== mainwindow.cpp ===
<code>
#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
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
}
</code>
 
=== main.cpp ===
<code>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWindow;
    mainWindow.showMaximized();
    return app.exec();
}
</code>
 
=== PushButtonExample.pro ===
<code>
QT      += core gui 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = PushButtonExample TEMPLATE = app
SOURCES += main.cpp \ mainwindow.cpp 
HEADERS += mainwindow.h
</code>
|}

Latest revision as of 22:52, 10 April 2022

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 هم ارث‌بری می‌کند.

Signals(سیگنال‌ها)

سیگنال‌های ارث برده شده از 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 )

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

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.

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

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.

mainwindow.h

 #ifndef MAINWINDOW_H
 #define MAINWINDOW_H
 
 #include <QMainWindow>
 #include <QPushButton>
 
 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 <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
    m_button->setText("Example");
    // resize button
    m_button->resize(100,100);
 }

main.cpp

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

PushButtonExample.pro

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