如何重新绘制另一个Qt类

如何重新绘制另一个Qt类,qt,class,repaint,Qt,Class,Repaint,我是Qt的新成员 我有一个QtGUI应用程序(由我编写),我们称它为QtAPP.exe 当QtAPP.exe运行时,我将使用QThread和QProcess执行一些外部文件, 例如player.exe(用本机C编写) 我的问题是: 在QtAPP.exe中,有两个类, 1.QMainWindow-QtAPP.exe的核心 2.QThread-执行外部事物的线程类 现在,如果我在QThread中得到一个finished()信号, 如何强制QMainWindow重新绘制自身 希望有人能给我一些提示,也

我是Qt的新成员

我有一个QtGUI应用程序(由我编写),我们称它为QtAPP.exe 当QtAPP.exe运行时,我将使用QThread和QProcess执行一些外部文件, 例如player.exe(用本机C编写)

我的问题是: 在QtAPP.exe中,有两个类, 1.QMainWindow-QtAPP.exe的核心 2.QThread-执行外部事物的线程类

现在,如果我在QThread中得到一个finished()信号, 如何强制QMainWindow重新绘制自身

希望有人能给我一些提示,也许是示例代码:)
欢迎任何建议~

一种解决方案是将finished()信号连接到MainWindow中的一个插槽,该插槽的实现调用update()。请注意,此信号的传递将是异步的,因为发送方和接收方对象位于不同的线程中

以下是一个工作示例:

main.cpp

#include <QtGui/QApplication>
#include "stuff.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}
#include <QtCore/QTimer>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include "stuff.h"

#include <QDebug>

// Global variables used for ITC
QWaitCondition buttonPressed;
QMutex mutex;

Thread::Thread(QObject *parent)
    :   QThread(parent)
{

}

void Thread::run()
{
    qDebug() << "Thread::run" << QThread::currentThreadId();
    while (1) {
        mutex.lock();
        buttonPressed.wait(&mutex);
        mutex.unlock();
        startWork();
    }
}

void Thread::startWork()
{
    qDebug() << "Thread::startWork" << QThread::currentThreadId();
    // Simulate some long-running task
    sleep(3);
    // Emit a signal, which will be received in the main thread
    emit workFinished();
}


MainWindow::MainWindow()
    :   m_label(new QLabel(this))
    ,   m_thread(new Thread(this))
{
    QPushButton *button = new QPushButton("Start", this);
    connect(button, SIGNAL(pressed()), this, SLOT(startWork()));

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(button);
    layout->addWidget(m_label);
    setLayout(layout);

    // Create connection across thread boundary
    connect(m_thread, SIGNAL(workFinished()), this, SLOT(workFinished()));

    m_thread->start();
}

void MainWindow::startWork()
{
    // Signal the thread to tell it that the button has been pressed
    mutex.lock();
    m_label->setText("Started");
    buttonPressed.wakeAll();
    mutex.unlock();
}

void MainWindow::workFinished()
{
    qDebug() << "MainWindow::workFinished" << QThread::currentThreadId();
    m_label->setText("Finished");
}
#包括
#包括“stuff.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
主窗口w;
w、 show();
返回app.exec();
}
stuff.h

#ifndef STUFF_H
#define STUFF_H

#include <QtGui/QMainWindow>
#include <QtCore/QThread>

class QLabel;

class Thread : public QThread
{
    Q_OBJECT
public:
    Thread(QObject *parent);
    void run();

private:
    void startWork();

signals:
    void workFinished();
};

class MainWindow : public QWidget
{
    Q_OBJECT
public:
    MainWindow();

public slots:
    void startWork();
    void workFinished();

private:
    QLabel* m_label;
    Thread* m_thread;
};

#endif
#如果没有东西#
#定义东西
#包括
#包括
类QLabel;
类线程:公共QThread
{
Q_对象
公众:
线程(QObject*父线程);
无效运行();
私人:
void startWork();
信号:
作废已完成的工作();
};
类主窗口:公共QWidget
{
Q_对象
公众:
主窗口();
公众时段:
void startWork();
作废已完成的工作();
私人:
QLabel*m_标签;
螺纹*m_螺纹;
};
#恩迪夫
stuff.cpp

#include <QtGui/QApplication>
#include "stuff.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}
#include <QtCore/QTimer>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include "stuff.h"

#include <QDebug>

// Global variables used for ITC
QWaitCondition buttonPressed;
QMutex mutex;

Thread::Thread(QObject *parent)
    :   QThread(parent)
{

}

void Thread::run()
{
    qDebug() << "Thread::run" << QThread::currentThreadId();
    while (1) {
        mutex.lock();
        buttonPressed.wait(&mutex);
        mutex.unlock();
        startWork();
    }
}

void Thread::startWork()
{
    qDebug() << "Thread::startWork" << QThread::currentThreadId();
    // Simulate some long-running task
    sleep(3);
    // Emit a signal, which will be received in the main thread
    emit workFinished();
}


MainWindow::MainWindow()
    :   m_label(new QLabel(this))
    ,   m_thread(new Thread(this))
{
    QPushButton *button = new QPushButton("Start", this);
    connect(button, SIGNAL(pressed()), this, SLOT(startWork()));

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(button);
    layout->addWidget(m_label);
    setLayout(layout);

    // Create connection across thread boundary
    connect(m_thread, SIGNAL(workFinished()), this, SLOT(workFinished()));

    m_thread->start();
}

void MainWindow::startWork()
{
    // Signal the thread to tell it that the button has been pressed
    mutex.lock();
    m_label->setText("Started");
    buttonPressed.wakeAll();
    mutex.unlock();
}

void MainWindow::workFinished()
{
    qDebug() << "MainWindow::workFinished" << QThread::currentThreadId();
    m_label->setText("Finished");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“stuff.h”
#包括
//用于ITC的全局变量
QWaitCondition按钮按下;
QMutex互斥;
线程::线程(QObject*父对象)
:QThread(父线程)
{
}
void Thread::run()
{
qDebug()setText(“已启动”);
按下按钮。wakeAll();
mutex.unlock();
}
void主窗口::workFinished()
{

qDebug()这里是main int main(int argc,char*argv[]){QApplication a(argc,argv);MainWindow w;w.show();return a.exec();}ManiWindow void MainWindow::exext(){pT.setupExt(一些参数));//Initial QThread pT.start();//start QThread}QThread void Exthread::setup(一些参数)){//Do some init}void Exthread::run(){QProcess*extP;extP->execute(“./player.exe参数)”;QObject::connect(extP,SIGNAL(finished(int)),XXXX,SLOT(yyyy));//仍然不知道如何执行}请检查我上面的示例代码,因为不允许在1个注释中使用太多的单词…XXXX是什么,我如何处理MainWindow类的实例?或者我的想法完全不正确?感谢您的建议,但我仍然不理解如何将信号连接到其他类中的插槽@@“你们能再给我一些提示吗?谢谢,我确实捕捉到了完成的()信号并传递给了其他类,但未能重新绘制:((这是另一个故事…)仍然感谢你们帮助我解决问题:)