void myWid::on_myButton_clicked(){int i = 0;char R[5] = {0};struct termios tp1;struct termios tp2;tcgetattr( 0, &tp1);tp2 = tp1; tp2.c_iflag&=~ICRNL; tp2.c_lflag&=~ICANON; tp2.c_lflag&=~ECHO; tp2.c_cc[VMIN ]=1; tp2.c_cc[VTIME]=0; tp2.c_cc[VINTR]=0xFF; tp2.c_cc[VSUSP]=0xFF; tp2.c_cc[VQUIT]=0xFF;tcsetattr( 0, TCSANOW, &tp2);fd_set set0, set;FD_ZERO( &set);FD_SET( fp, &set);FD_SET( 0, &set);set0 = set;while(1) { if ( select( fp+1, &set, NULL, NULL, NULL ) > 0) { if( FD_ISSET( fp, &set)) { i = read( fp, R, 1); ui->myLcd->display(int( R[0] )); //break; } if( FD_ISSET( 0, &set) ) { i = read( 0, R, 1); if( R[0] == 27){ break;} } } set = set0; }tcflush( fp, TCIOFLUSH);tcsetattr( 0, TCSANOW, &tp1);}
C++ (Qt)signals: void update_signal(int);
C++ (Qt)while ( true ) { ... emit update_signal(int( R[0] ));}
C++ (Qt)mythread = new MyThread(this);connect(mythread, SIGNAL(update_signal(int)), ui->myLcd, SLOT(display(int)));mythread->start();
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>class MyThread : public QThread { public: void run(); signals: void update_signal(int); };#endif // MYTHREAD_H
#include "mythread.h"void MyThread::run() { int i = 0; while( i < 100) { i++; msleep(10); emit update_signal( i ); } }
4 myWid::myWid(QWidget *parent)5 : QWidget(parent), ui(new Ui::myWidClass)6 {7 ui->setupUi(this);8 mythread = new MyThread(this);9 connect(mythread, SIGNAL(update_signal(int)), ui->myNum, SLOT(display(int)));10 mythread->start();11 }
C++ (Qt)class MyThread : public QThread { Q_OBJECT public: MyThread(QObject* o):QThread(o) { /** other initialization's are needed put here */ } void run(); signals: void update_signal(int); };
C++ (Qt)MyThread* mythread;
C++ (Qt)mythread = new MyThread(parent);
connect(mySerialDevice, SIGNAL(readyRead()), myLCD, SLOT(updateLCD()));
void myLCD::updateLCD() { QByteArray ba = mySerialDevice->read(1); myLCD->data = (тут преобразование/приведение типа) ba; }
C++ (Qt)void MyMainWindow::longStandingCalculation(void) { for(int i=0; i<1024*1024*1024; i++) { if(i%1000 == 0) { updateValue(i); } }} void MyMainWindow::updateValue(int value) { static QTime CurTime = QTime::currentTime(); if(CurTime.msecsTo(QTime::currentTime()) >= 100) { CurTime = QTime::currentTime(); ui.label->setText(QString::number(value)); QApplication::processEvents(); }}
1 #include "mythread.h"2 3 void MyThread::run()4 {5 int i = 0;6 while( i < 100)7 {8 i++;9 msleep(10);10 emit update_signal( i );11 }12 }
TARGET = ziklTEMPLATE = appSOURCES += main.cpp \ myWid.cpp \ mythread.cppHEADERS += myWid.h \ mythread.hFORMS += myWid.ui
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>class MyThread : public QThread { Q_OBJECT public: MyThread(QObject* o):QThread(o) { } void run(); signals: void update_signal(int); };#endif // MYTHREAD_H