[Solved] Accessing the data of one form to another form
-
hai,
I have created 5 forms like read1,read2,read3,read4 and read_main.
read1,read2,read3,read4 having the QComboBox. In this QComboBox added multiple options but all are having one similar option like "ALL". If I select ALL option from any ui window(read1,read2,read3,read4 ) It will open the read_main window and pass the related data to this read_main window. I would like to pass the data using connect() function. but my question is how can I choose the data is coming from the which form in read_main window?
can any one have an idea about this -
Well, there is a way to "ask" in the slot which object "send" the signal. It's not really Object orientated though, so not so great to use, but sometimes handy. It's based on the Qt Meta Object class and related QObject stuff.
@QObject * QObject::sender() const [protected]@
I would recommend using a different kind of way. Maybe in the constructor give every form a enum value and add this enum value into the signal. It totally depends on the application your writing. -
Hmm,
In every form generate a signal adding a "value" to indicate the sending object. e.g. in your read1 form:
@emit SelectedAll(READ_MAIN::FORM_1);@
And in your main_read:
@//Slot to catch the signal
connect (read1, &SelectedAll, this, &HandleSelectedAll);
...
void main_read(READ_MAIN eValue)
{
.. Do you clicked all stuff where eValue hold the form number..
}
@
That's about it and is basic signal/slot stuff. The big advantage of Qt signal/slots it is thread safe and multiple connections are easily made.The second option I described is more complex and needs more understanding of QObject and the MOC compiler in Qt. Therefor I would not recommend it! Still, you asked for it. Again connect all the signals to the main_read slot, but then without the arguments.
In your form constructors:
@
this->setProperty("Name", "Form1");
@
In your main_read slot do the following:
@
void main_read(void)
{
QObject * ObjectSender;
ObjectSender = sender(); // this should return the pointer to the sending object.
if (Sender != NULL)
{
if (sender->property("Name") == Form1)
.. it was form1 how triggered the slot
}
}
@
-- Didn't test this code, but should look something like this --
But AGAIN, it's not object orientated minded this option because the receiving class must know the name of the other objects causing dependencies between classes.
Hope this helps a bit, -
Oke, I reread your first post, but if I understand correct you want to copy some data from the first form and place it in the read_main form? You could define a class and add this to the Q_DECLARE_METATYPE(<type>) option and add this as argument in your signal/slot connection, but why do you want to "know" who issued the signal? Do you also want to update the data of the read1 form etc from the form read_main?
The thing to do is connect all readx form signals to a single slot in your read_main form.
Read the signal/slot document how to do the connect() function properly.
Btw this is done in code!! Not in designer. Designer only converts your forms to compiler readable code, that's all. but if all forms are the same, you only need 1 form that has multiple instances in your MainWindow (or other QWidget class).
Can't make it more clear in text without deep going conversation about design techniques, Qt creator, designer and QObject. -
Have u used socket ?? u can send text using socket.
-
Sorry . I misunderstood. Actually there is no use of socket.