当前位置: 首页 > news >正文

企业网站建设的背景小型网站维护

企业网站建设的背景,小型网站维护,营销的网站建设公司,上海推广系统知识点#xff1a;启动进程 #xff0c;线程 #xff0c;线程同步互斥 1 启动进程 应用场景#xff1a;通常在qt中打开另一个程序 process模板 QString program “/bin/ls; QStringList arguments; arguments -l “-a;QPro…知识点启动进程 线程 线程同步互斥 1 启动进程 应用场景通常在qt中打开另一个程序 process模板 QString program “/bin/ls; QStringList arguments; arguments -l “-a;QProcess *myProcess new QProcess(parent); myProcess-execute(program, arguments);示例 widget.h #ifndef WIDGET_H #define WIDGET_H#include QWidget #include QLineEdit #include QPushButton #include QFileDialog #include QProcess #include QStringListclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent 0);~Widget(); public slots:void showfile(){QString filename QFileDialog::getOpenFileName();le-setText(filename);QStringList arg {filename};QProcess ppp;ppp.execute(notepad, arg);}private:QLineEdit *le;QPushButton *pb; };#endif // WIDGET_Hwidget.cpp #include widget.h #include QVBoxLayoutWidget::Widget(QWidget *parent): QWidget(parent) {le new QLineEdit;pb new QPushButton(showtxt);QVBoxLayout *vbox new QVBoxLayout;vbox-addWidget(le);vbox-addWidget(pb);setLayout(vbox);connect(pb, SIGNAL(clicked(bool)), this, SLOT(showfile())); }Widget::~Widget() {}效果在qt中使用文本编辑器打开一个文本。  2 线程 应用场景启动一个线程和进程来辅助程序 线程 class WorkerThread : public Qthread{ Q_OBJECTvoid run() {/* ... here is the expensive or blocking operation ... */emit resultReady(result);} signals:void resultReady(const QString s);}; WorkerThread x; x.start();void run()方法这是QThread的一个虚函数你需要在子类中重写它以实现线程的任务。当调用线程的start()方法时这里的代码将在新的线程中执行。 void resultReady(const QString s)信号这是一个自定义信号当线程完成工作并有结果可供返回时将通过emit关键字发射这个信号。 QT是信号驱动、或者异步驱动的框架平时app需要执行到a.exec()才会执行 双线程示例一个线程打印数组一个线程排序数组 thread_show.h(继承QThread类) #ifndef THREAD_SHOW_H #define THREAD_SHOW_H#include Qthread #include QDebug #include QMutexclass thread_show : public QThread {Q_OBJECT public:thread_show(char *p):m_arr(p){}void run() //这是QThread的一个虚函数(qt中斜线表示你需要在子类中重写它以实现线程的任务。当调用线程的start()方法时这里的代码将在新的线程中执行。{while(1){//lockqDebug()m_arr;sleep(1);}} private:char *m_arr;};#endif // THREAD_SHOW_Hthread_show.cpp重写构造函数 #include thread_show.h/* thread_show::thread_show() {} */thread_rev.h #ifndef THREAD_REV_H #define THREAD_REV_H#include QThreadclass thread_rev : public QThread {Q_OBJECT public:thread_rev(char *p):m_arr(p){} //构造时直接赋值效率高void run(){while(1){for(int i0; i5; i){m_arr[i] ^ m_arr[9-i];m_arr[9-i] ^ m_arr[i];m_arr[i] ^ m_arr[9-i];}}} private:char *m_arr; };#endif // THREAD_REV_Hthread_rev.cpp重写构造函数 #include thread_rev.h/* thread_rev::thread_rev() {} */main.cpp #include widget.h #include QApplication #include thread_rev.h #include thread_show.hint main(int argc, char *argv[]) {QApplication a(argc, argv);char arr[] 0123456789;thread_show t1(arr); //打印thread_rev t2(arr); //翻转数组t1.start();t2.start();return a.exec(); }效果两个线程同时操作打印出来的数组无规律 3 线程互斥 官方示例 QSemaphore QSemaphore sem(5); // sem.available() 5sem.acquire(3); // sem.available() 2sem.acquire(2); // sem.available() 0sem.release(5); // sem.available() 5sem.release(5); // sem.available() 10sem.tryAcquire(1); // sem.available() 9, returns truesem.tryAcquire(250); // sem.available() 9, returns falseQMutex //lockerQMutex mutex;void method1(){mutex.lock();mutex.unlock();}void method2(){mutex.lock();mutex.unlock();}实验示例 thread_show.h #ifndef THREAD_SHOW_H #define THREAD_SHOW_H#include Qthread #include QDebug #include QMutexclass thread_show : public QThread {Q_OBJECT public:thread_show(char *p, QMutex *l):m_arr(p), m_arrlock(l){}void run() //这是QThread的一个虚函数(qt中斜线表示你需要在子类中重写它以实现线程的任务。当调用线程的start()方法时这里的代码将在新的线程中执行。{while(1){m_arrlock-lock();qDebug()m_arr;m_arrlock-unlock();sleep(1);}} private:char *m_arr;QMutex *m_arrlock;};#endif // THREAD_SHOW_Hthread_show.cpp #include thread_show.h/* thread_show::thread_show() {} */thread_rev.h #ifndef THREAD_REV_H #define THREAD_REV_H#include QThread #include QMutexclass thread_rev : public QThread {Q_OBJECT public:thread_rev(char *p, QMutex *l):m_arr(p), m_arrlock(l){}void run(){while(1){m_arrlock-lock();for(int i0; i5; i){m_arr[i] ^ m_arr[9-i];m_arr[9-i] ^ m_arr[i];m_arr[i] ^ m_arr[9-i];}m_arrlock-unlock();}} private:char *m_arr;QMutex *m_arrlock;};#endif // THREAD_REV_Hthread_rev.cpp #include thread_rev.h/* thread_rev::thread_rev() {} */main.cpp #include widget.h #include QApplication #include thread_rev.h #include thread_show.h #include QMutexint main(int argc, char *argv[]) {QApplication a(argc, argv);char arr[] 0123456789;QMutex arr_lock;thread_show t1(arr,arr_lock); //打印thread_rev t2(arr,arr_lock); //翻转数组t1.start();t2.start();return a.exec(); }效果两个线程同时操作打印出来的数组均为排序后的结果排序中不会受到干扰 4 线程同步 目的实现一个线程获取资源需要等另一个线程释放资源。 thread_hello.h #ifndef THREAD_HELLO_H #define THREAD_HELLO_H#include QSemaphore #include QThread #include QDebugclass thread_hello : public QThread {Q_OBJECT public:thread_hello(QSemaphore *s):sem(s){ }void run(){while(1){qDebug()hello;sleep(1);//Vsem-release();}} private:QSemaphore *sem; };#endif // THREAD_HELLO_Hthread_hello.cpp #include thread_hello.h/* thread_hello::thread_hello() {} */thread_world.h #ifndef THREAD_WORLD_H #define THREAD_WORLD_H#include QThread #include QDebug #include QSemaphoreclass thread_world : public QThread {Q_OBJECT public:thread_world(QSemaphore *s):sem(s){ }void run() //字体歪的就是虚函数{while(1){//Psem-acquire();qDebug()world;}} private:QSemaphore *sem; };#endif // THREAD_WORLD_Hthread_world.cpp #include thread_hello.h/* thread_hello::thread_hello() {} */main.cpp #include QCoreApplication #include thread_hello.h #include thread_world.h #include QSemaphoreint main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QSemaphore sem;thread_hello hello(sem);thread_world world(sem);hello.start();world.start();return a.exec(); }效果hello先打印才会有world。  综合示例实现两个进度条在下载 mythread1.h #ifndef MYTHREAD1_H #define MYTHREAD1_H#include QThreadclass myThread1 : public QThread {Q_OBJECT signals:downloaded(int); public:myThread1();void run(){for(int i0;i100; i){//p1-setValue(i);emit downloaded(i);QThread::sleep(2);}} };#endif // MYTHREAD1_Hmythread1.cpp #include mythread1.hmyThread1::myThread1() {}mythread2.h #ifndef MYTHREAD2_H #define MYTHREAD2_H#include QThreadclass myThread2 : public QThread {Q_OBJECT signals:downloaded(int); public:myThread2();void run(){for(int i0;i100; i){//p1-setValue(i);emit downloaded(i);QThread::sleep(1);}} };#endif // MYTHREAD2_Hmythread1.cpp #include mythread2.hmyThread2::myThread2() {}widget.h #ifndef WIDGET_H #define WIDGET_H#include QWidget #include QProgressBar #include mythread2.h #include mythread1.hclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent 0);~Widget(); private:QProgressBar *p1, *p2;myThread1 *t1;myThread2 *t2; };#endif // WIDGET_Hwidget.cpp #include widget.h #include QVBoxLayout #include QThreadWidget::Widget(QWidget *parent): QWidget(parent) {p1 new QProgressBar;p2 new QProgressBar;QVBoxLayout *vbox new QVBoxLayout;vbox-addWidget(p1);vbox-addWidget(p2);setLayout(vbox);t1 new myThread1;t2 new myThread2;connect(t1, SIGNAL(downloaded(int)), p1, SLOT(setValue(int)));connect(t2, SIGNAL(downloaded(int)), p2, SLOT(setValue(int)));t1-start();t2-start();#if 0for(int i0;i100; i){p1-setValue(i);QThread::sleep(1);}for(int i0;i100; i){p2-setValue(i);QThread::sleep(2);} #endif }Widget::~Widget() {}注 downloaded不需要实现,qt实现的不是函数 emit发送一个信号 exit 发送信号
http://www.hkea.cn/news/14465771/

