知识点
QPushButton信号函数clicked(void)QLineEdit信号函数textChanged(const QString&)QString编译期间直接生成的宏QStringLiteral(str)数字验证器,只允许输入数字内容 lineEdit->setValidator(new QDoubleValidator(this));//数字验证器在创建按钮或其他控件时,可以定义一个类中的私有成员函数来完成控件的创建以及槽函数连接,在创建函数形参表中使用const char* member接收需要绑定的自定义槽函数,传递槽函数实参时使用SLOT(mySolt())
m_Button
= createButton(QLatin1String("="),SLOT(clickButton()));
QPushButton
*MainWindow
::createButton(const QString
& name
, const char *member
)
{
QPushButton
* button
= new QPushButton(name
,this);
button
->setEnabled(false);
connect(button
,SIGNAL(clicked()),this,member
);
return button
;
}
void MainWindow
::clickButton()
{
double num
= m_editL
->text().toDouble()+m_editR
->text().toDouble();
m_editO
->setText(QString
::number(num
));
}
运行效果
当左操作数或右操作数为空时将禁用等号pushbutton,在左右操作数两个lineEdit中使用数字验证器设置只可以输入数字
文件结构
代码
Calculator.pro
QT
+= core gui
greaterThan(QT_MAJOR_VERSION
, 4): QT
+= widgets
CONFIG
+= c
++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES
+= QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES
+= \
main
.cpp \
mainwindow
.cpp
HEADERS
+= \
mainwindow
.h
# Default rules for deployment.
qnx
: target
.path
= /tmp
/$$
{TARGET
}/bin
else: unix
:!android
: target
.path
= /opt
/$$
{TARGET
}/bin
!isEmpty(target
.path
): INSTALLS
+= target
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QDebug>
#include <QDoubleValidator>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget
*parent
= nullptr);
~MainWindow();
public://func
void initWindow();
public slots
:
void enableButton(void);
void clickButton(void);
private://func
QLineEdit
* createLineEdit(const char* member
=nullptr,bool readOnlycli
= false);
QPushButton
* createButton(const QString
& name
,const char* member
);
private:
QLineEdit
* m_editL
;
QLineEdit
* m_editR
;
QLineEdit
* m_editO
;
QLabel
* m_labelA
;
QPushButton
* m_Button
;
};
#endif
mainwindow.cpp
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget
*parent
)
: QMainWindow(parent
)
{
initWindow();
}
MainWindow
::~MainWindow()
{
}
void MainWindow
::initWindow()
{
setWindowTitle("Calculator");
QFont font
;
font
.setPointSize(20);
setFont(font
);
resize(600,600);
m_editL
= createLineEdit(SLOT(enableButton()));
m_editR
= createLineEdit(SLOT(enableButton()));
m_editO
= createLineEdit(nullptr ,true);
m_labelA
= new QLabel("+",this);
m_Button
= createButton(QLatin1String("="),SLOT(clickButton()));
m_editL
->move(50,30) ;
m_labelA
->move(180,30);
m_editR
->move(200,30) ;
m_Button
->move(300,30) ;
m_editO
->move(400,30) ;
}
void MainWindow
::enableButton()
{
if(m_editR
->text() != ""&&m_editL
->text() != ""){
m_Button
->setEnabled(true);
}
else
m_Button
->setEnabled(false);
}
void MainWindow
::clickButton()
{
double num
= m_editL
->text().toDouble()+m_editR
->text().toDouble();
m_editO
->setText(QString
::number(num
));
}
QLineEdit
*MainWindow
::createLineEdit(const char *member
,bool readOnly
)
{
QLineEdit
*lineEdit
= new QLineEdit(this);
lineEdit
->setAlignment(Qt
::AlignRight
);
lineEdit
->setValidator(new QDoubleValidator(this));
if(readOnly
){
lineEdit
->setReadOnly(readOnly
);
}
if(member
){
connect(lineEdit
,SIGNAL(textChanged(const QString
)),this,member
);
}
return lineEdit
;
}
QPushButton
*MainWindow
::createButton(const QString
& name
, const char *member
)
{
QPushButton
* button
= new QPushButton(name
,this);
button
->setEnabled(false);
connect(button
,SIGNAL(clicked()),this,member
);
return button
;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc
, char *argv
[])
{
QApplication
a(argc
, argv
);
MainWindow w
;
w
.show();
return a
.exec();
}