简体   繁体   English

创建信号和插槽QT4 GUI构建器

[英]Creating signals and slots qt4 gui builder

Hi im brand new to c++ and trying to get my head around the concepts. 您好,我是C ++的新手,并试图让我了解这些概念。 I am creating a very simple app to get going with the help of the tutorials, so im trying to do my own first try. 我正在创建一个非常简单的应用程序,以借助教程进行操作,因此,我尝试自己尝试一下。 I'm having problems with the file.h and file.cpp the one besides main.cpp I would like to click the button in the button box "ok" and have text come up in the text box. 我在使用main.cpp之外的file.h和file.cpp时遇到问题,我想单击按钮框中的按钮“确定”,并在文本框中显示文本。 Here is MainWindow.h first 这是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

Next is the MainWindow.cpp 接下来是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));
}

Im trying to be as simple as posible to just to get it going, it will compile but i can't get the signals and slots to talk, where am i going wrong.... remember brand new. 我试图尽可能简单地做到这一点,它将进行编译,但我无法获得进行通话的信号和插槽,我在哪里错了...。记住全新的内容。

You have to connect your Slots to the Signals, add this into your constructor: 您必须将插槽连接到信号,然后将其添加到构造函数中:

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

Also please see here: http://qt-project.org/doc/qt-4.8/signalsandslots.html 还请参见此处: http : //qt-project.org/doc/qt-4.8/signalsandslots.html


Edit: 编辑:

MainWindow.cpp 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*))); 
}

// ...

But don't forget to change yourButton to whatever you've named yours. 但是不要忘记将yourButton更改为您命名的任何yourButton

Alright, you have to connect the signal to the slot somewhere. 好了,您必须将信号连接到某个位置的插槽。 You should do this on the constructor, 您应该在构造函数上执行此操作

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

Just remember that the slot only will be called if there is a signal connect to it. 请记住,仅当有信号连接到插槽时才会调用该插槽。 Otherwise your button won´t be able to know where to go. 否则,您的按钮将不知道要去哪里。

The mechanism of SIGNAL and SLOT is very simple and is used to register a widget(buttons, spinbox etc..) to an event. SIGNAL和SLOT的机制非常简单,用于向事件注册小部件(按钮,旋转框等)。 For example "when I click that button a new window will show up." 例如,“当我单击该按钮时,将显示一个新窗口。” That being said, let's see how can we register our SLOT(what to do after receiving the signal) to his SIGNAL(an event: a click, a selection, an edit of a form etc..) 话虽这么说,让我们看看如何将SLOT(收到信号后的操作)注册到他的SIGNAL(事件:单击,选择,编辑表单等。)

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

button is the widget that will throw the signal. button是将引发信号的小部件。

SIGNAL( click()) you are telling that clicking(press and release) button an action will be performed SIGNAL(click())告诉您单击(按下并释放) 按钮将执行某项操作

this is the object that declare the slot 是声明插槽的对象

SLOT( openWindow() ) is the method(slot) will be called clicking button SLOT(openWindow())是方法(插槽),将称为单击按钮

Signals and slots must have same parameters!! 信号和插槽必须具有相同的参数!! So to answer your question you have to declare a slot with same parameters as the signal. 因此,要回答您的问题,您必须声明一个与信号具有相同参数的插槽。 click() has no parameter so you have to declare your slot as: click()没有参数,因此您必须将广告位声明为:

    void on_buttonbox_buttonClicked ()

PS: as I remember there is an issue naming a slot with the prefix on . PS:我记得有一个问题命名前缀插槽。 But I have to do a little search because I don't remember very well. 但是我必须做一点搜索,因为我不太记得。

update: I made a little test and naming the slot with the prefix on_ gives an error message at run time QMetaObject::connectSlotsByName: No matching signal for on_ClickChangeBack() but the slot execute. 更新:我进行了一些测试,并使用前缀on_命名插槽,在运行时给出了错误消息QMetaObject::connectSlotsByName: No matching signal for on_ClickChangeBack()但插槽执行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM