qt2

控件的运用

(1)新建项目dialog
qt2_第1张图片
dialog.PNG
(2)在界面上添加控件
qt2_第2张图片
界面.PNG
qt2_第3张图片
ObjectName.PNG
(3)在.cpp中添加代码
#include "firstui.h"
#include "ui_firstui.h"
#include 
#include 
#include 
#include 
#include 

firstui::firstui(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::firstui)
{
    ui->setupUi(this);
}

firstui::~firstui()
{
    delete ui;
}

void firstui::on_ok_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString id = ui->lineEdit_id->text();
    QString sex = ui->sex->checkedButton()->text();
    QString age = ui->comboBox->currentText();
    QString college = ui->comboBox_college->currentText();

    QList ins = ui->interest->buttons();

    //保存文件
    QFile file("student.txt");
    if(!file.open(QIODevice::WriteOnly\
                  |QIODevice::Text|QIODevice::Append))return;
    QTextStream out(&file);
    out<isChecked())
        {
            out<<" "<text();
            ins[i]->setChecked(false);//清空界面
        }
    }
    out<lineEdit_name->clear();
    ui->lineEdit_id->clear();
    ui->comboBox->setCurrentIndex(0);
    ui->comboBox_college->setCurrentIndex(0);
    ui->sex->setExclusive(false);
    ui->sex->checkedButton()->setChecked(false);
    ui->sex->setExclusive(true);
}

void firstui::on_close_clicked()
{
    close();
}

运行结果:

在运行界面上输入如下
qt2_第4张图片
输入.PNG
点击ok,会清空界面
qt2_第5张图片
清空.PNG
并写入文件
qt2_第6张图片
写入文件.PNG

计算器

(1)界面如图:
qt2_第7张图片
计算机.PNG
(2)mainwindows.cpp里的代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化
    m_num1=0;
    m_num2=0;
    m_result=0;
    m_op='+';
    m_make=0;
    t='\0';
}
//析构函数
MainWindow::~MainWindow()
{
    delete ui;
}

//数字1按钮的函数
void MainWindow::on_pushButton_clicked()
{
    t+="1";
    if(!m_make)
    {
        m_num1=1;
    }
    else
    {
        m_num2=1;
    }
    ui->lineEdit->setText(t);
}

//数字2按钮的函数
void MainWindow::on_pushButton_2_clicked()
{
    t+="2";
    if(!m_make)
    {
        m_num1=2;
    }
    else
    {
        m_num2=2;
    }
    ui->lineEdit->setText(t);
}

//数字3按钮的函数
void MainWindow::on_pushButton_3_clicked()
{
    t+="3";
    if(!m_make)
    {
        m_num1=3;
    }
    else
    {
        m_num2=3;
    }
    ui->lineEdit->setText(t);
}

//数字4按钮的函数
void MainWindow::on_pushButton_4_clicked()
{
    t+="4";
    if(!m_make)
    {
        m_num1=4;
    }
    else
    {
        m_num2=4;
    }
    ui->lineEdit->setText(t);
}

//数字5按钮的函数
void MainWindow::on_pushButton_5_clicked()
{
    t+="5";
    if(!m_make)
    {
        m_num1=5;
    }
    else
    {
        m_num2=5;
    }
    ui->lineEdit->setText(t);
}

//数字6按钮的函数
void MainWindow::on_pushButton_6_clicked()
{
    t+="6";
    if(!m_make)
    {
        m_num1=6;
    }
    else
    {
        m_num2=6;
    }
    ui->lineEdit->setText(t);
}

//数字7按钮的函数
void MainWindow::on_pushButton_7_clicked()
{
    t+="7";
    if(!m_make)
    {
        m_num1=7;
    }
    else
    {
        m_num2=7;
    }
    ui->lineEdit->setText(t);
}

//数字8按钮的函数
void MainWindow::on_pushButton_8_clicked()
{
    t+="8";
    if(!m_make)
    {
        m_num1=8;
    }
    else
    {
        m_num2=8;
    }
    ui->lineEdit->setText(t);
}

//数字9按钮的函数
void MainWindow::on_pushButton_9_clicked()
{
    t+="9";
    if(!m_make)
    {
        m_num1=9;
    }
    else
    {
        m_num2=9;
    }
    ui->lineEdit->setText(t);
}

//按钮'+'号的函数
void MainWindow::on_add_clicked()
{
    t+="+";
    m_op='+';
    m_make=1;
    ui->lineEdit->setText(t);
}

//按钮'-'号的函数
void MainWindow::on_Reduction_clicked()
{
    t+="-";
    m_op='-';
    m_make=1;
    ui->lineEdit->setText(t);
}

//按钮'*'号的函数
void MainWindow::on_multiply_clicked()
{
    t+="*";
    m_op='*';
    m_make=1;
    ui->lineEdit->setText(t);
}

//按钮'/'号的函数
void MainWindow::on_divide_clicked()
{
    t+="/";
    m_op='/';
    m_make=1;
    ui->lineEdit->setText(t);
}

//按钮'='号的函数
void MainWindow::on_equal_clicked()
{
    switch(m_op)
    {
        case '+':
            m_result=m_num1+m_num2;
            break;
        case '-':
            m_result=m_num1-m_num2;
            break;
        case '*':
            m_result=m_num1*m_num2;
            break;
        case '/':
            m_result=m_num1/m_num2;
            break;
        default:
            break;
    }
    t+="=";
    QString s=QString::number(m_result,10);
    t+=s;
    ui->lineEdit->setText(t);
    //清空界面
    m_num1=0;
    m_num2=0;
    m_result=0;
    m_op='+';
    m_make=0;
    t='\0';
}

//清空按钮C
void MainWindow::on_empty_clicked()
{
    m_num1=0;
    m_num2=0;
    m_result=0;
    m_op='+';
    m_make=0;
    t='\0';
    ui->lineEdit->clear();
}


你可能感兴趣的:(qt2)