PDA

View Full Version : From VB6 to QT



axisdj
19th May 2010, 16:38
Alright, I have a mental black and would like some help. I have 2 commercial applications written in VB6 , over the past 8 years. Gonna start re-writing it using QT Creator, but functionallity needs to stay the same.

In vb6 you bassically add a form to a project and any public methods in any of the forms are visible to each other. I am trying to find the best way to do this in QT.

For example. I have 2 forms, Form A and From B created in the designer. Form A has a dial on it. Form B has a label on it. I want to be able to set the label on Form B to be the value of the dail when it changes.

in vb6 it would simply be in the: on the form A dial value change event: formB.Label.Text = dial.value

I guess behind the scenes in vb6 somewhere all the form object are kept track of. Can I do this in QT, or what is the best approach.

Zlatomir
19th May 2010, 16:45
In Qt the mechanism for that are: signal and slots, read more here (http://doc.trolltech.com/4.6/signalsandslots.html).

Use signal/slots in simple application in the beginning, because they might look "strange" when you see them first time, but after you use them a couple of times you get "addicted" and wish every framework will have something like this ;)

axisdj
19th May 2010, 17:00
I do understand signals and slots. My main lack of understanding is how to access a slot in another form?

I guess in mainWindow I could create an instance of formA and FormB and that would allow each form to talk to each other? A small example event in psuedo code would help.

Thanks

Zlatomir
19th May 2010, 17:08
Widgets (forms, dialogs, anything) are all objects, you just connect the one object SIGNAL with another object SLOT.
See here (http://www.qtcentre.org/threads/30755-how-to-create-window-that-opened-when-close-main-window?p=143644#post143644) a simple example of one window with one button, and the press of the button is opening another window (it's similar with widgets created with Designer)

axisdj
19th May 2010, 17:16
Just to clarify, all the forms I'll be using need to be instantiated from the main loop? In the example the main loop is where all the forms get created, and kept track of, is that correct? See in vb6 all forms automatically get instantiated and the main loop is not visible.

Zlatomir
19th May 2010, 17:34
You can create custom widgets in separate class, but you have to initialize them, or a widget that contains them in main(...) function, in c++ this is where the execution starts and hopefully (if no error elsewhere) finish.

The Widgets that are created (no mater where) are not visible by default (you have to set themselves, or their parent, visible with show() method or SLOT)

Extremely important advice: do not try to write code like in VB, learn the Qt way of c++ (this advice is valid for any two programming languages).

LE: main isn't a loop, it's a function, the run of app.exec() is function that run the event-loop in Qt.

axisdj
19th May 2010, 17:47
Thanks for your response you have cleared up alot. I will try to learn the c++ way, I have read about 10 books and am now trying to apply my learning. It is just extremly difficult to go from vb6 that is so easy and automatic, to this.

I am looking for a tutor that can help me via skype. I can pay per hour rate? if anyone is interested please email me at axisdj@world-net.net.

Thanks again for the quick response.

squidge
19th May 2010, 17:48
I do understand signals and slots. My main lack of understanding is how to access a slot in another form?You can do it very similar to the way you do it in VB, but this is discouraged. If you have the time you really should do it properly and have each form as a seperate entity. For example, class A which contains your dial could either call a method in class B (which contains your label) to state that its dial value has been changed, or you class A could emit a signal which class B could catch. Class B would then take the appropriate action based on that information, such as updating the value of it's label.

So your dial in class A would be connected to a slot in class A.
Class A would then call a method in class B, or emit a signal.
Class B would update it's form.

axisdj
19th May 2010, 17:54
I was just testing functionality, most of my vb6 app does use classes and the approach you mentioned above, by having each form/class handle the updates themselves.
Ok that makes sense, but both Class A and Class B were instatiated from main?

squidge
19th May 2010, 20:01
In Qt land normally you would instantiate a class C of type QMainWindow, and that would create class A and class B.