1、思维导图
2、聊天界面
//头文件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_OBJECTQPushButton *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
//头文件2
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class Second;
}class Second : public QWidget
{Q_OBJECTpublic:explicit Second(QWidget *parent = nullptr);~Second();
public slots:void on_jump();private:Ui::Second *ui;
};#endif // SECOND_H
//源文件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");}
}
//源文件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();
}
//源文件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、完整文本编辑器
头文件:
#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_NAMESPACEclass Widget2 : public QWidget
{Q_OBJECTQTextEdit *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
源文件:
#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);}
}