简体   繁体   English

Qt:将对象传递到每个窗口

[英]Qt: passing objects to every window

Im a beginner at C++ since I'm a Java-Dev. 我是Java开发人员,因此是C ++的初学者。 I'm learning Qt right now. 我正在学习Qt。 Now I want in my widget application on central object for holding special functionality (for example a bunch of methods for sending different network-command or something like that). 现在,我想在中心对象的小部件应用程序中保留特殊功能(例如,用于发送不同网络命令或类似方法的一堆方法)。 In my mainwindow.cpp I've got this 在我的mainwindow.cpp文件中

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //...
    BusinessLogic* bl = new BusinessLogic;
    bl->setMyValue(44);
    this->setBusinessLogic(bl);
    this->getBusinessLogic();

    //...
}

BusinessLogic* MainWindow::getBusinessLogic()
{
    return this->bl;
}

void MainWindow::setBusinessLogic(BusinessLogic* newbl)
{
    this->bl = newbl;
}

Where BusinessLogic.cpp is the class which I want to access from all windows in my application. 我要从应用程序中所有窗口访问的类是BusinessLogic.cpp。 So in my Dialog which get opend when the pushButton is clicked, I try to use the businesslogic instance like this in the constructor 因此,在单击pushButton时打开的Dialog中,我尝试在构造函数中使用像这样的businesslogic实例

Dialog::Dialog(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    //...   
    ((MainWindow*)parent)->getBusinessLogic();
}

Unfortunately the program crashes everytime the dialog get instanciated (the line with ((MainWindow*)parent)->getBusinessLogic(); get called. 不幸的是,每当实例化对话框(带有((MainWindow*)parent)->getBusinessLogic();被调用((MainWindow*)parent)->getBusinessLogic();程序都会崩溃。

What can I do? 我能做什么?

There are several methods for achieving your goal(all depends on your requirements) but one approach is you can create Singleton class of your BusinessLogic and then access it anywhere. 有几种方法可以实现您的目标(全部取决于您的要求),但是一种方法是您可以创建BusinessLogic的Singleton类,然后在任何地方访问它。 have a look here . 在这里看看。 OR you can also pass object of class one class to another. 或者,您也可以将一类的对象传递给另一类。 OR you can use signal slot mechanism. 或者,您可以使用信号插槽机制。

For you best Solution can be 为您提供最佳解决方案

Class BLogic{
public:
    static BLogic* instance(){
       static BLogic ins;
       return &ins;
    }
    void setVal(int v){ 
        val = v;
    }
private:
    BLogic(){}
    int val;
};

Now you can call functions of BLogic from any class like 现在您可以从任何类调用BLogic的函数

BLogic::instance()->setVal(10);

OR Example of passing objects can be 传递对象的示例可以是

class ClassA
{
public:
   void setClassB(ClassB *ob){
      m_classB = ob;
   }
   ClassB* getClassB(){
      return m_classB;
   }
private:
   ClassB* m_classB;
};

class ClassB
{
public:
   void setClassA(ClassA *ob){
      m_classA = ob;
   }
   ClassA* getClassA(){
      return m_classA;
   }
private:
   ClassA* m_classA;
};

Now from main.cpp 现在从main.cpp

ClassA* obA = new ClassA;
ClassB* obB = new ClassB;
obA->setClassB(obB);
obB->setClassA(obA);

Don't pass a zero parent or a dangling pointer to the dialog. 不要将零父级或悬挂指针传递给对话框。

But more seriously, verify your assumptions: 但更重要的是,验证您的假设:

qDebug() << "the parent object is" << parent;
auto p = qobject_cast<MainWindow*>(parent);
if (p)
    p->getBusinessLogic();

Debug output will give you some idea of whether a parent is set, and of what type it is at the moment. 调试输出将使您对是否设置了父对象以及当前的父类型有一些了解。 Remember that during the construction of a MainWindow instance, it goes through being different types (quite literally). 请记住,在构造MainWindow实例的过程中,它经历了不同的类型(从字面上看很像)。 So depending on when you construct the child, you might get a parent of type QWidget* or QMainWindow* or MainWindow* . 因此,根据构造子项的时间,您可能会得到QWidget*QMainWindow*MainWindow*类型的parent项。 During the destruction you could even get a QObject* parent! 在销毁期间,您甚至可以获得QObject*父对象!

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

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