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);
        }
}
相关推荐
夜夜敲码1 分钟前
C语言教程(十六): C 语言字符串详解
c语言·开发语言
宋康8 分钟前
C语言结构体和union内存对齐
c语言·开发语言
꧁坚持很酷꧂14 分钟前
Linux Ubuntu18.04下安装Qt Craeator 5.12.9(图文详解)
linux·运维·qt
居然是阿宋18 分钟前
Kotlin高阶函数 vs Lambda表达式:关键区别与协作关系
android·开发语言·kotlin
ChoSeitaku28 分钟前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
凉、介31 分钟前
PCI 总线学习笔记(五)
android·linux·笔记·学习·pcie·pci
Cao1234567893211 小时前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine1 小时前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
电鱼智能的电小鱼1 小时前
EFISH-SBC-RK3588无人机地面基准站项目
linux·网络·嵌入式硬件·机器人·无人机·边缘计算
亿坊电商1 小时前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php