如何在不同的qt QMainWindow上传播keyPressEvent

发布于 2024-09-27 02:34:08 字数 1831 浏览 1 评论 0原文

我有 2 个不同的 QMainWindow,第一个是第二个的父窗口。我想在子窗口中按一些键并在父窗口中传播事件。我为每个人创建了函数 void keyPressEvent(QKeyEvent* event);但是当我在孩子身上按下按键时,事件不会传播到父母。为什么?

这是代码...

//父类.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeyEvent>
#include "test.h"
#include <QDebug>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    void keyPressEvent(QKeyEvent* event);
private:
    Ui::MainWindow *ui;
    test *form;

private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

//父类.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    form = new test(this);
}

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

void MainWindow::keyPressEvent(QKeyEvent* event)
{
    qDebug() << event->text();
}

void MainWindow::on_pushButton_clicked()
{
    form->show();
}

//子类.h

#ifndef TEST_H
#define TEST_H

#include <QMainWindow>
#include <QKeyEvent>
namespace Ui {
    class test;
}

class test : public QMainWindow
{
    Q_OBJECT

public:
    explicit test(QWidget *parent = 0);
    ~test();
protected:
    void keyPressEvent(QKeyEvent* event);
private:
    Ui::test *ui;
};

#endif // TEST_H

//子类.cpp

#include "test.h"
#include "ui_test.h"

test::test(QWidget *parent) :  QMainWindow(parent),  ui(new Ui::test)
{
    ui->setupUi(this);
}

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

void test::keyPressEvent(QKeyEvent* event)
{
  qDebug() << event->text();
  event->ignore();

}

I have 2 different QMainWindow, the first is the parent of the second. I want press some keys in the children windows and propagate the event in the parent. I create for everyone the function void keyPressEvent(QKeyEvent* event); but when i press key on the children the event is not propagate to the parent. Why?

This is the code...

//Parent class.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeyEvent>
#include "test.h"
#include <QDebug>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    void keyPressEvent(QKeyEvent* event);
private:
    Ui::MainWindow *ui;
    test *form;

private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H

//Parent class.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    form = new test(this);
}

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

void MainWindow::keyPressEvent(QKeyEvent* event)
{
    qDebug() << event->text();
}

void MainWindow::on_pushButton_clicked()
{
    form->show();
}

//Children class.h

#ifndef TEST_H
#define TEST_H

#include <QMainWindow>
#include <QKeyEvent>
namespace Ui {
    class test;
}

class test : public QMainWindow
{
    Q_OBJECT

public:
    explicit test(QWidget *parent = 0);
    ~test();
protected:
    void keyPressEvent(QKeyEvent* event);
private:
    Ui::test *ui;
};

#endif // TEST_H

//Children class.cpp

#include "test.h"
#include "ui_test.h"

test::test(QWidget *parent) :  QMainWindow(parent),  ui(new Ui::test)
{
    ui->setupUi(this);
}

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

void test::keyPressEvent(QKeyEvent* event)
{
  qDebug() << event->text();
  event->ignore();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-10-04 02:34:08

QMainWindow 是一个顶级窗口,通常是事件传播结束的地方。我不完全清楚当顶级窗口有父级时可能的规则是什么。根据你的结果,我不得不假设它是成立的。

无论如何,您应该能够通过在 MainWindow 类中定义过滤器方法并将其安装在 test 类上来获取事件。请参阅此文档

您还可以选择重写 QApplicationevent() 方法。

A QMainWindow is a top-level window and that is usually where event propagation ends. I'm not entirely clear what may be the rule though when a top-level window is parented. By your results, I'll have to assume that it stands.

In any case, you should be able to get the event by defining a filter method in your MainWindow class and installing it on test class. Refer to this documentation.

You also have the option of overriding the event() method of QApplication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文