QT作业3

1、思维导图

2、聊天界面

cpp 复制代码
//头文件1
#ifndef MYWIDGET_H
#define MYWIDGET_H


#include <QWidget>
#include<QIcon> //图标类
#include<QLabel> //标签类
#include<QMovie> //动图类
#include<QLineEdit> //行编辑器类
#include<QPushButton> //按钮类
#include <QDebug> //信息调试类
#include <QMessageBox>//对话框

class MyWidget : public QWidget
{
    Q_OBJECT
    QPushButton *btn;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
    QMovie *mv;
public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
signals:
    void jump();
public slots:
    void btn_clicked();
    void edit_changed();

};

#endif // MYWIDGET_H
cpp 复制代码
//头文件2
#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT

public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();
public slots:
    void on_jump();

private:
    Ui::Second *ui;
};

#endif // SECOND_H
cpp 复制代码
//源文件1
#include "mywidget.h"


MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(540,415);
    this->setFixedSize(540,415);
    this->setWindowTitle("盗版QQ");
    this->setWindowIcon(QIcon("D:\\yans\\color\\pictrue\\qq.png"));
    this->setStyleSheet("background-color:white");
    this->setWindowFlag(Qt::FramelessWindowHint);

    this->lab1 = new QLabel(this);
    lab1->resize(540, 160);
    lab1->setStyleSheet("background-color:pink");
    this->mv = new QMovie("D:\\yans\\color\\pictrue\\zz.gif");
    lab1->setMovie(mv);
    mv->start();
    lab1->setScaledContents(true);

    this->lab2 = new QLabel(this);
    lab2->resize(30,30);
    lab2->move(120,210);
    lab2->setPixmap(QPixmap("D:\\yans\\color\\pictrue\\wodepeizhenshi.png"));
    lab2->setScaledContents(true);

    this->lab3 = new QLabel(this);
    lab3->resize(30,30);
    lab3->move(120, 260);
    lab3->setPixmap(QPixmap("D:\\yans\\color\\pictrue\\passwd.jpg"));
    lab3->setScaledContents(true);

    this->edit1 = new QLineEdit(this);
    edit1->resize(275,30);
    edit1->move(155,210);
    edit1->setPlaceholderText("QQ号/手机号/邮箱");

    this->edit2 = new QLineEdit(this);
    edit2->resize(275,30);
    edit2->move(155,260);
    edit2->setPlaceholderText("密码");
    edit2->setEchoMode(QLineEdit::Password);

    this->btn = new QPushButton("登录",this);
    btn->resize(300,45);
    btn->move(120,345);
    //样式函数setStyleSheet()
    btn->setStyleSheet("background-color:yellow;border-radius:5px;color:white");

    QObject::connect(btn,&QPushButton::clicked,this,&MyWidget::btn_clicked);
    QObject::connect(edit1,&QLineEdit::textChanged,this,&MyWidget::edit_changed);
    QObject::connect(edit2,&QLineEdit::textChanged,this,&MyWidget::edit_changed);
}

MyWidget::~MyWidget()
{

}
void MyWidget::btn_clicked()
{
    if(this->edit1->text()=="123456"&&this->edit2->text()=="123456")
    {
    QMessageBox btn1(QMessageBox::Information,
                     "信息对话框",
                     "登录成功",
                     QMessageBox::Ok,
                     this
                      );
    int res = btn1.exec();
    emit jump();
    this->close();
    }
    else {
    QMessageBox btn2(QMessageBox::Information,
                     "错误对话框",
                     "登录失败,是否重新登录",
                     QMessageBox::Yes |  QMessageBox::No,
                     this
                      );
    int res1 = btn2.exec();
    if(res1== QMessageBox::Yes)
        {
            this->edit1->clear();
            this->edit2->clear();
        }
    else if(res1==QMessageBox::No)
         {
            this->close();
         }
    }
}
void MyWidget::edit_changed()
{
    if(this->edit1->text().length()>=5&&this->edit2->text().length()>=5)
    {
        this->btn->setStyleSheet("background-color:blue");
    }
    else
    {
        this->btn->setStyleSheet("background-color:yellow");
    }
}
cpp 复制代码
//源文件2
#include "second.h"
#include "ui_second.h"

Second::Second(QWidget *parent) :
    QWidget(parent),

    ui(new Ui::Second)
{
    ui->setupUi(this);
    this->resize(800,500);
    this->setFixedSize(800,500);

}
Second::~Second()
{
    delete ui;
}
void Second::on_jump()
{
    this->show();
}
cpp 复制代码
//源文件3
#include "mywidget.h"
#include "second.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    Second s;
    w.show();
    QObject::connect(&w,&MyWidget::jump,&s,&Second::on_jump);
    return a.exec();
}

3、完整文本编辑器

cpp 复制代码
头文件:
#ifndef WIDGET2_H
#define WIDGET2_H

#include <QWidget>
#include<QIcon> //图标类
#include<QLabel> //标签类
#include<QMovie> //动图类
#include<QLineEdit> //行编辑器类
#include<QPushButton> //按钮类
#include <QDebug> //信息调试类
#include <QMessageBox>//对话框
#include <QMessageBox>
#include <QFontDialog>
#include <QFont>
#include <QColorDialog>
#include <QColor>
#include <QFile>
#include <QFileDialog>
#include <QInputDialog>
#include <QTextEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget2; }
QT_END_NAMESPACE

