QT One project called class member variables for another project

Source: Internet
Author: User

A sentence has not been able to express the excitement of the present, only a sigh of knowledge and profound, and the sigh into the text.

Calling other class member variables in the same project is straightforward.

Such as:

Defined

Declare member variables in Test1.h

Class a{public:double m_ftest;};

Change the value of the member variable in Test1.cpp.

M_ftest = 265.78;

Then in Class B, the member variable m_ftest of Class A is called, and when the value is changed in Class A, it is also changed in Class B:

Method of invocation: The header file of the inclusion Class A

#include "Test1.h"

Then declare the object of Class A at the call:

A *a;

You can invoke a member variable by using the object of Class A:

a->m_ftest;

Calls in different projects:

This can no longer be called as above, although it is also possible to invoke, but the value does not change, only the initialization value can be called.

This requires a call through a virtual function:

In Project 1:

In Test1.h:

Class a{public:virtual void Update (); virtual void Onsimulationslider (int n) {};};

and call the virtual function in Project 2 in the virtual function updatesimulation

Test2.h: Overrides the virtual function Onsimulationslider and calls the virtual function in Project 2 in the virtual function updatesimulation

#include "Test1.h" class b{public:double m_ftest;void onsimulationslider (int n) {};};

In Project 2: The virtual function in Class A is called in Test3:

#include "Test1.h"

A *mtest;

Mtest->onsimulationslider (n);

Then manipulate the member variable that you want to invoke in the virtual function Onsimulationslider in Project 1.

If the class in Test4 in Project 2 wants to call the member variable through a class in Test3, you can pass the message.

For example: Test4 is an interface class, there is a slider control on the interface, after clicking on the slider, you want to make the value of member variable m_ftest in the Test1 in Project 1 change with the value of the slider.

A virtual function update in TEST4 is required in the virtual function updatesimulation in Test3 (overriding the virtual function update in Test1)

In Test4.h:

Declaring signals and slot functions:

Signals:

Voidhorizontalslider (int);

Public Slots:

Voidonhorizontalslider (int);

In the Test4.cpp:

Rewrite the virtual function updata in the Test1 and place the slider's signal in the virtual function
BOOL Simulationhardwaregui::update () {Qobject::connect (ui.m_slider,signal (valuechanged (int)), This,SLOT ( Onhorizontalslider (int))); This->repaint (); return true;}

Define the slot function Onhorizontalslider and call the signal function in the slot function

void Simulationhardwaregui::onhorizontalslider (int n) {qslider *changeslider = Qobject_cast<qslider *> (Sender () ); if (changeslider! = NULL) {horizontalslider (n);}}

Call the TEST4 signal function in TEST3:

Connect ((qobject*) (M_simulationhardware), SIGNAL (horizontalslider (int)), this, SLOT (onsimulationslider (int)));

and define the slot function Onsimulationslider, call the virtual function Onsimulationslider in Test1 in the slot function.

This cyclic invocation enables member variable intermodulation of different projects.

It is important to note that the parameters of the slot function and the signal function can only be types, and variables cannot be defined.


QT One project called class member variables for another project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.