运行效果如图
具体实现步骤:
一、首先用 Qt Designer 创建一个两张图的对话框,分别保存为listdialog.ui和editdialog.ui文件
要注意其中各个空间对应的名称修改好
二、新建一个Qt应用程序工程,取名为listdialog
三、删除已有的listdialog.ui和ui_listdialog.h,listdialog.h,listdialog.cpp文件
四、将listdialog.ui和editdialog.ui文件复制到工程下并且导入到工程,运行,此时可能会报错,没关系
五、添加文件listdialog.h,listdialog.cpp,editdialog.h,editdialog.cpp
内容如下:
listdialog.h文件
#ifndef LISTDIALOG_H
#define LISTDIALOG_H
#include <QDialog>
#include "ui_listdialog.h"
class ListDialog : public QDialog
{
Q_OBJECT
public:
ListDialog();
~ListDialog();
private slots:
void addItem();
void editItem();
void deleteItem();
private:
Ui::ListDialog ui;
};
#endif // LISTDIALOG_H
listdialog.cpp文件
#include "listdialog.h"
#include "editdialog.h"
ListDialog::ListDialog()
: QDialog()
{
ui.setupUi(this);
connect(ui.addButton,SIGNAL(clicked()),this,SLOT(addItem()));
connect(ui.editButton,SIGNAL(clicked()),this,SLOT(editItem()));
connect(ui.deleteButton,SIGNAL(clicked()),this,SLOT(deleteItem()));
}
ListDialog::~ListDialog()
{
}
void ListDialog::addItem()
{
EditDialog dlg(this);
if(dlg.exec()==1)
ui.list->addItem(dlg.name()+"--"+dlg.number());
}
void ListDialog::editItem()
{
if(!ui.list->currentItem())
return;
QStringList parts=ui.list->currentItem()->text().split("--");
EditDialog dlg(this);
dlg.setName(parts[0].trimmed());
dlg.setNumber(parts[1].trimmed());
if(dlg.exec()==1)
ui.list->currentItem()->setText(dlg.name()+"--"+dlg.number());
}
void ListDialog::deleteItem()
{
delete ui.list->currentItem();
}
editdialog.h 文件
#ifndef EDITDIALOG_H
#define EDITDIALOG_H
#include <QDialog>
#include "ui_editdialog.h"
class EditDialog:public QDialog
{
public:
EditDialog(QWidget*parent=0);
const QString name() const;
void setName(const QString&);
const QString number() const;
void setNumber(const QString&);
private:
Ui::EditDialog ui;
};
#endif
editdialog.cpp 文件
#include "editdialog.h"
EditDialog::EditDialog(QWidget *parent):QDialog(parent)
{
ui.setupUi(this);
}
const QString EditDialog::name()const
{
return ui.nameEdit->text().replace("--","").trimmed();
}
const QString EditDialog::number() const
{
return ui.numberEdit->text().trimmed();
}
void EditDialog::setName(const QString &name)
{
ui.nameEdit->setText(name);
}
void EditDialog::setNumber(const QString &number)
{
ui.numberEdit->setText(number);
}
注意还要查看自动生成的 ui_editdialog.h 和 ui_listdialog.h
根据需要修改类的名称,上例中需要将ui_editdialog.h修改为如下:
#ifndef UI_EDITDIALOG_H
#define UI_EDITDIALOG_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_EditDialog
{
public:
QDialogButtonBox *buttonBox;
QWidget *layoutWidget;
QHBoxLayout *horizontalLayout;
QVBoxLayout *verticalLayout;
QLabel *label;
QLabel *label_2;
QVBoxLayout *verticalLayout_2;
QLineEdit *nameEdit;
QLineEdit *numberEdit;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(360, 125);
buttonBox = new QDialogButtonBox(Dialog);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setGeometry(QRect(0, 80, 341, 41));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
layoutWidget = new QWidget(Dialog);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(10, 10, 331, 51));
horizontalLayout = new QHBoxLayout(layoutWidget);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
verticalLayout = new QVBoxLayout();
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
label = new QLabel(layoutWidget);
label->setObjectName(QString::fromUtf8("label"));
verticalLayout->addWidget(label);
label_2 = new QLabel(layoutWidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
verticalLayout->addWidget(label_2);
horizontalLayout->addLayout(verticalLayout);
verticalLayout_2 = new QVBoxLayout();
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
nameEdit = new QLineEdit(layoutWidget);
nameEdit->setObjectName(QString::fromUtf8("nameEdit"));
verticalLayout_2->addWidget(nameEdit);
numberEdit = new QLineEdit(layoutWidget);
numberEdit->setObjectName(QString::fromUtf8("numberEdit"));
verticalLayout_2->addWidget(numberEdit);
horizontalLayout->addLayout(verticalLayout_2);
QWidget::setTabOrder(nameEdit, numberEdit);
QWidget::setTabOrder(numberEdit, buttonBox);
retranslateUi(Dialog);
QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("Dialog", "Name:", 0, QApplication::UnicodeUTF8));
label_2->setText(QApplication::translate("Dialog", "Number:", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class EditDialog: public Ui_EditDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_EDITDIALOG_H
现在编译运行就可以了
注意ui_editdialog.h有时候打不开,需要先编译一下,有错误没关系然后就可以打开了