此工程(在QtCreator下建的工程)包含了几个类,dialog类、designerwidget类、multichoicewindow类、calculator类。
其中dialog类和designerwidget类都是用于测试槽的使用,dialog类是代码显示的ui,而designerwidget是用ui设计的界面。
Multichoicewindow 类更为复杂,主要功能是区分模态对话框和非模态对话框
Calculator是一个简单的计算器
代码如下:
//Designerwidget.h
#ifndef DESIGNERWIDGET_H
#define DESIGNERWIDGET_H
#include
#include
namespace Ui {
class DesignerWidget;
}
class DesignerWidget : public QWidget
{
Q_OBJECT
public:
explicit DesignerWidget(QWidget *parent = 0);
~DesignerWidget();
private slots:
void on_pushButton_clicked();
void on_lineEdit_returnPressed();
private:
Ui::DesignerWidget *ui;
};
#endif // DESIGNERWIDGET_H
// Designerwidget.cpp
#include "designerwidget.h"
#include "ui_designerwidget.h"
DesignerWidget::DesignerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DesignerWidget)
{
ui->setupUi(this);
}
DesignerWidget::~DesignerWidget()
{
delete ui;
}
void DesignerWidget::on_pushButton_clicked()
{
QMessageBox::information(this, "Message", "The text entered in the "
"manual widget window is:/n" + ui->lineEdit->text());
}
void DesignerWidget::on_lineEdit_returnPressed()
{
QMessageBox::information(this, "Message", "The text entered in the "
"manual widget window is:/n" + ui->lineEdit->text());
}
// Designerwidget.ui
//Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
private:
QLineEdit *m_lineEdit;
QPushButton *m_pbtn;
signals:
public slots:
private slots:
void showMessage(void);
};
#endif // DIALOG_H
//Dialog.cpp
#include
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
,m_lineEdit(new QLineEdit)
{
QHBoxLayout *topLayout = new QHBoxLayout();
m_pbtn=new QPushButton("OK");
topLayout->addWidget(new QLabel("EnterText:"));
// topLayout->addWidget(new QLineEdit);
topLayout->addWidget(m_lineEdit);
topLayout->addWidget(m_pbtn);
setLayout(topLayout);
//this->findChild("pushbutton")
//connect(m_pbtn,SIGNAL(clicked()),this,SLOT(showMessage()));
connect(m_lineEdit,SIGNAL(returnPressed()),this,SLOT(showMessage()));
}
void Dialog::showMessage(void)
{
QMessageBox::information(this, "Message", "The text entered in the "
"manual widget window is:/n" + m_lineEdit->text());
}
//calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include
namespace Ui {
class calculator;
}
class calculator : public QWidget
{
Q_OBJECT
public:
explicit calculator(QWidget *parent = 0);
~calculator();
private:
Ui::calculator *ui;
};
#endif // CALCULATOR_H
//calculator.cpp
#include "calculator.h"
#include "ui_calculator.h"
calculator::calculator(QWidget *parent) :
QWidget(parent),
ui(new Ui::calculator)
{
ui->setupUi(this);
}
calculator::~calculator()
{
delete ui;
}
//calculator.ui
//multichoicewindow.h
#ifndef MULTICHOICEWINDOW_H
#define MULTICHOICEWINDOW_H
#include
namespace Ui {
class MultiChoiceWindow;
}
class MultiChoiceWindow : public QWidget
{
Q_OBJECT
public:
explicit MultiChoiceWindow(QWidget *parent = 0);
~MultiChoiceWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MultiChoiceWindow *ui;
};
#endif // MULTICHOICEWINDOW_H
//multichoicewindow.cpp
#include "multichoicewindow.h"
#include "ui_multichoicewindow.h"
#include "designerwidget.h"
#include "dialog.h"
MultiChoiceWindow::MultiChoiceWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MultiChoiceWindow)
{
ui->setupUi(this);
}
MultiChoiceWindow::~MultiChoiceWindow()
{
delete ui;
}
void MultiChoiceWindow::on_pushButton_clicked()
{
QWidget *w=0;
if(ui->radioManual->isChecked())
//w = new ManualWidget();
w = new Dialog();
else
w = new DesignerWidget();
if(ui->radioModal->isChecked())
w->setWindowModality(Qt::ApplicationModal); //set modal dialog
w->setWindowTitle(ui->titleEdit->text());
w->show();
}
//multichoicewindow.ui
//main.cpp
//#include
#include
#include "dialog.h"
#include "designerwidget.h"
#include "multichoicewindow.h"
#include "calculator.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MultiChoiceWindow mc;
mc.show();
return a.exec();
}
程序运行结果图: