开发者

Qt: Executing append from another class

开发者 https://www.devze.com 2023-04-02 22:10 出处:网络
I\'m quite new to Qt and C++ in general so this might be a simple problem. Current problem: I want to execute a function that appends text to a TextEditField by calling the function from another cla

I'm quite new to Qt and C++ in general so this might be a simple problem.

Current problem:

I want to execute a function that appends text to a TextEditField by calling the function from another class. I can call the function and the text gets appended via the same class but when i try calling it from another class it simply won't execute the append and gives no errors.

To make it simple i will just call my classes A and B.

Class A // The class that was created automatically by Qt 
Class B // A class that i made myself

Class A have a function called "logger" which looks like this:

void ClassA::logger()
{
    ui->Logg->append("A log message"); // Straight forward, it appends the text
}

I can call ClassA::logger from another function in Class A and everything works out great like this:

void ClassA::MakeALogg()
{
    logger(); // The logger function appends the text to TextEditField Logg
}

BUT, when i try to execute the logger function from another function it won't work. My current code looks like this in ClassB开发者_JS百科:

void ClassB::MakeALogg()
{
    ClassA A;
    A.logger(); // The text doesn't get appended to TextEditField Logg via external class
}

This won't work when calling the logger() function and no errors where given.

The end result is to make this being enabled to get a QString passed into the logger() function, something i all tough can do at this time if i call it via the same class e.g ClassA::MakeALogg() to ClassA::logger(QString LogMSG). But i want to first make sure that i can access the ui-> function from another class.

My Theory

Well i guess that the problem lies in some form of miss instantiation for the class for this specific exection (e.g the Ui type). I can at this stage call a function from class A to Class B and rechive a return { for example ClassA::GiveNumber() to ClassB::NumberCalculator() }. But when it comes related to the Ui it can't make a connection for some strange reason.

I've also tried to in the header file of ClassB to make a relation to it but that didn't work either:

class ClassA;

class ClassB
{
public:
ClassA * PointToClassA;
}

Void ClassB::Logger()
{
PointToClassA = new ClassA;
ClassA->logger();
}


On both your methods, you are creating a new ClassA object, you would see the problem if you called show on the local variable:

void ClassB::MakeALogg()
{
    ClassA A;
    A.show();
    A.logger(); 
}

So, you should be calling the logger method on the class instance that already exists, and to do so, you need either to:

  • pass the pointer of ClassA to all the classes that need logging,
  • use a static function of ClassA or a global function to route the call to that instance (some sort of "Singleton pattern"),
  • emit a signal in ClassB, and connect it to a slot in ClassA that would do the logging (the signal/slot connection should be done in ClassA).

If there is only 2 classes, as it seems to be the case according to the example you gave with "ClassB::NumberCalculator" or if all the instances of the other classes are children of a unique instance of ClassA, the third solution is probably the best.


Like in your other question. Everytime you call this function you create a completely new instance of class A. The function actually works, but you don't see anything, because right after the function returns you return from the class B method and the local object gets destroyed. You have to provide the B a pointer or reference to the real A object you want this function to be called on.

0

精彩评论

暂无评论...
验证码 换一张
取 消