class Widget2 : public QWidget
{
    Q_OBJECT
    QTextEdit *text;
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
    QPushButton *btn4;
    QPushButton *btn5;
    QPushButton *btn6;
public:
    Widget2(QWidget *parent = nullptr);
    ~Widget2();

private slots:
    void pushButton_clicked();
    void pushButton_2_clicked();
    void pushButton_3_clicked();
    void pushButton_4_clicked();
    void pushButton_5_clicked();
    void pushButton_6_clicked();
private:
    Ui::Widget2 *ui;
};
#endif // WIDGET2_H
cpp 复制代码
源文件:
#include "widget2.h"
#include "ui_widget2.h"

Widget2::Widget2(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget2)
{
    ui->setupUi(this);
    this->resize(800,600);
    this->setFixedSize(800,600);
    this->text=new QTextEdit(this);
    text->resize(481,111);
    text->move(20,200);
    this->btn1=new QPushButton("对话1",this);
    btn1->resize(93,28);
    btn1->move(30,150);
    this->btn2=new QPushButton("对话2",this);
    btn2->resize(93,28);
    btn2->move(380,150);
    this->btn3=new QPushButton("字体",this);
    btn3->resize(93,28);
    btn3->move(20,320);
    this->btn4=new QPushButton("颜色",this);
    btn4->resize(93,28);
    btn4->move(150,320);
    this->btn5=new QPushButton("文件选择",this);
    btn5->resize(93,28);
    btn5->move(280,320);
    this->btn6=new QPushButton("附件输入",this);
    btn6->resize(93,28);
    btn6->move(410,320);
    QObject::connect(btn1,&QPushButton::clicked,this,&Widget2::pushButton_clicked);
    QObject::connect(btn2,&QPushButton::clicked,this,&Widget2::pushButton_2_clicked);
    QObject::connect(btn3,&QPushButton::clicked,this,&Widget2::pushButton_3_clicked);
    QObject::connect(btn4,&QPushButton::clicked,this,&Widget2::pushButton_4_clicked);
    QObject::connect(btn5,&QPushButton::clicked,this,&Widget2::pushButton_5_clicked);
    QObject::connect(btn6,&QPushButton::clicked,this,&Widget2::pushButton_6_clicked);
}

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



void Widget2::pushButton_clicked()
{
    QMessageBox box(QMessageBox::Question,
                    "对话框",
                    "我是个帅哥吧",
                    QMessageBox::Yes | QMessageBox::No,
                    this);
    int res=box.exec();
    if(res==QMessageBox::Yes)
    {
        qDebug()<<"是";
    }else if(res==QMessageBox::No)
    {
        qDebug()<<"不是";
    }
}

void Widget2::pushButton_2_clicked()
{
    int btn=QMessageBox::warning(this,
                                 "警告",
                                 "别跑",
                                 QMessageBox::Ok | QMessageBox::No,
                                 QMessageBox::Ok);
    if(btn==QMessageBox::Ok)
    {
        qDebug()<<"就跑";
    }else if(btn==QMessageBox::No)
    {
        qDebug()<<"不跑";
    }
}

void Widget2::pushButton_3_clicked()
{
    bool ok=false;
    QFont f=QFontDialog::getFont(
                &ok,
                QFont("宋体",10,10,true),
                this,
                "选择字体"
                );
    if(ok)
    {
        this->text->setCurrentFont(f);
    }
}
void Widget2::pushButton_4_clicked()
{
    QColor c=QColorDialog::getColor(QColor("red"),
                                    this,
                                    "颜色选择");
    if(c.isValid()==true){
        this->text->setTextBackgroundColor(c);
    }
}

void Widget2::pushButton_5_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(

                this,
                "选择文件",
                "./",
                "all(*.*)::images(*.png *.jpg *.gif);;源文件(*.cpp)"
                );
    QFile file(fileName);

    if(file.open(QFile::ReadOnly)==false)
    {
        QMessageBox::information(this,"提示","文件打开失败");
        return;
    }
    QByteArray msg=file.readAll();
    this->text->setText(msg);
    file.close();
}

void Widget2::pushButton_6_clicked()
{
    bool ok=false;
    QString msg=QInputDialog::getText(this,
                                      "请输入文本",
                                      "姓名",
                                      QLineEdit::Normal,
                                      "理想",
                                      &ok);
    if(ok)
        {
            //使用输入的文本内容
            this->text->setText(msg);
        }
}
相关推荐
海阔天空任鸟飞~20 小时前
Linux 权限 777
linux·运维·服务器
程序喵大人21 小时前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里
开发语言·c++·stl
cui_ruicheng1 天前
Python从入门到实战(十六):多进程编程
开发语言·python
Kina_C1 天前
Linux iptables 防火墙原理与实操——从四表五链到 NAT 配置
linux·运维·服务器·iptables
wdfk_prog1 天前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构
linux·开发语言·面试·架构
晴空了无痕1 天前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
神明不懂浪漫1 天前
【第五章】Java中的继承与多态
java·开发语言
神州世通1 天前
解密企业通信安全防线:Avaya Aura 三层安全架构深度解析
开发语言·php
AC赳赳老秦1 天前
企业工商公开信息采集分析:OpenClaw 批量查询企业工商信息,生成企业画像报告
大数据·开发语言·python·自动化·php·deepseek·openclaw
qq_2518364571 天前
基于java Web 动漫视频网站毕业论文
java·开发语言·前端