Getting Started |
Widget | ObjectName | Text |
---|---|---|
QLabel | "nameText" | "Name:" |
QLabel | "emailText" | "Email:" |
QLineEdit | "nameEdit" | "" |
QLineEdit | "emailEdit" | "" |
QPushButton | "okButton" | "OK" |
Now you can layout your widgets. Hint: Use a grid layout for the labels and line edits.
We want the OK button to invoke the QDialog::accept() slot. and we do this by clicking on the Edit Connections toolbar button. You will then enter Qt Designer's connection mode.
Click on the OK button and drag the mouse cursor to an empty area of the form and release the mouse button. The Configure Connection dialog will pop up, allowing you to establish a signal-slot connection between the OK button and the form. Use it to connect the button's clicked() signal to the form's accept() slot.
Once we have completed designing the dialog, we have to invoke it when the user clicks the main dialog's Add button. To achieve this behavior, we add a slot to the AddressBook class and invoke the AddDialog from this slot.
The Qt Visual Studio Integration makes this very easy. Simply double-click on the Add button; this will open the .cpp file associated with the dialog and generate a skeleton slot called on_addButton_clicked(). Type in the following lines of code in the slot's body:
AddDialog dialog(this); dialog.exec();
Forms created using Qt Designer call QMetaObject::connectSlotsByName() to establish connections between signals emitted by the form's child widgets and slots that follow the naming convention on_<sender>_<signal>().
If we want to connect to another signal than clicked(), we have to manually add the slot to the AddressBook class. This requires editing both the header file (addressbook.h) and the implementation file (addressbook.cpp).
Next, we include adddialog.h to addressbook.cpp:
#include "adddialog.h"
Build and run the program now. If you click on the Add button, the Add Address dialog will pop up. If you click on OK, the dialog will disappear.
When the user clicks OK, we would like to add an item to the QListWidget. To do so, modify the code in the on_addButton_clicked() slot to the following:
AddDialog dialog(this); if (dialog.exec()) { QString name = dialog.nameEdit->text(); QString email = dialog.emailEdit->text(); if (!name.isEmpty() && !email.isEmpty()) { QListWidgetItem *item = new QListWidgetItem(name, ui.addressList); item->setData(Qt::UserRole, email); ui.addressList->setCurrentItem(item); } }
We execute the dialog. If the dialog is accepted, e.g., OK is clicked, we extract the Name an Email fields and create a QListWidgetItem with the given information.
Try out the application. Click Add, enter "John Doe" for the name and "john@johndoe.de" for the email. Click OK. The list widget should now contain the new item.
When the user selects an item in the list widget, we would like to update the nameLabel and emailLabel at the bottom of the form. This requires another slot to be added to the AddressBook class.
In the addressbook.h file, add the following code in the private slots section of the class:
void on_addressList_currentItemChanged();
Then add the block of code below to addressbook.cpp:
void AddressBook::on_addressList_currentItemChanged() { QListWidgetItem *curItem = ui.addressList->currentItem(); if (curItem) { ui.nameLabel->setText("Name: " + curItem->text()); ui.emailLabel->setText("Email: " + curItem->data(Qt::UserRole).toString()); } else { ui.nameLabel->setText("<No item selected>"); ui.emailLabel->clear(); } }
Thanks to the naming convention, this slot will automatically be connected to addressList's currentItemChanged() signal, and will be invoked whenever the selected item in the list changes.
Earlier, we added some code by double-clicking the Add button. This automatically creates a slot corresponding to the widget's default signal - the signal that you are most likely to be interested in - for example, clicked() for QPushButton. If you want to connect to another signal, you must write the code manually as we have done for the addressList's currentItemChanged() signal.
Similar to the Add button, we implement a slot for the Delete button. Open the addressbook.ui file and double-click on Delete. This will pop a code editor up with an empty slot called on_deleteButton_clicked(). Type the following code in the slot's boy:
QListWidgetItem *curItem = ui.addressList->currentItem(); if (curItem) { int row = ui.addressList->row(curItem); ui.addressList->takeItem(row); delete curItem; if (ui.addressList->count() > 0) ui.addressList->setCurrentRow(0); else on_addressList_currentItemChanged(); }
Your application is now complete!
If you want to build this application on other platforms, you need to create a .pro file for the project. A simple way to do this is to let the Visual Studio integration create a basic .pro file for you by clicking Qt|Create Basic .pro File. When the Export Project dialog shows up, ensure that the Create .pri File option is checked, then click OK. Visual Studio will then ask you where to save the .pri file. The default location and name is usually sufficient, so just click Save. For more information about .pro files and their associated .pri files, see Managing Projects.
That's it! You should now have a working .pro file and .pri file for your project. For more complex projects, manually editing the .pro file is required to make it work on all plaforms; however, for our simple project the generated .pro file is sufficient.
[Previous: Qt Visual Studio .NET Integration] [Contents] [Next: Managing Projects]
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt vs-integration-1.4 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com