Saturday, January 5, 2013

Add control on Qt GUI and assign Signal/Slot graphically.

Last post describe how to new a empty Qt Gui Application. Base on the generated project, we are going to add a button graphically.

- Double click to open mainwindow.ui under Forms of our project in the Projects box. With .ui file selected, we can switch between (text) Edit View and (graphically) Design view, by click on the Edit/Design button on the left side bar.


- With Design View opened and make sure the Edit Widgets (F3) button is pressed, we can drag any widget from the left selection box to the design pane. Lets drag to add a Push Button. And double click on the text over it to edit text property to "Hello Qt".

Now, we can assign SIGNAL and SLOT to the Push Button to close the Window.

- Click on the Edit Signals/Slots (F4) button.

- Press and hold mouse left button over the Push Button, and pull a line out to any space, and release mouse button.

- Select clicked() in pushButton(QPushButton) box.
- Check to enable Show signals and slots inherited from QWidget.
- Select close() in MainWindow(QMainWindow).
- Click OK.

Now your Design view will become:

Finally Run your application. Now you can click the Push Button to close the Window.