【问题标题】:Creating signals and slots qt4 gui builder创建信号和插槽 qt4 gui builder
【发布时间】:2013-12-06 20:59:53
【问题描述】:

您好,我是 C++ 新手,我正试图理解这些概念。 我正在创建一个非常简单的应用程序以在教程的帮助下开始,所以我尝试自己做第一次尝试。 除了 main.cpp 之外,我遇到了 file.h 和 file.cpp 的问题 我想单击按钮框中的按钮“确定”并在文本框中显示文本。 这里首先是 MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_MainWindow.h"

class MainWindow: public QMainWindow, private Ui::MainWindow
{
    Q_OBJECT

public:
    MainWindow(QMainWindow *parent = 0);

private slots:
    //here is where im tyring to add a slot.
    void on_buttonbox_buttonClicked ( QAbstractButton * );
    // void on_inputSpinBox2_valueChanged(int value);
    private:
    Ui::MainWindow ui;
};

#endif

接下来是 MainWindow.cpp

#include <QtGui>
#include "MainWindow.h"

MainWindow::MainWindow(QMainWindow *parent)
     : QMainWindow(parent)
{
    ui.setupUi(this);
}

//This is where i would like to catch the clicked signal from the ok button and add the text to the text box.
void MainWindow::on_buttonbox_buttonClicked ( QAbstractButton * ){
    ui.textEdit->setText(QString::number(16));
}

我试图尽可能简单地让它运行,它会编译但我无法让信号和插槽说话,我哪里出错了......记住全新的。

【问题讨论】:

  • 我不知道该放在哪里
  • connect(...) 放入构造函数的实现(!)中(在您的cpp 文件中,在ui.setupUi(this); 行之后)。一会儿我会把它编辑成我的答案......

标签: c++ qt4 qt4.7


【解决方案1】:

您必须将您的插槽连接到信号,将其添加到您的构造函数中:

this->connect(this->ui.yourButton, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton*))); 
//                         ^
//                         |
//             The name of your Button here ...

也请看这里:http://qt-project.org/doc/qt-4.8/signalsandslots.html


编辑:

MainWindow.cpp

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
    ui.setupUi(this);

    // Connect Signals-Slots
    this->connect(this->ui.yourButton, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton*))); 
}

// ...

但不要忘记将 yourButton 更改为您命名的任何名称。

【讨论】:

    【解决方案2】:

    好的,您必须将信号连接到某处的插槽。你应该在构造函数上这样做,

    connect(button, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton *)));

    请记住,只有当有信号连接到插槽时才会调用插槽。否则你的按钮将无法知道去哪里。

    【讨论】:

    • 当我尝试你的 sujestion 时,我一直收到这个错误,,对不起,真的在这里绊倒了,真的很困惑
    • 错误“MainWindow.cpp:10: error: expected constructor, destructor, or type conversion before ‘(’ token make: *** [MainWindow.o] Error 1”
    • 你必须在标题中包含 QMainWindow。
    • 并且还声明并实现了一个析构函数。
    【解决方案3】:

    SIGNAL 和 SLOT 的机制非常简单,用于将小部件(按钮、旋转框等)注册到事件。例如“当我单击该按钮时,将显示一个新窗口。” 话虽如此,让我们看看如何将我们的 SLOT(收到信号后做什么)注册到他的 SIGNAL(一个事件:点击、选择、编辑表单等)

    QObject::connect( button , SIGNAL( click()), this , SLOT( openWindow() ))
    

    按钮是会发出信号的小部件。

    SIGNAL(click())你是说点击(按下并释放)按钮会执行一个动作

    this是声明槽的对象

    SLOT( openWindow() ) 是点击按钮

    时会调用的方法(slot)

    信号和槽必须有相同的参数!! 因此,要回答您的问题,您必须声明一个与信号具有相同参数的插槽。 click() 没有参数,因此您必须将您的插槽声明为:

        void on_buttonbox_buttonClicked ()
    

    PS:我记得有一个问题,命名带有前缀 on 的插槽。但是我必须做一点搜索,因为我记不太清了。

    更新:我做了一个小测试,使用前缀 on_ 命名插槽会在运行时出现错误消息QMetaObject::connectSlotsByName: No matching signal for on_ClickChangeBack(),但插槽会执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多