How to Use QPushButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Cleanup)
(Added needed pro file and updated other files to use the normal #include without the modules)
Line 53: Line 53:


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.
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.
Run <tt>qmake</tt> on the <tt>PushButtonExample.pro</tt> file and then <tt>make</tt> and then it should be able to run.


=== mainwindow.h ===
=== mainwindow.h ===
Line 60: Line 62:
#define MAINWINDOW_H
#define MAINWINDOW_H


#include <QtGui/QMainWindow>
#include <QMainWindow>
#include <QtGui/QPushButton>
#include <QPushButton>
//#include <QMainWindow>//For Qt5
//#include <QPushButton>//For Qt5


namespace Ui {
namespace Ui {
class MainWindow;
    class MainWindow;
}
}


class MainWindow : public QMainWindow
class MainWindow : public QMainWindow
{
{
Q_OBJECT
    Q_OBJECT
 
public:
public:
explicit MainWindow(QWidget *parent = 0);
    explicit MainWindow(QWidget *parent = 0);
 
private slots:
private slots:
void handleButton();
    void handleButton();
 
private:
private:
QPushButton *m_button;
    QPushButton *m_button;
};
};


Line 91: Line 88:
#include "mainwindow.h"
#include "mainwindow.h"


#include <QtCore/QCoreApplication>
#include <QCoreApplication>
//#include <QCoreApplication>//For Qt5


MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
    : QMainWindow(parent)
{
{
// Create the button, make "this" the parent
    // Create the button, make "this" the parent
m_button = new QPushButton("My Button", this);
    m_button = new QPushButton("My Button", this);
// set size and location of the button
    // set size and location of the button
m_button->setGeometry(QRect(QPoint(100, 100),
    m_button->setGeometry(QRect(QPoint(100, 100),
QSize(200, 50)));
    QSize(200, 50)));


// Connect button signal to appropriate slot
    // Connect button signal to appropriate slot
connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));
    connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));
}
}


void MainWindow::handleButton()
void MainWindow::handleButton()
{
{
// change the text
    // change the text
m_button->setText("Example");
    m_button->setText("Example");
// resize button
    // resize button
m_button->resize(100,100);
    m_button->resize(100,100);
}
}
</code>
</code>
Line 121: Line 117:
#include "mainwindow.h"
#include "mainwindow.h"


#include <QtGui/QApplication>
#include <QApplication>  
//#include <QApplication> //For Qt5


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


MainWindow mainWindow;
    MainWindow mainWindow;
mainWindow.showMaximized();
    mainWindow.showMaximized();
return app.exec();
    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>
</code>

Revision as of 15:28, 10 November 2015

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.

Run qmake on the PushButtonExample.pro file and then make and then it should be able to run.

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