C++ Qt编程实践——收货人信息清单
收货人信息清单(改进中):
dialog.h文件:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLineEdit> #include <QLabel> #include <QPushButton> #include <QComboBox> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); signals: private slots: void clearEditLine(); void saveClicked(); private: QLabel *name; QLabel *area; QLabel *address; QLabel *phone; QLabel *huo; QLabel *tel; QLabel *email; QLabel *addressAliase; QLabel *note; QLineEdit *editName; QComboBox *cobProvince; QComboBox *cobCity; QComboBox *cobArea; QLineEdit *editAddress; QLineEdit *editPhone; QLineEdit *editTel; QLineEdit *editEmail; QLineEdit *editAddressAliase; QPushButton *save; }; #endif // DIALOG_H
dialog.cpp文件:
#include "dialog.h" #include <QtGui> #include <QLayout> Dialog::Dialog(QWidget *parent) : QDialog(parent) { name = new QLabel(tr("收货人:")); editName = new QLineEdit; name->setBuddy(editName); area = new QLabel(tr("所在地区:")); cobProvince = new QComboBox(); cobProvince->addItem(tr("请选择")); cobProvince->addItem(tr("陕西")); cobCity = new QComboBox(); cobCity->addItem(tr("请选择")); cobCity->addItem(tr("西安市")); cobArea = new QComboBox(); cobArea->addItem(tr("请选择")); cobArea->addItem(tr("未央区")); cobArea->addItem(tr("长安区")); cobArea->addItem(tr("碑林区")); cobArea->addItem(tr("新城区")); cobArea->addItem(tr("高新区")); cobArea->addItem(tr("莲湖区")); cobArea->addItem(tr("雁塔区")); cobArea->addItem(tr("临潼区")); address = new QLabel(tr("详细地址:")); editAddress = new QLineEdit; address->setBuddy(editAddress); phone = new QLabel(tr("手机号码:")); editPhone = new QLineEdit; phone->setBuddy(editPhone); huo = new QLabel(tr("或")); tel = new QLabel(tr("固定电话:")); editTel = new QLineEdit; tel->setBuddy(editTel); email = new QLabel(tr("邮箱:")); editEmail = new QLineEdit; email->setBuddy(editEmail); addressAliase = new QLabel(tr("地址别名:")); editAddressAliase = new QLineEdit; addressAliase->setBuddy(editAddressAliase); note = new QLabel(tr("设置一个易记的名称,如:“送到家里”、“送到公司”")); save = new QPushButton(tr("保存收货地址")); connect(save, SIGNAL(clicked()), this, SLOT(saveClicked())); QHBoxLayout *areaLayout = new QHBoxLayout; areaLayout->addWidget(cobProvince); areaLayout->addWidget(cobCity); areaLayout->addWidget(cobArea); QVBoxLayout *leftPhone = new QVBoxLayout; leftPhone->addWidget(phone); leftPhone->addWidget(editPhone); //leftPhone->addStretch(); QVBoxLayout *rightTel = new QVBoxLayout; rightTel->addWidget(tel); rightTel->addWidget(editTel); //rightTel->addStretch(); QHBoxLayout *numLayout = new QHBoxLayout; numLayout->addLayout(leftPhone); numLayout->addWidget(huo); numLayout->addLayout(rightTel); //numLayout->addStretch(); QHBoxLayout *aliaseLayout = new QHBoxLayout; aliaseLayout->addWidget(editAddressAliase); aliaseLayout->addWidget(note); //aliaseLayout->addStretch(); QVBoxLayout *leftMainLayout = new QVBoxLayout; leftMainLayout->addWidget(name); leftMainLayout->addWidget(editName, 10); leftMainLayout->addWidget(area); // leftMainLayout->addLayout(areaLayout); leftMainLayout->addWidget(address); leftMainLayout->addWidget(editAddress); leftMainLayout->addLayout(numLayout); leftMainLayout->addWidget(email); leftMainLayout->addWidget(editEmail); leftMainLayout->addWidget(addressAliase); leftMainLayout->addLayout(aliaseLayout); leftMainLayout->addWidget(save, 20); //leftMainLayout->addStretch(); QVBoxLayout *rightMainLayout = new QVBoxLayout; QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(leftMainLayout); mainLayout->addLayout(rightMainLayout, 10); setLayout(mainLayout); setWindowTitle(tr("添加收货地址")); setFixedHeight(sizeHint().height()); setFixedWidth(sizeHint().width()); } Dialog::~Dialog() { } void Dialog::clearEditLine() { editName->setText(""); //cobProvince->setItemText(1); editAddress->setText(""); editPhone->setText(""); editTel->setText(""); editEmail->setText(""); editAddressAliase->setText(""); } void Dialog::saveClicked() { QDialog *ShowDialog = new QDialog; QString textName = editName->text(); QString textProvince = cobProvince->currentText(); QString textCity = cobCity->currentText(); QString textArea = cobArea->currentText(); QString textAddress = editAddress->text(); QString textPhone = editPhone->text(); QString textTel = editTel->text(); QString textEmail = editEmail->text(); QString textAddressAliase = editAddressAliase->text(); QLabel *showName = new QLabel(name->text() + textName); QLabel *showArea = new QLabel(area->text() + textProvince + textCity + textArea); QLabel *showAddress = new QLabel(address->text() + textAddress); QLabel *showPhone = new QLabel(phone->text() + textPhone); QLabel *showTel = new QLabel(tel->text() + textTel); QLabel *showEmail = new QLabel(email->text() + textEmail); QLabel *showAddressAliase = new QLabel(addressAliase->text() + textAddressAliase); QPushButton *editText = new QPushButton(tr("编辑")); QPushButton *deleteText = new QPushButton(tr("删除")); ShowDialog->connect(editText, SIGNAL(clicked()), ShowDialog, SLOT(close())); ShowDialog->connect(deleteText, SIGNAL(clicked()), ShowDialog, SLOT(close())); ShowDialog->connect(deleteText, SIGNAL(clicked()), this, SLOT(clearEditLine())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(editText); buttonLayout->addWidget(deleteText); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(buttonLayout); mainLayout->addWidget(showName); mainLayout->addWidget(showArea); mainLayout->addWidget(showAddress); mainLayout->addWidget(showPhone); mainLayout->addWidget(showTel); mainLayout->addWidget(showEmail); mainLayout->addWidget(showAddressAliase); ShowDialog->setLayout(mainLayout); ShowDialog->setWindowTitle(textName + textArea); ShowDialog->exec(); }
main.cpp文件:
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
运行截图:
本文出自 “V脸” 博客,请务必保留此出处http://xuzhen1024.blog.51cto.com/8881317/1625810
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。