Linux 学习记录48(QT篇)
本文目录
- Linux 学习记录48(QT篇)
- 一、
- 1.
- 2.
- 二、
- 三、
- 四、
- 练习
- 1. 自制文本编辑器
- (0. main.cpp
- (1. txt_window.h
- (2. txt_window.cpp
- 2. 登录界面完善
一、
1.
2.
二、
三、
四、
练习
1. 自制文本编辑器
(0. main.cpp
#include "txt_window.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Txt_Window w;w.show();return a.exec();
}
(1. txt_window.h
#ifndef TXT_WINDOW_H
#define TXT_WINDOW_H#include <QMainWindow>#include <QGridLayout>
#include <QString>
#include <QPushButton>
#include <QDebug>
#include <QLabel>
#include <QIcon>
#include <QMovie>
#include <QLineEdit>
#include <QFont>
#include <QFontDialog>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QImage>
#include <QColor>
#include <QColorDialog>
#include <QFile>
#include <QFileDialog>
#include <QAbstractButton>#include <QTextEdit>QT_BEGIN_NAMESPACE
namespace Ui { class Txt_Window; }
QT_END_NAMESPACEclass Txt_Window : public QMainWindow
{Q_OBJECTpublic:Txt_Window(QWidget *parent = nullptr);~Txt_Window();private slots:/*字体*/void font_t_signals(void);/*颜色*/void colour_t_signals(void);/*打开文件*/void open_f_signals(void);/*保存文件*/void save_f_signals(void);private:Ui::Txt_Window *ui;QPushButton *font_t;//字体QPushButton *colour_t;//颜色QPushButton *open_f;//打开文件QPushButton *save_f;//保存文件QTextEdit* text;
};
#endif // TXT_WINDOW_H
(2. txt_window.cpp
#include "txt_window.h"
#include "ui_txt_window.h"Txt_Window::Txt_Window(QWidget *parent): QMainWindow(parent), ui(new Ui::Txt_Window)
{ui->setupUi(this);//设置固定尺寸this->setFixedSize(600,360);this->setWindowTitle("Qt window");//当前界面尺寸//设置窗口图标this->setWindowIcon(QIcon(":/new/prefix1/Qt_w.png"));//设置窗口图标this->setMouseTracking(true);/***********************动图设置***********************/// 创建一个QMovie对象,并加载动态图文件QMovie* movie = new QMovie (":/new/prefix1/cre.gif");// 创建一个QLabel用于显示动态图QLabel* label = new QLabel (this);// 将QMovie与QLabel关联label->setMovie(movie);label->resize(600,360);label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);label->setScaledContents(true);movie->start();//开始播放动态图/***********************文本编辑器设置***********************/text = new QTextEdit (this);text->resize(550,250);text->move(25,10);text->setStyleSheet("border:none;border-radius:10px;background-color:rgba(105,105,105,200);color:#fff");// 设置样式表/***********************按钮设置***********************/int Button_size[2]={80,40};//登录/取消按键设置int Y = 290;int dt_X = 60;font_t = new QPushButton ("字体",this);colour_t = new QPushButton ("颜色",this);open_f = new QPushButton ("打开文件",this);save_f = new QPushButton ("保存文件",this);font_t->resize(Button_size[0],Button_size[1]);font_t->move(50+dt_X,Y);font_t->setStyleSheet("border:none;border-radius:10px;background-color:rgba(60,179,113);color:#fff");colour_t->resize(Button_size[0],Button_size[1]);colour_t->move(150+dt_X,Y);colour_t->setStyleSheet("border:none;border-radius:10px;background-color:rgba(60,179,113);color:#fff");open_f->resize(Button_size[0],Button_size[1]);open_f->move(250+dt_X,Y);open_f->setStyleSheet("border:none;border-radius:10px;background-color:rgba(60,179,113);color:#fff");save_f->resize(Button_size[0],Button_size[1]);save_f->move(350+dt_X,Y);save_f->setStyleSheet("border:none;border-radius:10px;background-color:rgba(60,179,113);color:#fff");connect(font_t,&QPushButton::clicked,this,&Txt_Window::font_t_signals);connect(colour_t,&QPushButton::clicked,this,&Txt_Window::colour_t_signals);connect(open_f,&QPushButton::clicked,this,&Txt_Window::open_f_signals);connect(save_f,&QPushButton::clicked,this,&Txt_Window::save_f_signals);
}/*字体*/
void Txt_Window::font_t_signals(void)
{bool ok;QFont font_t = QFontDialog::getFont(&ok,QFont("黑体",10,2,false),this,"选择字体");if(ok){/*选择了字体*/text->setCurrentFont(font_t);//设置当前选中的字体}else{/*未选择字体*/QMessageBox::warning(this,"font error!!",//"未选择字体",//QMessageBox::Ok | QMessageBox::Cancel);}
}/*颜色*/
void Txt_Window::colour_t_signals(void)
{/*颜色选择对话框*/QColor color_t = QColorDialog::getColor(QColor(100,100,100),this,"选择颜色");if(color_t.isValid()){/*选择颜色合法*/text->setTextColor(color_t);}else{/*未选择颜色/不合法*/QMessageBox::warning(this,"Color error!!",//"未选择颜色/不合法",//QMessageBox::Ok | QMessageBox::Cancel);}
}/*打开文件*/
void Txt_Window::open_f_signals(void)
{/*打开文件选择对话框*/QString filename = QFileDialog::getOpenFileName(this,"选择文件","./","所有文件(*.*);;头文件(*.h);;图片(*.png *.xpm *.jrp)");//过滤器QFile file(filename);if(!file.exists()){/*提示文件不存在*/QMessageBox::warning(this,"file error!!",//"文件不存在",//QMessageBox::Ok | QMessageBox::Cancel);return;}if(!file.open(QIODevice::ReadOnly)){/*打开失败*/QMessageBox::warning(this,"file error!!",//"打开失败",//QMessageBox::Ok | QMessageBox::Cancel);return;}QByteArray msg = file.readAll();this->text->setText(QString::fromLocal8Bit(msg));file.close();
}/*保存文件*/
void Txt_Window::save_f_signals(void)
{/*打开文件选择对话框*/QString filename = QFileDialog::getOpenFileName(this,"选择文件","./","所有文件(*.*);;头文件(*.h);;图片(*.png *.xpm *.jrp)");//过滤器QFile file(filename);if(!file.exists()){/*提示文件不存在*/QMessageBox::warning(this,"file error!!",//"文件不存在",//QMessageBox::Ok | QMessageBox::Cancel);return;}if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){/*打开失败*/QMessageBox::warning(this,"file error!!",//"打开失败",//QMessageBox::Ok | QMessageBox::Cancel);return;}QString text_buf = text->toPlainText();file.write(text_buf.toLocal8Bit());file.close();
}Txt_Window::~Txt_Window()
{delete ui;
}
2. 登录界面完善