Qt学习-(基本按钮功能实现)

2016-12-20  本文已影响0人  I踏雪寻梅

Qt学习

模拟登陆界面

void MainWindow::on_pushButton_clicked()
{
    //qDebug()<<ui->linename->text()<<endl;
    //qDebug()<<ui->linepwd->text()<<endl;
    if((ui->linename->text()=="ssh123")&&(ui->linepwd->text()=="543859438"))
    {
        qDebug()<<"ok"<<endl;
    }
    else
    {
        qDebug()<<"error"<<endl;
    }
}

void MainWindow::on_close_clicked()
{
    close();
}
Paste_Image.png Paste_Image.png

将password的echo模式改为password即可实现密码隐藏

Paste_Image.png

界面的objectname-MainWindow不可以修改!

模拟信号和槽之间的连接机制

mainwindow.h

public slots:
    void hello(int a);
signals:
    void mySignal(int b);//定义一个信号

mainwindow.cpp

#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this,SIGNAL(mySignal(int)),this,SLOT(hello(int)));//链接一般写在构造函数里
    emit mySignal(1001);
}

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

void MainWindow::hello(int a)
{
    qDebug()<<"hello a="<<a<<endl;
}
Paste_Image.png

信号与槽

使用系统默认槽操作

Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png

实现一个两位数的计算器

  • 图形界面的设计如下:


    Paste_Image.png
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

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_mark=0;
}

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

void MainWindow::on_pushButton_clicked()
{
    if(m_mark==0)
    {
        m_num1=1;
    }

    else
    {
        m_num2=1;
    }

    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton->text());
}

void MainWindow::on_pushButton_2_clicked()
{
    if(m_mark==0)
    {
        m_num1=2;
    }

    else
    {
        m_num2=2;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_2->text());
}

void MainWindow::on_pushButton_3_clicked()
{
    if(m_mark==0)
    {
        m_num1=3;
    }

    else
    {
        m_num2=3;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_3->text());

}

void MainWindow::on_pushButton_4_clicked()
{
    if(m_mark==0)
    {
        m_num1=4;
    }

    else
    {
        m_num2=4;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_4->text());
}

void MainWindow::on_pushButton_5_clicked()
{
    if(m_mark==0)
    {
        m_num1=5;
    }

    else
    {
        m_num2=5;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_5->text());
}

void MainWindow::on_pushButton_6_clicked()
{
    if(m_mark==0)
    {
        m_num1=6;
    }

    else
    {
        m_num2=6;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_6->text());
}

void MainWindow::on_pushButton_7_clicked()
{
    if(m_mark==0)
    {
        m_num1=7;
    }

    else
    {
        m_num2=7;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_7->text());
}

void MainWindow::on_pushButton_8_clicked()
{
    if(m_mark==0)
    {
        m_num1=8;
    }

    else
    {
        m_num2=8;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_8->text());
}

void MainWindow::on_pushButton_9_clicked()
{
    if(m_mark==0)
    {
        m_num1=9;
    }

    else
    {
        m_num2=9;
    }
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_9->text());
}

void MainWindow::on_pushButton_10_clicked()
{
    m_op='+';
    m_mark=1;
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_10->text());

}

void MainWindow::on_pushButton_11_clicked()
{
    m_op='-';
    m_mark=1;
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_11->text());
}

void MainWindow::on_pushButton_12_clicked()
{
    m_op='*';
    m_mark=1;
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_12->text());
}

void MainWindow::on_pushButton_13_clicked()
{
    m_op='/';
    m_mark=1;
    ui->lineEdit->setText(ui->lineEdit->text()+ui->pushButton_13->text());
}

void MainWindow::on_pushButton_14_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;
    }
    QString temp=QString::number(m_result,10);
    temp=ui->lineEdit->text()+ui->pushButton_14->text()+temp;
    ui->lineEdit->setText(temp);
    m_num1=0;
    m_num2=0;
    m_result=0;
    m_op='+';
    m_mark=0;
}

void MainWindow::on_pushButton_15_clicked()
{
    ui->lineEdit->clear();
}
Paste_Image.png Paste_Image.png Paste_Image.png Paste_Image.png

firstui

  • 学生信息管理系统
  • styleSheet:可以修改文件的颜色和大小,颜色前面要加上color,不同应用之间要用;隔开
