Qt 信号槽

71 篇文章 4 订阅

信号槽
1.类似windows的消息机制
2.信号函数,只发送不需要知道接受者
3.槽函数(普通函数),只接受不管通信
4.QObject绑定

Qt信号槽原理
1.绑定信号函数和槽函数
2.调用信号函数(将信号写入队列)
3.主线程从队列中获取信号

在这里插入图片描述

信号处理的主循环


QApplication a(argc,argv);
//Enters the main event loop and waits until exit() is called
return a.exec()

创建testsign.pro QWidget 项目


xz@xiaqiu:~/study/csdn/day0/testsign$ ls
build  main.cpp  testsign.pro  widget.cpp  widget.h  widget.ui
xz@xiaqiu:~/study/csdn/day0/testsign$ 

打开设计器


xz@xiaqiu:~/study/csdn/day0/testsign$ designer 
build/        main.cpp      testsign.pro  widget.cpp    widget.h      widget.ui
xz@xiaqiu:~/study/csdn/day0/testsign$ designer widget.ui 

在这里插入图片描述

widget.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QPushButton" name="CloseButton">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>160</y>
     <width>241</width>
     <height>141</height>
    </rect>
   </property>
   <property name="text">
    <string>关闭窗口</string>
   </property>
  </widget>
  <widget class="QPushButton" name="minButton">
   <property name="geometry">
    <rect>
     <x>460</x>
     <y>160</y>
     <width>241</width>
     <height>141</height>
    </rect>
   </property>
   <property name="text">
    <string>最小化</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>CloseButton</sender>
   <signal>clicked()</signal>
   <receiver>Widget</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>244</x>
     <y>235</y>
    </hint>
    <hint type="destinationlabel">
     <x>305</x>
     <y>615</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>minButton</sender>
   <signal>clicked()</signal>
   <receiver>Widget</receiver>
   <slot>showMinimized()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>580</x>
     <y>230</y>
    </hint>
    <hint type="destinationlabel">
     <x>399</x>
     <y>299</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

ui_widget.h


#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QPushButton *CloseButton;
    QPushButton *minButton;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        CloseButton = new QPushButton(Widget);
        CloseButton->setObjectName(QString::fromUtf8("CloseButton"));
        CloseButton->setGeometry(QRect(100, 160, 241, 141));
        minButton = new QPushButton(Widget);
        minButton->setObjectName(QString::fromUtf8("minButton"));
        minButton->setGeometry(QRect(460, 160, 241, 141));

        retranslateUi(Widget);
        QObject::connect(CloseButton, &QPushButton::clicked, Widget, qOverload<>(&QWidget::close));
        QObject::connect(minButton, &QPushButton::clicked, Widget, qOverload<>(&QWidget::showMinimized));

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        CloseButton->setText(QCoreApplication::translate("Widget", "\345\205\263\351\227\255\347\252\227\345\217\243", nullptr));
        minButton->setText(QCoreApplication::translate("Widget", "\346\234\200\345\260\217\345\214\226", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值