V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
h3xz
V2EX  ›  Qt

关于 QT 中通过信号与槽的方式启动子线程的问题。

  •  
  •   h3xz · 21 天前 · 645 次点击

    我自定义了一个多线程类TimerThread:

       TimerThread* t_thread = new TimerThread();
       QThread* thread = new QThread();
       t_thread->moveToThread(thread);
    

    我想通过界面上的一个按钮ui->startThread来启动子线程:

       connect(ui->startThread, &QPushButton::clicked, thread, &QThread::start);
    

    但是程序报错:error: static assertion failed: Signal and slot arguments are not compatible. 但是我使用了另一种方法连接信号与槽,程序又能正常运行:

       connect(ui->startThread, &QPushButton::clicked, thread, [&](){
    	thread->start();
       });
    

    报错的意思是发送的信号参数与槽函数的参数没有对应。但是当我自己定义一个槽函数void testFun(int); ,并且,点击按钮时发出的信号所携带的参数同样与我自定义的槽函数的参数不能对应,但是程序却能正确运行。

       connect(ui->startThread, &QPushButton::clicked, this, &MainWindow::testFun);
    

    我非常困惑,大佬能否为我指点迷津,非常感谢🙏🙏🙏

    2 条回复    2024-08-26 16:45:09 +08:00
    NoOneNoBody
        1
    NoOneNoBody  
       21 天前   ❤️ 1
    qpushbutton clicked 发送一个 checked 的 bool 信号,默认 false ,button 可以 setcheckable
    qthread start 参数是 0~7 的 int 信号,默认 7 ,QThread::Priority
    看看是不是这个原因,我也不了解 QT C++,只写过 pyqt
    frayesshi1
        2
    frayesshi1  
       21 天前
    qthread::start 是有参数的,你的第三种方式是在同一个线程里面,可以不用 emit 参数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   872 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 20:32 · PVG 04:32 · LAX 13:32 · JFK 16:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.