Paste_Image.png Paste_Image.png

输入姓名与学号之后,点击ok按钮,则显示出来,并且保存到文件里

  • 在firstui.h中:
private slots:
void on_pushok_clicked();
  • 在firstui.cpp中
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<endl;
}
  • 则已在文件里保存了信息,如果student.txt前没有加路劲,则默认在别的路径下,不好找,最好加路径
Paste_Image.png
  • 性别可分为男或者女,这样就要弄可选择的按钮,即单选按钮Radio Button
  • 在firstui.cpp中
Paste_Image.png
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    QString sex=ui->sex->checkedButton()->text();//这里的sex是组名
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<" "<<sex<<endl;
}

  • 单选按钮man和female,我们可以把它们放在一个组里,如何选择组,选中两个单项按钮,然后右击,选择第二项,可以把组的名字改为sex.
  • 结果为如下:
Paste_Image.png
  • 在firstui.cpp中
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    QString sex=ui->sex->checkedButton()->text();
    QString age=ui->comboBox->currentText();
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<" "<<sex<<" "<<age<<endl;
}

  • 则运行结果为,可以选择适当的年龄:双击复选框,则进入编辑,调价多个项
Paste_Image.png Paste_Image.png

科目如上,与age相同

  • 在firstui.cpp中
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    QString sex=ui->sex->checkedButton()->text();
    QString age=ui->comboBox->currentText();
    QString college=ui->comboBox_2->currentText();
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<" "<<sex<<" "<<age<<" "<<college<<endl;
}

  • 结果为如下:
Paste_Image.png Paste_Image.png

兴趣爱好,会有许多的兴趣爱好,符合要求的都要勾上,则就要用到check Box

Paste_Image.png
  • 如果要多选的话,则就要把右下角interset组的exclusive的勾去掉,即去掉互斥性
Paste_Image.png
  • 则结果为:这里面用到了链表QList,则要包含头文件
#include <QDebug>//要显示line里面的内容要加的
#include <QTextStream>
#include <QFile>//存到文件需要加的
#include <QList>
#include <QAbstractButton>

  • 在firstui.cpp中:
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    QString sex=ui->sex->checkedButton()->text();
    QString age=ui->comboBox->currentText();
    QString college=ui->comboBox_2->currentText();
    QList<QAbstractButton*>ins=ui->interset->buttons();
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<" "<<sex<<" "<<age<<" "<<college;
    for(int i=0;i<ins.size();i++)
    {
        if(ins[i]->isChecked())
        {
           out<<" "<<ins[i]->text();
        }
    }
    out<<endl;
}

Paste_Image.png

在上一次输入完成之后,清空里面的内容

  • 在firstui.cpp中:
void firstui::on_pushok_clicked()
{
    QString name=ui->linename->text();
    QString id=ui->lineid->text();
    QString sex=ui->sex->checkedButton()->text();
    QString age=ui->comboBox->currentText();
    QString college=ui->comboBox_2->currentText();
    QList<QAbstractButton*>ins=ui->interset->buttons();
    qDebug()<<name<<" "<<id<<endl;
    QFile file("/home/jxb/1612/12/12.20/student.txt");
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
    {
        return;
    }
    QTextStream out(&file);
    out<<name<<" "<<id<<" "<<sex<<" "<<age<<" "<<college;
    for(int i=0;i<ins.size();i++)
    {
        if(ins[i]->isChecked())
        {
           out<<" "<<ins[i]->text();
           ins[i]->setChecked(false);
        }
    }
    out<<endl;
    file.close();
    ui->linename->clear();
    ui->lineid->clear();
    ui->comboBox->setCurrentIndex(0);
    ui->comboBox_2->setCurrentIndex(0);
    ui->sex->setExclusive(false);//
    ui->sex->checkedButton()->setChecked(false);//
    ui->sex->setExclusive(true);//这三句是清空sex的选项
}
void firstui::on_pushno_clicked()
{
    close();
}

  • 程序运行之前
  • 在程序运行之后
Paste_Image.png

如果姓名想要兼容中文,则在main.cpp中

#include "firstui.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec* codec=QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    firstui w;
    w.show();

    return a.exec();
}

  • 能成功编译过,并且能存入道文件中
Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读