Qt:在 2 个不同的窗口中将 SIGNAL 连接到 SLOT

发布于 2025-01-02 19:11:14 字数 1156 浏览 2 评论 0原文

我要发疯了,试图找到这里的问题。我有一个主窗口和一个用 Qt 制作的表单类型窗口。我正在使用 Visual Studio 2010 插件。由于某种原因,我的 SLOT 从未在主窗口中调用;然而,信号似乎已发出。

这是我所做的:

这是形式:

class ScalerValuesWindow : public QWidget
{
    Q_OBJECT

private:
    Ui::ScalerValuesWindow ui;

    // Variables
    std::vector<int> scalerValues;

public slots:
    void storeScalerValues();

signals:
    void ScalerValues(std::vector<int>);

public:
    ScalerValuesWindow(QWidget *parent = 0);

};

void ScalerValuesWindow::storeScalerValues()
{
    emit ScalerValues(scalerValues);
    hide();
}

然后这是我的构造函数中的主窗口连接行:

scalerValuesWindow = new ScalerValuesWindow;
connect(scalerValuesWindow, SIGNAL(ScalerValues(std::vector<int>)), this, SLOT(RetrieveScalerValues(std::vector<int>)));

这是在我的主窗口的类声明中:

public slots:
    void RetrieveScalerValues(vector<int> scalerValues);

这是槽:

void RelayduinoGuiThreading::RetrieveScalerValues(vector<int> scalerVals)
{
    scalerValues = scalerVals;
}

我在两者中都声明了 Q_OBJECT。我不知道是什么原因造成的。

任何建议将不胜感激。

I'm going nuts trying to find the problem here. I have a main window, and a form type window made in Qt. I'm using the Visual Studio 2010 addon. For some reason, my SLOT is never called in the main window; however, the signal appears to be emitted.

Here's what I've done:

This is the form:

class ScalerValuesWindow : public QWidget
{
    Q_OBJECT

private:
    Ui::ScalerValuesWindow ui;

    // Variables
    std::vector<int> scalerValues;

public slots:
    void storeScalerValues();

signals:
    void ScalerValues(std::vector<int>);

public:
    ScalerValuesWindow(QWidget *parent = 0);

};

void ScalerValuesWindow::storeScalerValues()
{
    emit ScalerValues(scalerValues);
    hide();
}

Then here's my main window connection line in my constructor:

scalerValuesWindow = new ScalerValuesWindow;
connect(scalerValuesWindow, SIGNAL(ScalerValues(std::vector<int>)), this, SLOT(RetrieveScalerValues(std::vector<int>)));

This is in my main window's class declaration:

public slots:
    void RetrieveScalerValues(vector<int> scalerValues);

And this is the slot:

void RelayduinoGuiThreading::RetrieveScalerValues(vector<int> scalerVals)
{
    scalerValues = scalerVals;
}

I have Q_OBJECT declared in both. I have no idea what could be causing this.

Any advice would be greatly appreciated.

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

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

发布评论

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

评论(1

煞人兵器 2025-01-09 19:11:14

您必须将插槽定义为:(

public slots:
  void RetrieveScalerValues(std::vector<int> scalerValues);
                            ^^^

检查生成的 moc 文件以准确查看正在生成的信号/插槽签名。)

请勿在您的标头,无论如何,这都是不好的做法(将巨大的名称空间拉入该标头的所有用户中,这是不礼貌的)。

You must define your slot as:

public slots:
  void RetrieveScalerValues(std::vector<int> scalerValues);
                            ^^^

(Inspect the generated moc file to see exactly what signal/slot signatures are being generated.)

Don't use using namespace std; in your headers, that's bad practice anyway (pulls in that huge namespace into all the users of that header, which is impolite).

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