0

此代码创建一个线程并将其连接到MainWindowin QT

void UI_handling::create_thread_for_looping_terminal(){
    loop_terminal_thread = new Loop_terminal_thread(mainWindow, &j1939);
    loop_terminal_thread->start();
    mainWindow->connect(loop_terminal_thread, &Loop_terminal_thread::send_ID_and_data_to_looping_terminal, mainWindow, &MainWindow::update_looping_teriminal);
}

->connect由于mainWindow参数,此代码部分给出错误。

如果我在MainWindow类中运行这段代码,它会是这样的。它会正常工作,但正如你所看到的,mainWindow这就是问题所在。

connect(loop_terminal_thread, &Loop_terminal_thread::send_ID_and_data_to_looping_terminal, this, &MainWindow::update_looping_teriminal);

mainWindow那么,如果我有一个看起来像这样的构造函数,为什么会出现问题

UI_handling::UI_handling(QMainWindow* mainWindow, Ui::MainWindow* ui) : mainWindow(mainWindow), ui(ui){

}

我在类中调用UI_handling构造函数MainWindow

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow){
    ui->setupUi(this);

    /* Important handler */
    ui_handling = new UI_handling(this, ui);

那么为什么这个论点this,导致我没有得到正确的论点呢?

错误是:

ui_handling.cpp:119:17: error: no matching member function for call to 'connect'
qobject.h:242:43: note: candidate function [with Func1 = void (Loop_terminal_thread::*)(unsigned int, unsigned char *), Func2 = void (MainWindow::*)(unsigned int, unsigned char *)] not viable: cannot convert from base class pointer 'QMainWindow *' to derived class pointer 'const typename QtPrivate::FunctionPointer<void (MainWindow::*)(unsigned int, unsigned char *)>::Object *' (aka 'const MainWindow *') for 3rd argument
qobject.h:222:36: note: candidate function not viable: no known conversion from 'void (Loop_terminal_thread::*)(uint32_t, uint8_t *)' to 'const char *' for 2nd argument
qobject.h:225:36: note: candidate function not viable: no known conversion from 'void (Loop_terminal_thread::*)(uint32_t, uint8_t *)' to 'const QMetaMethod' for 2nd argument
qobject.h:481:41: note: candidate function not viable: no known conversion from 'void (Loop_terminal_thread::*)(uint32_t, uint8_t *)' to 'const char *' for 2nd argument
qobject.h:283:13: note: candidate template ignored: requirement '!QtPrivate::FunctionPointer<void (MainWindow::*)(unsigned int, unsigned char *)>::IsPointerToMemberFunction' was not satisfied [with Func1 = void (Loop_terminal_thread::*)(unsigned int, unsigned char *), Func2 = void (MainWindow::*)(unsigned int, unsigned char *)]
qobject.h:322:13: note: candidate template ignored: requirement 'QtPrivate::FunctionPointer<void (MainWindow::*)(unsigned int, unsigned char *)>::ArgumentCount == -1' was not satisfied [with Func1 = void (Loop_terminal_thread::*)(unsigned int, unsigned char *), Func2 = void (MainWindow::*)(unsigned int, unsigned char *)]
qobject.h:274:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
qobject.h:314:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided

最小的例子:

首先你创建线程头

#ifndef LOOP_TERMINAL_THREAD_H

#define LOOP_TERMINAL_THREAD_H

#include <QThread>
#include <QObject>

class Loop_terminal_thread : public QThread {
    Q_OBJECT
public:
    Loop_terminal_thread(QObject *parent = nullptr);
signals:
    void send_ID_and_data_to_looping_terminal(uint32_t ID, uint8_t data[]);
private:
    bool* start_loop_terminal_thread;
    void run();
};

#endif // LOOP_TERMINAL_THREAD_H

然后你创建线程源

#include "loop_terminal_thread.h"

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

}

void Loop_terminal_thread::run(){
    uint8_t data[8] = {0};
    data[1] = 23;
    while(1){
            emit send_ID_and_data_to_looping_terminal(300, data);
        msleep(1000);
    }
}

然后你创建UI_handler

/* QT includes */
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QIntValidator>
#include <QMainWindow>

/* Project includes */
#include "UI_threads/Loop_terminal_thread/loop_terminal_thread.h"

class UI_handling{
public:
    UI_handling(QMainWindow* mainWindow, Ui::MainWindow* ui);
    void create_thread_for_looping_terminal();

private:
    QMainWindow* mainWindow;
    Ui::MainWindow* ui;

    /* Thread for the looping terminal */
    Loop_terminal_thread* loop_terminal_thread;

};

#endif // UI_HANDLING_H

并且你在源文件中实现了 counstructor 和函数。

UI_handling::UI_handling(QMainWindow* mainWindow, Ui::MainWindow* ui) : mainWindow(mainWindow), ui(ui){

}

void UI_handling::create_thread_for_looping_terminal(){
    loop_terminal_thread = new Loop_terminal_thread(mainWindow, &j1939);
    loop_terminal_thread->start();
    mainWindow->connect(loop_terminal_thread, &Loop_terminal_thread::send_ID_and_data_to_looping_terminal, mainWindow, &MainWindow::update_looping_teriminal);
}

然后最后你在你的MainWindow构造函数中实现这个

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow){
    ui->setupUi(this);

    /* Important handler */
    ui_handling = new UI_handling(this, ui);
    ui_handling->create_thread_for_looping_terminal();
}

void MainWindow::update_looping_teriminal(uint32_t ID, uint8_t data[]){

}

类的头文件是MainWindow这样的

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void update_looping_teriminal(uint32_t ID, uint8_t data[]);
4

1 回答 1

1

线索在错误消息的这一部分:

cannot convert from base class pointer 'QMainWindow *'
to derived class pointer ... (aka 'const MainWindow *') for 3rd argument

您正在连接到MainWindow类中的一个插槽,因此您需要提供一个指向MainWindow. 一个指针QMainWindow是不够的。

当您在内部运行 connectMainWindow并 passthis时,此参数的类型是MainWindow并且它可以工作。

为了让您的代码正常工作,您需要更改UI_handling类,以便它存储指向MainWindow而不是QMainWindow.

于 2021-08-25T02:04:40.433 回答