#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
QApplication myapp(argc, argv);
QPushButton *btn = new QPushButton("Quit");
QObject::connect(btn,SIGNAL(clicked()),&myapp,SLOT(quit()));
btn->show();
return myapp.exec();
}
解析:
QPushButton:
The QPushButton widget provides a push button with a text or pixmap label.
QPushButton::QPushButton ( const char * text, QWidget * parent=0, const char * name=0 )
Constructs a push button with a text.The parent and name arguments are sent to the QWidget constructor.
QObject:
The QObject class is the base class of all Qt objects that can deal with signals, slots and events.
QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
const typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the receiver object.
Tips:
*connect(sender,signal,receiver,slot):
通俗地解释就是,对象A发射信号(这里当用户单击QPushButton时,该按钮就会发射一个click()信号),
对象B接到信号作出对于处理(这里QApplication接到按钮的clicked()信号,调用用quit()。)
*宏SIGNAL()和SLOT()是QT语法的一部分
附:
QT官网documentation中给出的connect()例子
QLabel *label = new QLabel;
QLineEdit *lineEdit = new QLineEdit;
QObject::connect(lineEdit, &QLineEdit::textChanged,
label, &QLabel::setText);
This example ensures that the label always displays the current line edit text.
#include<QPushButton>
int main(int argc, char *argv[])
{
QApplication myapp(argc, argv);
QPushButton *btn = new QPushButton("Quit");
QObject::connect(btn,SIGNAL(clicked()),&myapp,SLOT(quit()));
btn->show();
return myapp.exec();
}
运行结果:
解析:
QPushButton:
The QPushButton widget provides a push button with a text or pixmap label.
QPushButton::QPushButton ( const char * text, QWidget * parent=0, const char * name=0 )
Constructs a push button with a text.The parent and name arguments are sent to the QWidget constructor.
QObject:
The QObject class is the base class of all Qt objects that can deal with signals, slots and events.
QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
const typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the receiver object.
Tips:
*connect(sender,signal,receiver,slot):
通俗地解释就是,对象A发射信号(这里当用户单击QPushButton时,该按钮就会发射一个click()信号),
对象B接到信号作出对于处理(这里QApplication接到按钮的clicked()信号,调用用quit()。)
*宏SIGNAL()和SLOT()是QT语法的一部分
附:
QT官网documentation中给出的connect()例子
QLabel *label = new QLabel;
QLineEdit *lineEdit = new QLineEdit;
QObject::connect(lineEdit, &QLineEdit::textChanged,
label, &QLabel::setText);
This example ensures that the label always displays the current line edit text.