How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Correccion visualizacion de codigo, Importante agregar "Espacio" al inicio de cada linea del codigo, de lo contrario agrega numeracion.)
m (Correcion Visualizacion)
Line 71: Line 71:
<code>
<code>
  #ifndef MAINWINDOW_H
  #ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  <nowiki>#</nowiki>define MAINWINDOW_H
   
   
  #include <QMainWindow>
  <nowiki>#</nowiki>include <QMainWindow>
  #include <QPushButton>
  <nowiki>#</nowiki>include <QPushButton>
   
   
  namespace Ui {
  namespace Ui {
    class MainWindow;
<nowiki> </nowiki>  class MainWindow;
  }
  }
   
   
  class MainWindow : public QMainWindow
  class MainWindow : public QMainWindow
  {
  {
    Q_OBJECT
<nowiki> </nowiki>  Q_OBJECT
  public:
  public:
    explicit MainWindow(QWidget *parent = 0);
<nowiki> </nowiki>  explicit MainWindow(QWidget *parent = 0);
  private slots:
  private slots:
    void handleButton();
<nowiki> </nowiki>  void handleButton();
  private:
  private:
    QPushButton *m_button;
<nowiki> </nowiki>  QPushButton *m_button;
  };
  };
   
   
  #endif // MAINWINDOW_H
  <nowiki>#</nowiki>endif // MAINWINDOW_H
</code>
</code>


Line 98: Line 98:
  #include "mainwindow.h"
  #include "mainwindow.h"
   
   
  #include <QCoreApplication>
  <nowiki>#</nowiki>include <QCoreApplication>
   
   
  MainWindow::MainWindow(QWidget *parent)
  MainWindow::MainWindow(QWidget *parent)
Line 122: Line 122:
</code>
</code>


=== main.cpp ===
=== 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> ===
<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> ===
  #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>

Revision as of 17:39, 1 May 2017

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

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.

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
 <nowiki>#</nowiki>define MAINWINDOW_H
 
 <nowiki>#</nowiki>include <QMainWindow>
 <nowiki>#</nowiki>include <QPushButton>
 
 namespace Ui {
 <nowiki> </nowiki>  class MainWindow;
 }
 
 class MainWindow : public QMainWindow
 {
 <nowiki> </nowiki>  Q_OBJECT
 public:
 <nowiki> </nowiki>  explicit MainWindow(QWidget *parent = 0);
 private slots:
 <nowiki> </nowiki>  void handleButton();
 private:
 <nowiki> </nowiki>  QPushButton *m_button;
 };
 
 <nowiki>#</nowiki>endif // MAINWINDOW_H

mainwindow.cpp

 #include "mainwindow.h"
 
 <nowiki>#</nowiki>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