初识QT 5项目-爱代码爱编程
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
// 为使用 信号槽,必须定义这个宏
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
widget.ui
添加一个 Label 和 一个 PushButton
ui_widget.h
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *LabDemo;
QPushButton *btnClose;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QStringLiteral("Widget"));
Widget->resize(400, 300);
LabDemo = new QLabel(Widget);
LabDemo->setObjectName(QStringLiteral("LabDemo"));
// Geometry几何(即大小)
LabDemo->setGeometry(QRect(70, 92, 81, 20));
btnClose = new QPushButton(Widget);
btnClose->setObjectName(QStringLiteral("btnClose"));
btnClose->setGeometry(QRect(240, 90, 75, 23));
// 设置 Widget的WindowTitle、LabDemo的Text、btnClose的Text
retranslateUi(Widget);
// 设置 信号槽
QObject::connect(btnClose, SIGNAL(clicked()), Widget, SLOT(close()));
// 设置槽函数的关联方式,用于将UI设计器自动生成的组件信号的槽函数与组件信号相关联
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QApplication::translate("Widget", "My First Demo", Q_NULLPTR));
LabDemo->setText(QApplication::translate("Widget", "Hello World", Q_NULLPTR));
btnClose->setText(QApplication::translate("Widget", "Close", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
简要说明:
Widget类 继承 QWidget,private包含一个 Ui::Widget* 指针对象 ui
Widget类构造时:
先调用 父类QWidget的构造函数,
再 new一个 Ui::Widget对象实例化 ui,
最后调用 ui->setupUi(this),实际调用 Ui_Widget::setupUi(QWidget *Widget)
Ui_Widget
ui_widget.h 中 定义类 Ui::Widget,其继承自 Ui_Widget
namespace Ui
{
class Widget: public Ui_Widget {};
}
继承关系
QLabel : public QFrame
QFrame : public QWidget
QWidget : public QObject
对应UI 设计器:
QPushButton : public QAbstractButton
QAbstractButton : public QWidget
QWidget : public QObject
对应UI设计器:
信号槽函数
static QMetaObject::Connection connect(
const QObject *sender, const char *signal,
const QObject *receiver, const char *member,
Qt::ConnectionType = Qt::AutoConnection);
static QMetaObject::Connection connect(
const QObject *sender, const QMetaMethod &signal,
const QObject *receiver, const QMetaMethod &method,
Qt::ConnectionType type = Qt::AutoConnection);
inline QMetaObject::Connection connect(
const QObject *sender, const char *signal,
const char *member,
Qt::ConnectionType type = Qt::AutoConnection) const;
template <typename Func1, typename Func2>
static inline 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)
//connect to a function pointer (not a member)
template <typename Func1, typename Func2>
static inline typename std::enable_if<int(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0, QMetaObject::Connection>::type
connect(
const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
Func2 slot)
{ }
//connect to a function pointer (not a member)
template <typename Func1, typename Func2>
static inline typename std::enable_if<int(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0 &&
!QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction, QMetaObject::Connection>::type
connect(
const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
const QObject *context, Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
{ }
//connect to a functor
template <typename Func1, typename Func2>
static inline typename std::enable_if<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::type
connect(
const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
Func2 slot)
{ }
//connect to a functor, with a "context" object defining in which event loop is going to be executed
template <typename Func1, typename Func2>
static inline typename std::enable_if<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::type
connect(
const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
const QObject *context, Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
{ }
// 实际调用第3个函数
inline QMetaObject::Connection QObject::connect(
const QObject *asender, const char *asignal,
const char *amember,
Qt::ConnectionType atype) const
{ return connect(asender, asignal, this, amember, atype); }
// internal slot-name based connect
static void connectSlotsByName(QObject *o);
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/a731062834/article/details/110922236