QT 4-6

 代码

mywiget源码

#include "mywidget.h"
#include "ui_mywidget.h"

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::myWidget)
{
    ui->setupUi(this);
    //535 442
    //设置窗口的尺寸
    this->setFixedSize(535,442);
    //获取窗口标题
    qDebug()<<this->windowTitle();
    //设置窗口的标题
    this->setWindowTitle("登录界面");
    //设置窗口图标
    this->setWindowIcon(QIcon("C:/Users/24121/Desktop/QQ.jpg"));
    //  this->setStyleSheet("background-color: rgb(234, 198, 0)");
    //更改按钮的图标
    ui->btn1->setIcon(QIcon("C:/Users/24121/Desktop/icon/login.png"));
    ui->btn2->setIcon(QIcon("C:/Users/24121/Desktop/icon/cancel.png"));
    //更改label的图标
    ui->lab1->setPixmap(QPixmap(":/userName.jpg"));
    ui->lab1->setScaledContents(true);
    ui->lab2->setPixmap(QPixmap(":/passwd.jpg"));
    ui->lab2->setScaledContents(true);
    ui->lab3->setPixmap(QPixmap(":/logo.png"));
    ui->lab3->setScaledContents(true);
    connect(ui->btn1,&QPushButton::clicked,this,&myWidget::on_clicked_btn1);
    connect(ui->btn2,&QPushButton::clicked,this,&myWidget::on_clicked_btn2);
    connect(this, &myWidget::mysignal, [](QString e){
        qDebug()<<e;
    });



}

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


void myWidget::on_clicked_btn1()
{

    if(ui->lin1->text()=="admin")
    {
        if(ui->lin2->text()=="123456")
        {
            int ret=QMessageBox::information(this,"信息","登录成功",QMessageBox::Ok);
            if(QMessageBox::Ok==ret)
            {
                close();
            }
            qDebug()<<"登录成功";
              emit mysignal("hello word");

        }
        else
        {
            int ret=QMessageBox::information(this,"信息","账号密码不匹配,是否重新登录",QMessageBox::Ok|QMessageBox::Cancel);
            if(QMessageBox::Ok==ret)
            {
                ui->lin2->clear();
            }else if(QMessageBox::Cancel==ret)
            {
                close();
            }

        }
    }
    else
    {
        QMessageBox box(QMessageBox::Information,"信息","账户输入错误,是否重新输入",QMessageBox::Yes|QMessageBox::No,this);
        int rets=box.exec();
        if(QMessageBox::Yes==rets)
        {
            ui->lin1->clear();
        }else if(QMessageBox::No==rets)
        {
            close();
        }

        qDebug()<<"账户输入错误";
    }
}




void myWidget::on_clicked_btn2()
{
    int ret=QMessageBox::information(this,"信息","是否确定要退出登录",QMessageBox::Yes|QMessageBox::No);
    if(QMessageBox::Yes==ret)
    {
        close();
    }else if(QMessageBox::No==ret)
    {
        QMessageBox box(QMessageBox::Information,"信息","是否重新输入",QMessageBox::Yes|QMessageBox::No,this);
        int rets=box.exec();
        if(QMessageBox::Yes==rets)
        {
            ui->lin1->clear();
            ui->lin2->clear();
        }else if(QMessageBox::No==rets)
        {
            close();
        }

    }

 //emit mysignal("hello world");
}

头文件

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include<QIcon>
#include<QDebug>
#include<QPushButton>
#include<cstring>
#include<QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE

class myWidget : public QWidget
{
    Q_OBJECT
    
    
signals:
    void mysignal(QString e);
    void mysignal1();
    
public:
    myWidget(QWidget *parent = nullptr);
    ~myWidget();
    
private slots:
    
    void on_clicked_btn1();
    
    void on_clicked_btn2();
    
private:
    Ui::myWidget *ui;
};
#endif // MYWIDGET_H

form的源码

#include "form.h"
#include "ui_form.h"


Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    //设置窗口的尺寸
    this->setFixedSize(789,653);
    
    //设置窗口的标题
    this->setWindowTitle("聊天界面");
    //设置窗口图标
    this->setWindowIcon(QIcon("C:/Users/24121/Desktop/qw.jpg"));
    
}

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

void Form::myslot(QString w)
{
    this->show();
    
}


//字体函数对应的槽函数
void Form::on_btn1_clicked()
{
    bool ok=false;   //返回用户是否选中字体
    QFont myfont = QFontDialog::getFont(&ok,                        //判断字体是否被选中
                                        QFont("楷体"),               //初始字体
                                        this,                       //父组件
                                        "字体"                       //对话框标题
                                        );
    //将选中的字体设置到文本内
    if(ok)
    {
        // ui->text1->setFont(myfont);   //全部的文本改变字体
        ui->Form::text1->setCurrentFont(myfont);//选中的文本改变字体
    }
}
//颜色函数对应的槽函数
void Form::on_btn2_clicked()
{
    
    
    //获取一个颜色
    QColor c=QColorDialog::getColor(Qt::white,this,"字体");
    //将选中的颜色设置到字体上
    // ui->text1->setTextColor(c);  // 设置字体颜色
    ui->Form::text1->setTextBackgroundColor(c); //设置字体背景色
}

void Form::on_btn3_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this,
                                                  "选择文件",
                                                  "./",
                                                  "all(*.*);;cpp(*.cpp);;imag(*.png,*jpg,*bmp)");
    qDebug()<<fileName;
    //文件操作
    //1、实例化一个文件对象
    QFile f(fileName);
    //2、判断文件是否存在
    if(!f.exists())
    {
        QMessageBox::information(this,"信息","文件不存在");
    }
    //打开文件
    if(f.open(QFile::ReadWrite)==false)
    {
        QMessageBox::information(this,"信息","文件不存在");
    }
    
    
    //3、将文件中的内容读取出来: readall
    QByteArray msg=f.readAll();  //将文件中的所有内容读取出来
    
    QString m= QString::fromLocal8Bit(msg);   //将字节数转换成字符串
    
    
    //4、关闭文件
    f.close();
    
    //将获取的文本信息,展示出来
    ui->Form::text1->setText(m);
}

void Form::on_btn4_clicked()
{
    QString fileName=QFileDialog::getSaveFileName(this,
                                                  "选择文件",
                                                  "./",
                                                  "all(*.*);;cpp(*.cpp);;imag(*.png,*jpg,*bmp)");
    qDebug()<<fileName;
    //文件操作
    //1、实例化一个文件对象
    QFile f(fileName);
    //2、判断文件是否存在
    if(!f.exists())
    {
        QMessageBox::information(this,"信息","文件不存在");
    }
    //打开文件
    if(f.open(QFile::ReadWrite)==false)
    {
        QMessageBox::information(this,"信息","文件不存在");
    }
    //获取ui界面上的文本内容
    
    QString msg = ui->text1->toPlainText();
    //3、将内容写到文件中: write
    f.write(msg.toLocal8Bit());
    //4、关闭文件
    f.close();
}

头文件

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include<QIcon>
#include<QFont>
#include<QFontDialog>
#include<QColor>
#include<QColorDialog>
#include<QFile>
#include<QFileDialog>
#include<QString>
#include<QDebug>
#include<QMessageBox>
namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT
    
public slots:
    void myslot(QString e);
    
public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();
    
private slots:
    
    
    void on_btn1_clicked();
    
    void on_btn2_clicked();
    
    void on_btn3_clicked();
    
    void on_btn4_clicked();
    
private:
    Ui::Form *ui;
};

#endif // FORM_H

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值