相关文章:

  • 深圳手机网站建设保险公司早会新闻资讯
  • 郑州网站设计收费做区位图的网站
  • 瑞丽住建局网站wordpress前后台域名分离
  • 河南中国建设厅官方网站用文件传输协议登录网站
  • 中国著名摄影网站网站开发技术可行性分析
  • 做网站开发学什么电商网页美工设计
  • 高端网站建设域名注册windows搭建wordpress博客
  • 百度合伙人官方网站网站建设 商城
  • 一个主体如何添加网站茶叶网站建设方案
  • 设计名字的网站医疗器械四大龙头企业
  • 做兼职网上哪个网站好西宁网站制作公司
  • 域名和网站的区别2021年网络营销考试题及答案
  • 南宁网站建设优化服务网站模板 国外
  • 无锡网站建设策划方案中华建设
  • 社交网站开发平台企业做网站要多少钱
  • 苏州住房建设局网站首页镇江建站推广报价
  • 营销网站 需求说明书wordpress typecho 大数据
  • 好多网站权重都没了网站主体负责人必须是法人吗
  • 怎么制作弹幕网站中国最大的中文网站
  • 什么网站免费做推广固安建设网站
  • 怎么让自己做的网站让别人看到怎么利用国外网站做互联网挣钱
  • 古色古香的网站模板win7网站建设
  • 湛江做网站的有哪些北京市住房建设厅官网
  • 全景网站模版创意设计人才网
  • 做一个简单的网站多少钱网站优化网站优化
  • 北京城乡建设部网站首页地址生成器
  • 贵州国高建设工程有限公司 网站深圳建站公司网站
  • 设计公司企业网站山东外贸公司排名
  • 制作网站的步骤有哪些有一个网站 人物模型可以做各种动作
  • 怎么用ftp备份网站网站名称注册程序