简体   繁体   English

Qt访问类中的mainWindow而不给它对象

[英]Qt access mainWindow in a class without giving the object to it

I need to access the mainWindow object in a different class. 我需要在其他类中访问mainWindow对象。 The problem is, that I can not give mainWindow to this class (I don't want to do this, it would make everything much more complicated). 问题是,我不能将mainWindow赋予此类(我不想这样做,这会使一切变得更加复杂)。 Question: Is there any way in C++ or Qt to put an object in something like a local "database" or sth where every other class in the project can look into and communicate with the objects. 问题:在C ++或Qt中,是否有任何方法可以将对象放入本地“数据库”之类的项目中,以便项目中的所有其他类都可以调查这些对象并与之通信。 What I want to have in the end is something like this: 我最后想要的是这样的:

// A.h
#ifndef A_H
#define A_H
class A{
public:
    A() { /*here comes the ting*/ myMainWindow->sayHi(); }
};
#endif // A_H


// MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "a.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    A *a = new A;
}
MainWindow::~MainWindow(){delete ui;}
MainWindow::sayHi(){
    // just do anything
}

I don't think, this is possible, but I give it a try... Thanks for answers! 我认为这是不可能的,但是我尝试一下...感谢您的回答!

I need to access the mainWindow object in a different class. 我需要在其他类中访问mainWindow对象。 The problem is, that I can not give mainWindow to this class (I don't want to do this, it would make everything much more complicated). 问题是,我不能将mainWindow赋予此类(我不想这样做,这会使一切变得更加复杂)。

That is doable. 那是可行的。 The author don't want to expose the "main window" variable holding the reference or pointer to the object. 作者不想公开包含对象的引用或指针的“主窗口”变量。 And, obviously the author wants that UI object to be callable from other objects. 而且,显然,作者希望该UI对象可从其他对象调用。 In Qt that implies either both objects on UI thread or communication is via queued signal-slot connection only. 在Qt中,这意味着UI线程上的两个对象或通信仅通过排队的信号槽连接进行。 But the direct call wanted, hence on the same thread. 但是需要直接调用,因此在同一线程上。

Is there any way in C++ or Qt to put an object in something like a local "database" or sth where every other class in the project can look into and communicate with the objects. 在C ++或Qt中,是否有任何方法可以将对象放入本地“数据库”之类的项目中,以便项目中的所有其他类都可以调查对象并与对象进行通信。

Local thread storage is a known pattern to implement things like that. 本地线程存储是一种实现此类目标的已知模式。 Qt has own implementation of it called QThreadStorage . Qt有自己的实现,称为QThreadStorage You can attempt something like that: 您可以尝试如下操作:

// myLts.h
void ltsRegisterObject(const QString &key, QObject *object);
void ltsRemoveObject(const QString &key);
QObject* ltsGetObject(const QString &key);
template <typename T> T* ltsGet(const QString &key) {
     return qobject_cast<T*>(ltsGetObject(key));
}


// myLts.cpp
static QThreadStorage<QMap<QString, QObject*> > s_qtObjects;

void ltsRegisterObject(const QString &key, QObject *object)
{
    s_qtObjects.localData().insert(key, object);
}

void ltsRemoveObject(const QString &key)
{
    if (!s_qtObjects.hasLocalData())
        return;

    s_qtObjects.localData().remove(key);
}

QObject* ltsGetObject(const QString &key)
{
    QObject *object;
    auto it = s_qtObjects.localData().find(key);
    return it != s_qtObjects.localData().end() ? it.value() : nullptr;
}

Register main window object in LTS: 在LTS中注册主窗口对象:

#include "myLts.h"
// ...
// register main window in LTS
ltsRegisterObject("mainWindow", &mainWindow);

Find and use the object: 查找并使用对象:

#include "myLts.h"
// ...
// use main window from LTS
auto* pMainWnd = ltsGet<QMainWindow>("mainWindow");
if (pMainWnd)
    pMainWnd->show();

PS I did not compile this. PS我没有编译这个。 But it is not hard to fix if so. 但是如果这样的话,修复起来并不难。

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

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