是否有可能让Qt Creators UI设计师为它无法识别的类创建插槽?

Is it possible to get Qt Creators Ui designer to create slots for a class that it doesn't recognize?

本文关键字:识别 创建 插槽 Qt 有可能 Creators UI 设计师 是否      更新时间:2023-10-16

也就是说,通过右键单击小部件并选择"转到插槽..."来创建插槽。

示例 1,这有效:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

这不会,因为显然设计器会寻找 Ui::MainWindow*,如果找不到它,就会完全吓坏了(得到"找不到包含 'Ui::MainWindow' 的类......")

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <memory>
#include <QMainWindow>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    std::unique_ptr<Ui::MainWindow> ui;
};
#endif // MAINWINDOW_H

基本上是在寻找一种方法来告诉Qt"是的,我真的是那个类。我知道它不包含 Ui::MainWindow*,没关系,一切都会好起来的......"。除非我错了,并且由于某种原因,类实际上需要将其作为普通指针。

"找不到包含'Ui::MainWindow'的类..."当 QT 创建者找不到您的"主窗口.cpp"文件来放置插槽函数定义时,会发生问题。

确保您的 mainwindow.h 具有以下内容作为私有变量:

Ui::MainWindow *ui;

以下包含位于您的主窗口中.cpp文件中:

#include "ui_mainwindow.h"

如果这不起作用,则您的 QT 创建者可能存在错误。尝试更新您的软件。