QT:没有这样的插槽 [英] QT: No Such Slot

查看:83
本文介绍了QT:没有这样的插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是每次我从主窗口启动设置"窗口时,我都会在 Qt Creator 中不断收到没有这样的插槽"运行时错误.到目前为止,我发现 Qt 非常违反直觉,而且这个 slot 'n 信号的概念似乎与简单地传递变量或函数调用有点牵强.基本上,我有一个带有设置选项的菜单,单击该选项时会打开一个设置窗口,该窗口需要从用户那里获取一个 double 并在主窗口中更新一个 var.

Problem is that I keep getting the 'No Such Slot' runtime error in Qt Creator every time I launch a 'settings' window from my main window. I've found Qt to be quite counter-intuitive so far and this slots 'n signals concept seems a bit of a stretch from simply passing vars or function calls. Basically, I have menu with a settings option, that when clicked, opens a settings window which needs to grab a double from the user and update a var in the main window.

设置窗口.h

class SettingsWindow : public QWidget
{
      Q_OBJECT
  public:
      SettingsWindow(QWidget *parent = 0);
  signals:
      void ValChanged(double newVal);
  public slots:
      void Accept();
  private:
      QLineEdit *le1;
};

设置窗口有一个接受按钮,它调用 Accept(),它发出 ValChanged 信号,其中 newVal 设置为用户在 le1 中输入的 double.

The settings window has an accept button which calls Accept() which emits the ValChanged signal with newVal set as the user input in le1 as a double.

设置窗口.cpp

void SettingsWindow::Accept(){
    emit ValChanged(le1->text().toDouble());
    this->close();
}

这个设置窗口被应用程序的主窗口调用:MainWindow

This settings window is called by the application's main window: MainWindow

MainWindow.cpp

MainWindow.cpp

class MainWindow : public QMainWindow
{
      Q_OBJECT  
  public:
      MainWindow(QWidget *parent = 0);
  public slots:
      void SetVal(double x);
  private slots:
      void NewWindow();
  private:
      double theVal;
};

这个主窗口有一个菜单,可以从中选择设置.这将创建一个新窗口,其中包含一个供用户输入数字的字段.

This main window has a menu which one would select settings from. This creates a new window with a field for one to enter a number.

MainWindow.cpp

MainWindow.cpp

void MainWindow::NewWindow()
{
    SettingsWindow *MySettings=new SettingsWindow(this);
    QObject::connect(MySettings, SIGNAL(ValChanged(double)), this, SLOT(SetVal(double)));
    MySettings->show();
    MySettings->raise();
}

void MainWindow::SetVal(double x){
    theVal = x;
}

我希望当设置窗口打开时,用户可以在字段中输入一个 val,然后发出 ValChanged 信号,将 theVal 设置为用户指定的值.大多数时候我都看到有人不包含 Q_OBJECT 宏的问题,但我两次都包含了它.关于为什么这不起作用的任何建议?

My hope is that when the settings window is opened, the user can enter a val into the field which then emits the ValChanged Signal which sets theVal to the value specified by the user. Most of the time I saw an issue with people not including Q_OBJECT macro, but I've included it both times. Any suggestions on why this doesn't work?

推荐答案

您遇到的问题几乎肯定是由于没有重新创建 moc 文件、连接调用中的拼写错误或插槽声明中的拼写错误有问题.

The issue you are having is almost certainly due to a moc file not being recreated, a typo in your call to connect or a typo in the declaration of the slot in question.

您可能要考虑到,这比从对话框获取输入所需的工作要多得多.一种更简单的方法是将接受"按钮单击信号连接到主窗口中的插槽,然后通过 getXXX() 方法直接从设置窗口的实例中获取您想要的值.

You may want to consider that this is a lot more work than necessary for getting input from a dialog. A simpler method would be to connect the "Accept" button clicked signal to a slot in main window and then fetch the value you want directly from the instance of the settings window through a getXXX() method.

如果你最终有一个包含很多值的设置对话框,而不是通过 getter 获取每个值,让接受"按钮信号返回一个结构,其中所有值作为该结构的字段.

If you eventually have a settings dialog with a lot of values, instead of fetching each value through getters, have the "Accept" button signal return a structure with all the values as fields of that structure.

我应该提到,每次调用 NewWindow() 时,它都会创建一个新的 SettingsWindow 实例.所有这些实例都将持续存在,直到 MainWindow 被销毁.

I should mention that it looks like NewWindow() creates a new instance of SettingsWindow each time it is called. All of these instances will persist until the MainWindow is destroyed.

这篇关于QT:没有这样的插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