簡體   English   中英

如何在Qt中從子窗口小部件向父窗口發送消息?

[英]How to send message from child widget to parent window in Qt?

如何在qt中將消息從子小部件發送到父窗口? 我嘗試在 qt 中從子小部件向父窗口發送信號。 當我在 subwidget.cpp 中調用函數 test 時,信號被發送但主窗口插槽不執行。 如何發送消息?

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QPushButton>
#include <QProcess>
#include <QDebug>
#include <subwidget.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
   Q_OBJECT

   public:
       MainWindow(QWidget *parent = nullptr);
       ~MainWindow();
       void check_adb_exists();

   private:
       Ui::MainWindow *ui;
       QStackedWidget *stack;
       QPushButton *start_btn;
       SubWidget *f2;

   private slots:
       void StartApplication();
       void ReceiveCustomMessage(const QString &msg);

};
#endif // MAINWINDOW_H

子組件.h

#define SUBWIDGET_H

#include <QWidget>

namespace Ui {
class SubWidget;
}

class SubWidget : public QWidget
{
    Q_OBJECT

public:
    explicit SubWidget(QWidget *parent);
    ~SubWidget();
    void test();

private:
    Ui::SubWidget *ui;

signals:
    void SendCustomMessage(const QString& msg);
};

#endif // SUBWIDGET_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
// #include <formcustomnew.h>
#include <subwidget.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    stack = MainWindow::findChild<QStackedWidget *>("stackedWidget");
    start_btn = MainWindow::findChild<QPushButton *>("start_btn");
    stack->setCurrentIndex(0);
    connect(start_btn,SIGNAL(released()),this,SLOT(StartApplication()));
    f2 = new SubWidget(this);
    //connect(f2,SIGNAL(SendCustomMessage(QString)),this,SLOT(ReceiveCustomMessage(QString)));
    //f2->test();
    //auto f1 = new FormCustomNew(this);
    //connect(f1,SIGNAL(sendMessageNewMessage(QString)),this,SLOT(receiveMessage(QString)));
    //f1->test();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::StartApplication(){
    //check_adb_exists();
    f2->test();
}

void MainWindow::ReceiveCustomMessage(const QString &msg){
    qDebug("Recieved message from child");
    qDebug("Message: " + msg.toLatin1());
}

void MainWindow::check_adb_exists(){
    QProcess *p = new QProcess();
    connect(p,&QProcess::readyReadStandardOutput,[&](){
        auto data = p->readAllStandardOutput();
        qDebug("Stdout: " + data);
    });
    connect(p,&QProcess::readyReadStandardError,[&](){
        auto data = p->readAllStandardError();
        qDebug("Error: " + data);
        if(data.toStdString().compare("File Not Found")){
            qDebug("File Not Found is the error");
        }
    });
    QStringList args;
    args << "/c dir C:\\Users\\%USERNAME%\\AppData\\Local\\Android\\Sdk";
    p->setArguments(args);
    p->setProgram("C:\\Windows\\System32\\cmd.exe");
    p->start();
}

子組件.cpp

#include "subwidget.h"
#include "ui_subwidget.h"

SubWidget::SubWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SubWidget)
{
    ui->setupUi(this);
    qDebug("parent: " + parent->objectName().toLatin1());
    connect(this,SIGNAL(SendCustomMessage(QString)),parent,SLOT(ReceiveCustomMessage(QString)));
}

SubWidget::~SubWidget()
{
    delete ui;
}

void SubWidget::test(){
    emit SendCustomMessage("trial message");
}

void SubWidget::SendCustomMessage(const QString &msg){
    qDebug("Sending Message: " + msg.toLatin1());
}

不得在 Qt 中定義信號。

來自 Qt wiki 上的 Signal & Slots:

信號由 moc 自動生成,不得在 .cpp 文件中實現

刪除您的實現,這應該可以工作。 但是,您不應在subclass綁定信號,因為這會降低封裝性和可重用性(父類必須具有ReceiveCustomMessage(QString)槽)。 而是將其綁定到外部,就像您在注釋掉的代碼中一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM