QT 信号与槽 QT简单加法器的实现

信号与槽背景:    面向过程     模块之间低耦合设计(高内聚)。            函数调用:                 直接调用                 回调调用(低耦合)    面向对象        模块之间低耦合设计(高内聚)           对象调用              直接调用              接口
摘要由CSDN通过智能技术生成

信号与槽

背景:

    面向过程

     模块之间低耦合设计(高内聚)。

            函数调用:

                 直接调用

                 回调调用(低耦合)

    面向对象

        模块之间低耦合设计(高内聚)

           对象调用

              直接调用

              接口调用

QT:

   信号与槽解决问题:

       对象的成员变量调用?

      对象的成员函数调用?

 

案例:

     窗体,有一个文本框

     线程,每个一秒改变文本框

     问题:

                            线程类访问窗体组件比较麻烦,需要传递。

                            在QT中,使用线程程序可能不工作。

 

代码:

 

#include<QApplication>

#include “shake.h”

int main(int  args , char **argv)

{

    QApplication  app(args , argv);

    ShakeForm dlg;

    dlg.setVisible(true);

     return  app.exec();

}

 

 

 

shake.h

 

#ifndef SHAKE_H

#define SHAKE_H

#include<QDialog>

#include<QLineEdit>

#include “mythread.h”

class ShakeForm : public QDialog

{

 public:

     ShakeForm(QWidget * parent=NULL);

     ~ShakeForm();

 private:

     QLineEdit *txt;

     ShakeThread *th;

};

#endif

 

 

shake.cpp

 

#include “shake.h”

 

ShakeForm::ShakeForm(QWidget *parent)

{

    txt = new QLineEdit(this);

    this->resize(400,300);

    txt->resize(100,30);

    txt->move(10,10);

    th=new ShakeThread(txt);

     th->start();

}

 

ShakeForm::~ShakeForm()

{

    delete txt;

}

 

 

mythread.h

 

#ifndef SHAKE_THREAD_H

#define SHAKE_THREAD_H

 

#include<pthread.h>

#include<QLineEdit>

class ShakeThread

{

 public:

     pthread_t  tid;

     static  void * s_run(void *);

     QLineEdit *m_txt;

 private:

     ShakeThread(QLineEdit *txt);

     void start();

     virtual void run();

     

};

#endif

 

 

 

mythread.cpp

 

#include “mythread.h”

#include<unistd.h>

#include<math.h>

 

ShakeThread::ShakeThread(QLineEdit *txt)

{

  m_txt=txt;

}

void *ShakeThread::s_run(void * data)

{

    ShakeThread *st=(ShakeThread*)da

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值