void Prog::MySLot(int val){ emit OtherSignal(val); //do something}
void Player::SetVolume2(){ int volume = volume_int; if(volume>0) { Muted=false; } emit UpdateSettingsParamSignal(QString("volumelevel"),QString().setNum(volume)); ui->muteButton->setChecked(false); ui->VolumeSlider->setValue(volume); float vol=(float)((float)volume/(float)100); BASS_ChannelSetAttribute(chan, BASS_ATTRIB_VOL,vol);}
void MainWindow::Do(){ QThread *tdh = new QThread; connect(tdh,SIGNAL(started()),this,SLOT(Do2())); tdh->start();}
C++ (Qt)class Worker : public QObject{Q_OBJECT Q_SIGNALS: void finished(const QString &result); public Q_SLOTS: void do (const QString &in) { // some work emit finished(result); }}; ...................... MainWindow::MainWindow(){ QThread *thread = new QThread; worker = new Worker; worker->moveToThread(thread); thread->start();} .............. QMetaObject::invokeMethod(worker, "do", Q_ARG(QString, in));
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QThread>#include <QDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(Do()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::Do(){ QThread *tdh = new QThread; connect(tdh,SIGNAL(started()),this,SLOT(Do2())); tdh->start();}void MainWindow::Do2(){ QThread::sleep(2); ui->plainTextEdit->document()->setPlainText(QDateTime::currentDateTime().toString());}