Qt中的函数匹配

发布于 2024-08-27 04:45:07 字数 1015 浏览 15 评论 0原文

我在使用 Qt 时遇到了一些麻烦。

我在命名空间 Ui 中有一个类“Core”

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(Ui::MainWindow const *w);

   private:
        static MainWindow *main_window;
};

和类“MainWindow”:

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

在 MainWindow 构造函数中,我犯了错误

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

并得到错误

mainwindow.cpp:8: error: no matching function for call to 'Core::setMainwindow(MainWindow* const)'

当然,我包含 core.h 和“Core”类的声明。

这种情况仅发生在 setMainwindow 方法上。

所以问题是 - 为什么 Core 类方法 setMainwindow() 在 MainWindow 类中不可见?

I have some trouble with Qt.

I have a class 'Core'

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(Ui::MainWindow const *w);

   private:
        static MainWindow *main_window;
};

and class 'MainWindow' in namespace Ui:

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

In MainWindow constructor I make

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

and gets error

mainwindow.cpp:8: error: no matching function for call to 'Core::setMainwindow(MainWindow* const)'

Of cource, i include core.h with declaration of 'Core' class.

That's occurs only on setMainwindow method.

So the questions is - why Core class method setMainwindow() is invisible in MainWindow class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油焖大侠 2024-09-03 04:45:07

问题是您的 Core::setMainwindow 方法采用 Ui::MainWindow* 并且您正在传递 MainWindow*。从您发布的代码中,您有两个 MainWindow 类,一个位于命名空间 Ui 中,一个位于顶级命名空间中。这是你的意思吗?或者也许只有 Ui 中有一个?

The problem is that your Core::setMainwindow method takes a Ui::MainWindow* and you are passing a MainWindow*. From the code you posted you have two MainWindow classes, one in the namespace Ui and one in the top-level namespace. Is this what you mean or should there only be the one in Ui perhaps?

∞觅青森が 2024-09-03 04:45:07

您是否在 MainWindow 的 cpp/h 文件中添加了“Core.h”?

您是否尝试在 setMainWindow 中不带参数,只是为了检查它是否与它无关?

编辑:是的,在我看来,您需要 MainWindow 作为参数,而不是 Ui::MainWindow,您不觉得吗?

Did you add "Core.h" in your MainWindow's cpp/h file ?

Did you try without parameter in setMainWindow, just to check if it's not something related to it ?

Edit : Yeah seems to me you need MainWindow as parameter, not Ui::MainWindow, don't you think ?

倾城°AllureLove 2024-09-03 04:45:07

您的 MainWindow 类未嵌套在 Ui 命名空间内。您向前声明了 Ui::MainWindow 类,但随后实现了一个单独的 ::MainWindow 类(在全局命名空间中)。因此,您的 Core::setMainwindow 接受 Ui::MainWindow 但您传递的是 ::MainWindow

我猜这种缺乏嵌套的情况是正确的 - 并且 Ui::MainWindow 是由 Qt Designer 生成的,而 MainWindow 是实现包含所有自定义代码的类。如果是这样,请将您的代码更改为:

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(MainWindow const *w);

   private:
        static MainWindow *main_window;
};

Your MainWindow class isn't nested inside the Ui namespace. You forward-declared the Ui::MainWindow class, but then implemented a separate ::MainWindow class (in the global namespace). Because of this, your Core::setMainwindow takes a Ui::MainWindow but you're passing a ::MainWindow.

I'm guessing that this lack of nesting is correct -- and Ui::MainWindow is generated by Qt Designer, and MainWindow is the implementation class that contains all of your custom code. If so, change your code to:

class Core {

   public:

        static QString get_file_content(QString filename);
        static void setMainwindow(MainWindow const *w);

   private:
        static MainWindow *main_window;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文