从BOP到OOP
- 基于对象的Qt编程(不推荐)
- 面向对象的Qt编程
案例:
1. 基于对象的Qt编程:
方案
在文本框中写入文字,显示在上方的QLabel中
- 写在main.cpp
#include <QApplication>#include <QDialog>#include <QLabel>#include <QLineEdit>int main(int argc,char** argv){QApplication app(argc,argv);QDialog parent;parent.move(500,200);parent.resize(300,200);QLabel label(&parent);label.move(75,50);label.resize(150,35);label.setFrameShape(QFrame::Panel);QLineEdit edit(&parent);edit.move(75,100);edit.resize(150,35);parent.show();QObject::connect(&edit,SIGNAL(textChanged(QString)),&label,SLOT(setText(QString)));return app.exec();}
include
include
include
class ChangeDialog : public QDialog { Q_OBJECT
public: ChangeDialog(QWidget *parent = 0); ~ChangeDialog();
QLineEdit *edit;QLabel *label;
};
endif // CHANGEDIALOG_H
<br />changedialog.cpp```cpp#include "changedialog.h"ChangeDialog::ChangeDialog(QWidget *parent): QDialog(parent){this->resize(400,400);label = new QLabel(this);label->resize(100,30);label->move(50,50);label->setFrameShape(QFrame::Panel);edit = new QLineEdit(this);edit->resize(100,30);edit->move(50,80);// QObject::connect(edit,SIGNAL(textChanged(QString)),// label,SLOT(setText(QString)));}ChangeDialog::~ChangeDialog(){}
main.cpp
#include "changedialog.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);ChangeDialog w;QObject::connect(w.edit,SIGNAL(textChanged(QString)),w.label,SLOT(setText(QString)));w.show();return a.exec();}

2. 面向对象的Qt编程:加法计算器
方案
声明计算器对话框(继承QDialog)CalculatorDialog类。将操作数输入输出文本框(QLineEdit),和计算按钮(QPushButton)子窗口定义为成员变量,在输入文本框输入时,使能技术按钮。当点击计算按钮开始对输入文本框中数字字符串转换为数值,然后进行计算,将计算的结果转换为字符串并显示在输出文本框中。
calculatordialog.h
#ifndef CALCULATORDIALOG_H#define CALCULATORDIALOG_H#include <QDialog>#include <QLabel>#include <QPushButton>#include <QLineEdit> // 行编辑控件#include <QHBoxLayout> // 水平布局器#include <QDoubleValidator> // 数字验证器class CalculatorDialog : public QDialog{Q_OBJECT// moc(元对象编译器)public:CalculatorDialog(QWidget *parent = 0);~CalculatorDialog();public slots:// 使能等号按钮的槽函数void enableButton(void);// 计算并显示结果的槽函数void calcClicked(void);private:QLineEdit* m_editX; // 左操作数QLineEdit* m_editY; // 右操作数QLineEdit* m_editZ; // 结果QLabel* m_label; // "+"QPushButton* m_button;// "="};#endif // CALCULATORDIALOG_H
calculatordialog.cpp
#include "calculatordialog.h"CalculatorDialog::CalculatorDialog(QWidget *parent): QDialog(parent){// 界面初始化this->setWindowTitle("加法计算器");// 左操作数 this-> 当前父窗口m_editX = new QLineEdit(this);// 设置文本对齐 -- 右对齐m_editX->setAlignment(Qt::AlignRight);// 设置数字验证器 只能输入数字形式的内容m_editX->setValidator(new QDoubleValidator(this));// 右操作数m_editY = new QLineEdit(this);m_editY->setAlignment(Qt::AlignRight);m_editY->setValidator(new QDoubleValidator(this));// 结果m_editZ = new QLineEdit(this);m_editZ->setAlignment(Qt::AlignRight);// 设置只读m_editZ->setReadOnly(true);// "+"m_label = new QLabel("+",this);// "="m_button = new QPushButton("=",this);//设置禁用m_button->setEnabled(true);// 创建布局器:自动调整每个控件的大小和位置QHBoxLayout* layout = new QHBoxLayout(this);// 按水平方向,从左向右依次将控件放到布局器中layout->addWidget(m_editX);layout->addWidget(m_label);layout->addWidget(m_editY);layout->addWidget(m_button);layout->addWidget(m_editZ);// 设置布局器setLayout(layout);// 信号和槽函数的连接// 左右操作数文本改变时,会发送信号 textChangedQObject::connect(m_editX,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));QObject::connect(m_editY,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void)));// 点击等号按钮 发送 clicked信号QObject::connect(m_button,SIGNAL(clicked(void)),this,SLOT(calcClicked(void)));}// 使能等号按钮的槽函数void CalculatorDialog::enableButton(void){bool bXOk,bYOk;// text(): 获取QLineEdit的文本// toDouble(): QString转换成double,参数保存转换是否成功m_editX->text().toDouble(&bXOk);m_editY->text().toDouble(&bYOk);// 当左右操作数都输入了正确的数字时,则使能等号按钮,否则设置禁用m_button->setEnabled(bXOk && bYOk);}// 计算并显示结果的槽函数void CalculatorDialog::calcClicked(void){double res = m_editX->text().toDouble() + m_editY->text().toDouble();// number(): 将double转换成QStringQString str = QString::number(res);// 显示字符串形式的结果m_editZ->setText(str);}CalculatorDialog::~CalculatorDialog(){}

3.面向对象显示系统时间
方案
实现一个能获取显示系统时间的应用,通过点击获取按钮即能在标签文本上显示当前的系统时间。在Dailog父窗口中分别创建标签控件和按钮控件,创建自定义槽函数,用来获取当前系统时间,创建自定义信号函数,只需声明不用定义。moc编译器定义它,本质就是完成槽函数调用的工作用于显示最新的系统时间。
timedialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QLabel>#include <QTime>#include <QVBoxLayout>#include <QPushButton>//声明TimeDlg类,公共继承Qdialogclass TimeDialog : public QDialog{//声明宏Q_OBJECT//构造函数,参数为父窗口的指针,缺省为NULLpublic:TimeDialog(QWidget *parent = NULL);//声明标签成员变量,用来显示时间private:QLabel* m_labTime;public:~TimeDialog();//自定义槽函数,用来提供当前系统时间private slots:void timeClicked(void);signals://自定义信号函数,只需声明不用定义。//moc编译器定义它,本质就是完成槽函数调用的工作void setText(QString const&);};#endif // DIALOG_H
timedialog.cpp
#include "dialog.h"TimeDialog::TimeDialog(QWidget *parent): QDialog(parent){setWindowTitle(tr("时间"));m_labTime = new QLabel();/** 设置边框效果,凹陷的感觉* QFrame::Panel--QFrame绘制一个面板,使内容看起来凸起或凹陷* QFrame::Sunken--框架和内容显示为凹陷;使用当前颜色组的浅色和深色绘制三维凹陷线*/m_labTime->setFrameStyle(QFrame::Panel | QFrame::Sunken);//水平居中AlignHCenter和垂直居中AlignVCenterm_labTime->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//构建按钮对象QPushButton* btnTime = new QPushButton(tr("当前系统时间"));//垂直布局器QVBoxLayout* lay = new QVBoxLayout;//将小部件添加到此框布局的末尾,拉伸因子为拉伸和对齐lay->addWidget(m_labTime);lay->addWidget(btnTime);setLayout(lay);connect(btnTime,SIGNAL(clicked(void)),this,SLOT(timeClicked(void)));}//获取当前系统时间并按hh:mm:ss字符串格式显示出来。void TimeDialog::timeClicked(void){m_labTime->setText(QTime::currentTime().toString("hh:mm:ss"));}TimeDialog::~TimeDialog(){}


