qq_17352261
qq_17352261
2020-04-18 21:18
采纳率: 82.4%
浏览 416

Qt连接槽函数没有效果?

主窗口:QtGuiApplication1

QtGuiApplication1.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"

class QtGuiApplication1 : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
    void on_SaleButton_clicked();
    void Receive(int);


private:
    Ui::QtGuiApplication1Class ui;
};

QtGuiApplication1.cpp

#include "QtGuiApplication1.h"
#include "Sale.h"
#include <qmessagebox.h>
QtGuiApplication1::QtGuiApplication1(QWidget* parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}
void QtGuiApplication1::on_SaleButton_clicked()//跳转到收银模块
{
    auto sale=new Sale();
    connect(sale, SIGNAL(Send(int)), this, SLOT(Receive(int)));//连接槽函数
    sale->show();
}

void QtGuiApplication1::Receive(int a)
{
    QMessageBox messageBox(QMessageBox::Information, "提示","创建了子窗口", QMessageBox::Yes, NULL);
}

子窗口Sale

Sale.h

#pragma once

#include "ui_Sale.h"
#include <QtWidgets/QMainWindow>

class Sale : public QMainWindow
{
    Q_OBJECT

public:
    Sale(QWidget* parent = Q_NULLPTR);
signals:
    void Send(int);
private:
    Ui::Sale ui;
};

Sale.cpp

#include "Sale.h"
Sale::Sale(QWidget* parent ):QMainWindow(parent)
{
    ui.setupUi(this);
    emit Send(1);
}

我的想法是点击主窗体的SaleButton按钮显示子窗口,同时主窗口弹出对话框,但是能显示子窗口,并没有弹出对话框,槽函数是无效的

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • qq_26201309
    非弋 2020-04-27 21:02
    已采纳
    void QtGuiApplication1::on_SaleButton_clicked()//跳转到收银模块
    {
        auto sale=new Sale();   // 在Sale的构造函数中emit Send(1);这时信号槽都还没有绑定
        connect(sale, SIGNAL(Send(int)), this, SLOT(Receive(int)));//连接槽函数
        sale->show();
    }
    
    点赞 评论

相关推荐