简体   繁体   English

连接信号和插槽不起作用Qt

[英]Connecting signal and slot not working Qt

I mostly copied, pasted the code from Here , and implemented them in a small new program like this: 我主要是从Here复制,粘贴代码,并在一个小的新程序中实现它们,如下所示:
In mybutton.h : mybutton.h

class MyButton : public QPushButton
{
 Q_OBJECT

public:
    MyButton(QWidget *parent = Q_NULLPTR);

    QVector<MyButton*> buttons;

private slots:
    void mousePressEvent(QMouseEvent *e) {
        if(e->button()==Qt::RightButton) {
            emit btnRightClicked();
            qDebug() << "Emitted";
        }
    }

signals:
    void btnRightClicked();
};

And in mainwindow.cpp : mainwindow.cpp

MyButton mButtons;

QWidget *mWidget = new QWidget(this);
QGridLayout *gLayout = new QGridLayout(mWidget);

mButtons.buttons.resize(5);
for(int i = 0; i < 5; i++) {
    mButtons.buttons[i] = new MyButton(mWidget);
    gLayout->addWidget(mButtons.buttons[i], 0, i);
}

mWidget->setLayout(gLayout);
setCentralWidget(mWidget);

connect(&mButtons, SIGNAL(btnRightClicked()), this, SLOT(onRightClicked()));

And the onRightClicked slot is like this: 并且onRightClicked插槽是这样的:

void MainWindow::onRightClicked() 
{
    qDebug() << "clicked";
}

But the debug out come only has this: Emitted . 但是调试出来只有这样: Emitted
I do not know where is wrong here. 我不知道这里哪里错了。 So how can I solve the problem? 那我该如何解决这个问题呢?

Thanks. 谢谢。

It's just what I was thinking, you have created a called mButtons, and that you have connected to your signal, but that button has no parent is not visualized since it is deleted when you finish executing the constructor, that does not mean that the pointers that you save in QVector are deleted from memory but they subsist and emit the signals, but these are not connected to any slot. 正是我在想的是,您创建了一个名为mButton的控件,并且已连接到信号,但是该按钮没有父级是不可见的,因为在完成执行构造函数后会删除该父级,这并不意味着指针您保存在QVector中的信号将从内存中删除,但它们仍然存在并发出信号,但这些信号未连接到任何插槽。

What you have to do is create a button that only emits the signal: 您要做的是创建一个仅发出信号的按钮:

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QMouseEvent>
#include <QPushButton>
#include <QDebug>

class MyButton : public QPushButton
{
 Q_OBJECT

public:
    MyButton(QWidget *parent = Q_NULLPTR):QPushButton(parent){

    }

protected:
    void mousePressEvent(QMouseEvent *e) {
        if(e->button()==Qt::RightButton) {
            emit btnRightClicked();
            qDebug() << "Emitted";
        }
    }

signals:
    void btnRightClicked();
};
#endif // MYBUTTON_H

Then you create a container of buttons, and in the loop you create the buttons and connect it: 然后创建一个按钮容器,并在循环中创建按钮并将其连接:

*.h *。H

private slots:
    void onRightClicked();

private:
    QVector<MyButton *> mButtons;
};

*.cpp *的.cpp

QWidget *mWidget = new QWidget(this);
QGridLayout *gLayout = new QGridLayout(mWidget);
for(int i = 0; i < 5; i++) {
    MyButton *btn = new MyButton(mWidget);
    gLayout->addWidget(btn, 0, i);
    connect(btn, &MyButton::btnRightClicked, this, &MainWindow::onRightClicked);
    mButtons << btn;
}

mWidget->setLayout(gLayout);
setCentralWidget(mWidget);

You can download the example in the following link 您可以在以下链接中下载示例

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

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