zoukankan      html  css  js  c++  java
  • Qt598x64vs2017.跨线程传递std::string

    1、Qt编译的时候 提示 str::string 需要在main(...)中注册什么的(大概是这个意思,记不清了),于是 在main(...)中调用了 “qRegisterMetaType<std::string>("std::string");

      提示 “str::string&”需要注册的话,是这样注册的:“qRegisterMetaType<std::string>("std::string&");

    2、测试代码:

     2.1、MainWindow.h

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pbtnThread_clicked();
        void slot01(std::string str);
    };
    
    class TthreadZ :public QThread
    {
        Q_OBJECT
    public:
        explicit TthreadZ(QObject *parent = nullptr){ }
        ~TthreadZ(){}
    
    protected:
        void run();
    
    signals:
        void signal01(std::string str);
    
    };

     2.2、MainWindow.cpp

    void MainWindow::slot01(std::string str)
    {
        DWORD dwThreadId = ::GetCurrentThreadId();
    
        qDebug() << "MainWindow.dwThreadId : " << dwThreadId << ", " << str.c_str();
    }
    
    void MainWindow::on_pbtnThread_clicked()
    {
        TthreadZ* p = new TthreadZ();
    
        connect(p, &TthreadZ::signal01, this, &MainWindow::slot01);// ZC: 注意这里的参数 函数指针
    
        p->start();
    }
    
    
    
    void TthreadZ::run()
    {
        while (1)
        {
            DWORD dwThreadId = ::GetCurrentThreadId();
    
    
            DWORD dw1 = ::GetTickCount();
            std::string str = std::to_string(dw1);
            emit signal01(str);// <-- <-- <-- <-- <--
            DWORD dw2 = ::GetTickCount();
    
            qDebug() << "TthreadZ.dwThreadId : " << dwThreadId;// << ". Take time : " << dw1 << " --> " << dw2 << " : " << (dw2-dw1)<<"ms";
    
    //        if (g_pMainWindow != nullptr)
    //            g_pMainWindow->UpdateCnt();
    
            Sleep(1000);
            //qDebug() << "";
        }
    }

     2.3、main.cpp

    #include <string>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        qRegisterMetaType<std::string>("std::string");// <-- <-- <-- <-- <--
    
        QApplication a(argc, argv);
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }

    3、

    4、

    5、

  • 相关阅读:
    Ubuntu无法初始化软件包信息
    数组名做函数的参数
    Printf函数中%p代表什么数据类型
    assert()函数用法总结
    C语言printf 格式 zz
    零值指针指向何处? zz~
    命名空间“System.Net”中不存在类型或命名空间名称“Sockets”。是否缺少程序集引用?
    AD20配置为中文显示
    sharepoint:实现搜索功能
    sharepoint:爬网完成后上传新文档搜索不到(设定爬网Schedule)
  • 原文地址:https://www.cnblogs.com/cppskill/p/11889963.html
Copyright © 2011-2022 走看看