![]() |
|
#1
|
|||
|
|||
Passing an object as argument to a function?Hi,
I'm new to programming. I'm learning Cppand am coding my first program with Qt . I made some Dialog classes and objects from the classes. I then wrote a function for each dialog that will bring the dialog on top and set it as the active window. Up untill now I created a specific function for each dialog. I know it should be possible to do this with just one function though. This is an example of the functions: CPP / C++ / C Code:
In this function findDialog is an object from the FindDialog class. I created similar functions for dossierDialog from the DossierDialog class etc... These functions work, but i need one for each dialog. I then thought it should be possible to write just one function that can do this for each dialog. So I tried passing the name of the dialog as a string: CPP / C++ / C Code:
This ofcourse didn't work because dialogname is a string and not the object with wich you can do ->show()... Now in the past I wrote a few scripts for websites in Javascript where I could do something like this: eval(dialogname + "->show()"); This would turn dialogname into the right object. Is there something similar for Cpp or do I have to pass the object itself as a parameter for the function? And how would I go about that? Isn't it a problem that the objects are of different classes then? Thanks, Ahas |
|
#2
|
||||
|
||||
Re: Passing an object as argument to a function?Quote:
C++ isn't a scripting language, and something like this would never work; unfortunately. It is strongly typed, like most imperative languages I know, so you need to pass the actual object to your function. Now, how can you do that? I appologyze for my lack of experience with QT. However, I am almost sure there is a base type all these dialogs have, and you can promote your specific dialogs to that type, provided the base class has the minimum API interface required for your function (meaning, it has show, toTop and setActiveWindow defined). Here is a simple example of what I mean: animal.h CPP / C++ / C Code:
main.cpp CPP / C++ / C Code:
I am hoping there is a class like QDialog all your other Dialogs are derived from; you could use this as the parameter to the function, and call the show and the other functions like I did with to produce my "Meows". Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein Last edited by LuciWiz : 25-Jul-2006 at 09:19.
|
|
#3
|
|||
|
|||
Re: Passing an object as argument to a function?Thanks,
That's the solution I'm looking for. All dialogs are indeed derived of a QDialog class so this should work. I kind of expected I couldn't do it with the string. This will make things a lot easier for me. Thanks again, Ahas |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 11:33 |
| array function argument | pixienick | C++ Forum | 2 | 02-Sep-2005 04:34 |
| Trouble passing STL into a function | Clive73 | C++ Forum | 5 | 10-May-2005 18:00 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The