• 2023/9/18 -- C++/QT


    作业

    完善登录框

    点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

    如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

    点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

    要求:消息对话框,对象版和静态成员函数版至少各实现一个

    【免费】2023/9/18-C++/QT实现简易登录资源-CSDN文库icon-default.png?t=N7T8https://download.csdn.net/download/weixin_54147737/88354416

    一、信号与槽机制

    1.1 简介

    1> 信号与槽机制,是qt引以为傲的、能够立于编程界而不倒的核心机制

    2> 作用:实现多个组件之间的互相通信,一个组件发信号,另一个组件接收信号后,做相应的响应

    3> 原理类似于IO中的两个进程间通过信号通信,但是,这里的信号,是可以传递信息的

    1.2 信号与槽

    1> 信号:信号函数,定义在类体内signals权限下的特殊成员函数,该函数是不完整的函数,只有声明,没有定义,返回值为void,参数就是传递的信息

    2> 槽:槽函数,定义在类体内slots权限下的成员函数,该函数是完整的函数,既有声明也有定义,返回值为void类型,参数就是接收信号的信息

    3> 槽函数可以被当作普通函数一样调用,但是普通函数不能当作槽函数一样连接信号

    4> 信号函数和槽函数,不是标准的C++代码,通过moc.exe工具将其自动转化为标准的C++代码

    5> 信号函数和槽函数可以是组件自带的,也可以由用户自定义,系统提供的每个组件都会自带信号与槽

    6> 包含信号与槽的类体定义

    1. class Widget : public QWidget
    2. {
    3. Q_OBJECT //有关信号与槽的元对象
    4. signals: //该权限下定义属于自己的信号
    5. void my_signal(); //自定义一个无参无返回值的信号函数
    6. private slots:
    7. void my_slot(); //自定义无参无返回值的槽函数
    8. public:
    9. Widget(QWidget *parent = nullptr);
    10. ~Widget();
    11. private:
    12. Ui::Widget *ui;
    13. };
    14. #endif // WIDGET_H

    1.3 信号与槽的连接

    1> 基于ui界面的,手动选中信号与槽的连接,该连接只适用于系统提供的信号与槽的连接,连接函数也是由系统实现

    2> 也是基于ui界面,在组件上右键转到槽,手动实现槽函数的内容,该方法只能更改槽函数逻辑,信号、连接都是由系统实现

    3> qt4版本手动连接信号与槽,该连接是不友好的连接,因为两个宏的原因,它会将所有传递的数据都改成字符串类型

    1. [static] QMetaObject::Connection //返回值:是建立的信号与槽的连接,是一个静态成员函数
    2. QObject::connect( //函数名
    3. const QObject *sender, //信号发射者,是组件的指针
    4. const char *signal, //要发射的信号函数,函数是函数指针,要的是字符串类型,所以需要使用SIGNAL()宏进行转换后使用
    5. const QObject *receiver, //信号接收者,是组件的指针
    6. const char *method) //接收信号后,接收者要处理的槽函数,函数是函数指针,要的是字符串类型,所以需要使用SLOT()宏进行转换后使用
    7. 举个例子:
    8. QLabel *label = new QLabel;
    9. QScrollBar *scrollBar = new QScrollBar;
    10. QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
    11. label, SLOT(setNum(int)));

    4> qt5版本的信号与槽的连接

    1. [static] QMetaObject::Connection //返回值:是建立的信号与槽的连接,是一个静态成员函数
    2. QObject::connect( //函数名
    3. const QObject *sender, //信号发射者,是组件的指针
    4. PointerToMemberFunction signal, //信号函数的指针,要指定是哪个类中的哪个成员函数
    5. const QObject *receiver, //信号接收者,是组件的指针
    6. PointerToMemberFunction method) //槽函数的指针,要指定是哪个类中的哪个成员函数
    7. 举个例子:
    8. QLabel *label = new QLabel;
    9. QLineEdit *lineEdit = new QLineEdit;
    10. QObject::connect(lineEdit, &QLineEdit::textChanged,
    11. label, &QLabel::setText);

    5> 信号连接功能函数

    1. [static] QMetaObject::Connection //返回值:是建立的信号与槽的连接,是一个静态成员函数
    2. QObject::connect( //函数名
    3. const QObject *sender, //信号发射者,是组件的指针
    4. PointerToMemberFunction signal, //信号函数的指针,要指定是哪个类中的哪个成员函数
    5. Functor functor) //功能函数:全局函数作为功能函数、仿函数作为功能函数、lambda表达式作为功能函数
    6. 举个例子1,全局函数作为功能函数:
    7. void someFunction();
    8. QPushButton *button = new QPushButton;
    9. QObject::connect(button, &QPushButton::clicked, someFunction);
    10. 举个例子2:lambda作为功能函数
    11. QByteArray page = ...;
    12. QTcpSocket *socket = new QTcpSocket;
    13. socket->connectToHost("qt-project.org", 80);
    14. QObject::connect(socket, &QTcpSocket::connected, [=] () {
    15. socket->write("GET " + page + "\r\n");
    16. });

    6> 信号与槽的断开

    只需将之前的连接函数的connect改成disconnect即可,参数不变

    1.4 信号的定义与发射

    1、在类体的signals权限下,定义属于自己的信号

    2、信号函数只有声明,没有定义,返回值为void,可用有参数

    3、在程序所需处,使用关键字emit发射自定义的信号,格式:emit 信号名(实参);

    4、信号函数可以连接到槽函数中,也可以连接另一个信号函数,表明发射第一个信号时,第二个信号跟着发射

    1.5 信号与槽的总结

    1> 一个信号可以连接多个槽函数

    2> 一个槽函数可以连接多个信号函数

    3> 一个信号函数可以连接到槽函数中,也可以连接另一个信号

    4> 信号函数和槽函数参数个数

    1. 1、信号函数和槽函数进行链接时,一般要求信号函数和槽函数的参数保持一致
    2. connect(信号发送者, SIGNAL(signalFun()),信号接收者, SLOT(slotFun())); //Ok
    3. connect(信号发送者, SIGNAL(signalFun(int)),信号接收者, SLOT(slotFun(int))); //Ok
    4. connect(信号发送者, SIGNAL(signalFun(int, char)),信号接收者, SLOT(slotFun(int, char))); //Ok
    5. connect(信号发送者, SIGNAL(signalFun(char, int)),信号接收者, SLOT(slotFun(int, char))); //False
    6. connect(信号发送者, SIGNAL(signalFun(int)),信号接收者, SLOT(slotFun(char))); //False
    7. 2、当信号函数的参数大于槽函数的参数时
    8. connect(信号发送者, SIGNAL(signalFun(int, char)),信号接收者, SLOT(slotFun())); //Ok
    9. connect(信号发送者, SIGNAL(signalFun(int, char)),信号接收者, SLOT(slotFun(int))); //Ok
    10. connect(信号发送者, SIGNAL(signalFun(int, char)),信号接收者, SLOT(slotFun(char))); //False
    11. 3、当信号函数的参数小于槽函数的参数时
    12. connect(信号发送者, SIGNAL(signalFun(int)),信号接收者, SLOT(slotFun(int, char))); //False
    13. connect(信号发送者, SIGNAL(signalFun(int)),信号接收者, SLOT(slotFun(int, char=0))); //Ok

    1.6 使用信号与槽实现简单的页面跳转

    1> 准备两个界面

    2> 在第一个界面中,定义跳转的信号函数,第二个界面定义槽函数

    3> 可以在主程序中实例化两个界面,第一个界面调用show函数,第二个不调用,并且在主调函数中将第一个界面的信号与第二个界面的槽函数连接

    1) 头文件

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include"second.h"
    5. QT_BEGIN_NAMESPACE
    6. namespace Ui { class Widget; }
    7. QT_END_NAMESPACE
    8. class Widget : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. Widget(QWidget *parent = nullptr);
    13. ~Widget();
    14. signals:
    15. void jump(); //自定义跳转信号函数
    16. private slots:
    17. void on_loginBtn_clicked();
    18. private:
    19. Ui::Widget *ui;
    20. Second *s1; //定义另一个界面的指针
    21. };
    22. #endif // WIDGET_H

    second.h

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. namespace Ui {
    5. class Second;
    6. }
    7. class Second : public QWidget
    8. {
    9. Q_OBJECT
    10. public slots:
    11. void jump_slot(); //接收跳转信号的槽函数
    12. public:
    13. explicit Second(QWidget *parent = nullptr);
    14. ~Second();
    15. private:
    16. Ui::Second *ui;
    17. };
    18. #endif // SECOND_H

    2) 源文件

    widget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. s1 = new Second; //给另一个界面实例化空间
    9. //将当前界面的信号,与s1界面的槽函数进行连接
    10. connect(this,&Widget::jump, s1, &Second::jump_slot);
    11. }
    12. Widget::~Widget()
    13. {
    14. delete ui;
    15. }
    16. void Widget::on_loginBtn_clicked()
    17. {
    18. emit jump();
    19. this->hide(); //将当前界面隐藏
    20. }

    second.cpp

    1. #include "second.h"
    2. #include "ui_second.h"
    3. Second::Second(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Second)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Second::~Second()
    10. {
    11. delete ui;
    12. }
    13. //接收跳转信号对应的槽函数
    14. void Second::jump_slot()
    15. {
    16. this->show(); //将自己界面进行展示
    17. }

    3) 主程序

    1. #include "widget.h"
    2. #include"second.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9. //Second s; //定义第二个界面
    10. //连接w的信号与s的槽
    11. //QObject::connect(&w, &Widget::jump, &s, &Second::jump_slot);
    12. return a.exec();
    13. }

    二、对话框

    2.1 消息对话框(QMessageBox)

    1> 该类提供的消息对话框,用于提高用户的交互性,可以提供问题对话框、信息对话框、警告对话框、错误对话框。。。

    2> 实现该对话框的方式有两种:基于属性版本、静态成员函数版本

    3> 基于属性版本实现消息对话框:公共该类实例化对象,调用exec函数进入执行态,该函数返回值时用户点击的按钮

    1. QMessageBox::QMessageBox( //构造函数函数名
    2. QMessageBox::Icon icon, //图标
    3. const QString &title, //对话框标题
    4. const QString &text, //对话框提示文本内容
    5. QMessageBox::StandardButtons buttons = NoButton, //对话框提供的按钮,如果有多个按钮,则使用位或隔开
    6. QWidget *parent = nullptr) //父组件
    7. 参数1介绍,对话框提供的图标,该类中的枚举值
    8. Constant
    9. Value
    10. Description
    11. QMessageBox::NoIcon
    12. 0
    13. the message box does not have any icon.//不提供任何图标
    14. QMessageBox::Question
    15. 4
    16. an icon indicating that the message is asking a question.//提供一个问号图标
    17. QMessageBox::Information
    18. 1
    19. an icon indicating that the message is nothing out of the ordinary.//提供一个i符号图标
    20. QMessageBox::Warning
    21. 2
    22. an icon indicating that the message is a warning, but can be dealt with.//提供一个感叹号图标
    23. QMessageBox::Critical
    24. 3
    25. an icon indicating that the message represents a critical problem. //提供一个叉号图标
    26. 参数4介绍,对话框上提供的标准按钮,时类中的枚举值,如果有多个按钮,中间有位或隔开
    27. Constant
    28. Value
    29. Description
    30. QMessageBox::Ok
    31. 0x00000400
    32. An "OK" button defined with the AcceptRole. //提供一个ok按钮
    33. QMessageBox::Open
    34. 0x00002000
    35. An "Open" button defined with the AcceptRole. //提供一个open按钮
    36. QMessageBox::Save
    37. 0x00000800
    38. A "Save" button defined with the AcceptRole. //提供一个保存按钮
    39. QMessageBox::Cancel
    40. 0x00400000
    41. A "Cancel" button defined with the RejectRole. //提供一个取消按钮
    42. QMessageBox::Close
    43. 0x00200000
    44. A "Close" button defined with the RejectRole. //提供一个关闭按钮
    45. QMessageBox::Discard
    46. 0x00800000
    47. A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.//不保存
    48. 举个例子:
    49. QMessageBox msgBox; //实例化对象
    50. msgBox.setText("The document has been modified.");
    51. msgBox.setInformativeText("Do you want to save your changes?");
    52. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    53. msgBox.setDefaultButton(QMessageBox::Save);
    54. int ret = msgBox.exec(); //展示对话框
    55. //判断用户点击的结果
    56. switch (ret) {
    57. case QMessageBox::Save:
    58. // Save was clicked
    59. break;
    60. case QMessageBox::Discard:
    61. // Don't Save was clicked
    62. break;
    63. case QMessageBox::Cancel:
    64. // Cancel was clicked
    65. break;
    66. default:
    67. // should never be reached
    68. break;
    69. }

    4> 基于静态成员函数版本:无需实例化对象,但是只能设置有限的信息

    1. [static] QMessageBox::StandardButton //函数返回值类型,是用户点击的标准按钮,该函数是静态成员函数
    2. QMessageBox::warning( //函数名
    3. QWidget *parent, //父组件
    4. const QString &title, //对话框标题
    5. const QString &text, //对话框文本内容
    6. QMessageBox::StandardButtons buttons = Ok, //提供的标准按钮,多个按钮用位或隔开
    7. QMessageBox::StandardButton defaultButton = NoButton) //默认选中的按钮
    8. 举个例子:
    9. int ret = QMessageBox::warning(this, tr("My Application"),
    10. tr("The document has been modified.\n"
    11. "Do you want to save your changes?"),
    12. QMessageBox::Save | QMessageBox::Discard
    13. | QMessageBox::Cancel,
    14. QMessageBox::Save);
    15. 其余的静态成员函数
    16. QMessageBox::StandardButton
    17. critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
    18. QMessageBox::StandardButton
    19. information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
    20. QMessageBox::StandardButton
    21. question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)

    5> 案例

    1) 头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //消息对话框类
    5. #include
    6. QT_BEGIN_NAMESPACE
    7. namespace Ui { class Widget; }
    8. QT_END_NAMESPACE
    9. class Widget : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. Widget(QWidget *parent = nullptr);
    14. ~Widget();
    15. private slots:
    16. void on_quesBtn_clicked();
    17. void on_warnBtn_clicked();
    18. private:
    19. Ui::Widget *ui;
    20. };
    21. #endif // WIDGET_H

    2) 源文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //问题按钮对应的槽函数
    14. void Widget::on_quesBtn_clicked()
    15. {
    16. //1、调用构造函数实例化对象
    17. QMessageBox box(QMessageBox::Question, //图标
    18. "问题", //对话框标题
    19. "晚上约不?", //对话框文本内容
    20. QMessageBox::Yes | QMessageBox::No, //提供的按钮
    21. this); //父组件
    22. box.setDetailedText("有上好的飞天茅台"); //提供详细文本内容
    23. box.setDefaultButton(QMessageBox::No); //将no设置成默认按钮
    24. box.setButtonText(QMessageBox::Yes, "好的");
    25. box.setButtonText(QMessageBox::No, "没空");
    26. //2、调用exec函数运行对话框
    27. int ret = box.exec();
    28. //3、对结果进行判断
    29. if(ret == QMessageBox::Yes)
    30. {
    31. qDebug()<<"木的问题,老地方见";
    32. }else if(ret == QMessageBox::No)
    33. {
    34. qDebug()<<"下次一定";
    35. }
    36. }
    37. //警告按钮对应的槽函数
    38. void Widget::on_warnBtn_clicked()
    39. {
    40. //直接调用静态成员函数完成对话框的实现
    41. int ret = QMessageBox::warning(this, //父组件
    42. "警告", //对话框标题
    43. "放学等着,别走", //对话框文本内容
    44. QMessageBox::Yes|QMessageBox::No, //对话题提供的按钮
    45. QMessageBox::No); //默认选中的按钮
    46. //对用户选中的按钮进行判断
    47. if(ret == QMessageBox::Yes)
    48. {
    49. qDebug()<<"行,荤的还是素的,文的还是武的任你挑";
    50. }else if(ret == QMessageBox::No)
    51. {
    52. qDebug()<<"你永远是我大哥";
    53. }
    54. }

    2.2 字体对话框、颜色对话框、文件对话框

    1> 字体对话框(QFontDialog)

    1、该类提供了一个能够让用户选择字体的对话框

    2、可以使用该类提供的静态成员函数getFont获取字体对话框

    3、所需类:QFontDialog、QFont

    1. [static] QFont //函数返回值,返回用户选中的字体,该函数是一个静态成员函数
    2. QFontDialog::getFont( //函数名
    3. bool *ok, //返回用户是否选中字体,如果选中则结果为真,否则结果为假
    4. const QFont &initial, //字体对话框的初始字体
    5. QWidget *parent = nullptr, //父组件
    6. const QString &title = QString()) //对话框标题
    7. 举个例子:
    8. bool ok;
    9. QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
    10. if (ok) {
    11. // font is set to the font the user selected
    12. } else {
    13. // the user canceled the dialog; font is set to the initial
    14. // value, in this case Times, 12.
    15. }

    2> 颜色对话框(QColorDialog)

    1、该类提供了一个颜色对话框,让用户选择使用的颜色

    2、使用该类提供的一个静态成员函数getColor可以调出系统提供的颜色对话框

    3、所用类:QColorDialog、QColor

    1. [static] QColor //返回值类型,是用户选中的颜色,是一个静态成员函数
    2. QColorDialog::getColor( //函数名
    3. const QColor &initial = Qt::white, //初始颜色
    4. QWidget *parent = nullptr, //父组件
    5. const QString &title = QString()) //对话框标题
    6. 举个例子:
    7. QColor c = QColorDialog::getColor(QColor("green"), //初始颜色
    8. this, //父组件
    9. "选择颜色"); //对话框标题
    10. //判断c的合法性
    11. if(c.isValid())
    12. {
    13. //用户选择的颜色
    14. //将用户选择的颜色作用到文本上
    15. //ui->textEdit->setTextColor(c); //设置字体颜色
    16. ui->textEdit->setTextBackgroundColor(c); //设置背景色
    17. }else
    18. {
    19. QMessageBox::information(this, "取消","用户取消了选择颜色");
    20. }

    3> 文件对话框(QFileDialog)

    1、该类提供一个文件对话框,让用户选择文件或目录

    2、文件对话框得到一个文件的路径

    3、使用静态成员函数getOpenFileName()或getSaveFileName(),调取系统提供的文件对话框

    1. [static] QString //返回值类型,用户选中的文件的路径
    2. QFileDialog::getOpenFileName( //函数名
    3. QWidget *parent = nullptr, //父组件
    4. const QString &caption = QString(), //标题
    5. const QString &dir = QString(), //起始路径
    6. const QString &filter = QString()) //过滤器
    7. 过滤器定义格式: "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
    8. 举个例子:
    9. QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
    10. "/home",
    11. tr("Images (*.png *.xpm *.jpg)"));
    12. 获取保存文件对话框
    13. [static] QString
    14. QFileDialog::getSaveFileName(
    15. QWidget *parent = nullptr,
    16. const QString &caption = QString(),
    17. const QString &dir = QString(),
    18. const QString &filter = QString())

    4> 文件读写

    1、在C++中,对文件进行操作,需要依赖于文件对象(QFile)

    2、流程:使用该类实例化一个对象,通过类对象调用成员函数,构造函数、setFileName()设置要操作的文件、exists()判断文件是否存在、remove()删除文件、open()打开文件,close()关闭文件, read(), readLine(), readAll(), write().读写操作,atEnd()判断是否到文件结尾

    3> 举个例子

    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4. while (!file.atEnd()) {
    5. QByteArray line = file.readLine();
    6. process_line(line);
    7. }

    5> 案例

    1、ui界面

     

    三、加载资源文件

    1> 将所需的资源文件夹放入工程项目所在路径下

    2> 在工程文件中,右键添加新文件

    3> 选择Qt --》资源文件

    4> 添加前缀

    5> 添加文件

    6> 点下锤子即可

    2、头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //字体对话框
    5. #include //字体类
    6. #include //消息对话框
    7. #include //信息调试类
    8. #include //颜色对话框
    9. #include //颜色类
    10. #include //文件对话框类
    11. #include //文件头文件
    12. QT_BEGIN_NAMESPACE
    13. namespace Ui { class Widget; }
    14. QT_END_NAMESPACE
    15. class Widget : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. Widget(QWidget *parent = nullptr);
    20. ~Widget();
    21. private slots:
    22. void on_fontBtn_clicked();
    23. void on_colorBtn_clicked();
    24. void on_openBtn_clicked();
    25. private:
    26. Ui::Widget *ui;
    27. };
    28. #endif // WIDGET_H

    3、源文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Widget::~Widget()
    10. {
    11. delete ui;
    12. }
    13. //字体按钮对应的槽函数
    14. void Widget::on_fontBtn_clicked()
    15. {
    16. bool ok; //返回用户是否选中字体
    17. //直接调用getFont获取一个字体对话框
    18. QFont f = QFontDialog::getFont(&ok, //返回是否选择字体
    19. QFont("隶书", 10, 10,false), //初始字体
    20. this, //父组件
    21. "选择字体"); //对话框标题
    22. //对ok进行判断,判断用户是否选中字体了
    23. if(ok)
    24. {
    25. //用户选中字体了。可以使用该字体
    26. //将选中的字体,设置到文本文字上
    27. //ui->textEdit->setFont(f); //设置全部文字字体
    28. ui->textEdit->setCurrentFont(f); //设置选中的字体
    29. //ui->textEdit->setFontItalic(true); //设置选中字体倾斜
    30. }else
    31. {
    32. //用户取消了选中字体
    33. QMessageBox::information(this, "取消", "用户取消的选择字体");
    34. }
    35. }
    36. //颜色按钮对应的槽函数
    37. void Widget::on_colorBtn_clicked()
    38. {
    39. QColor c = QColorDialog::getColor(QColor("green"), //初始颜色
    40. this, //父组件
    41. "选择颜色"); //对话框标题
    42. //判断c的合法性
    43. if(c.isValid())
    44. {
    45. //用户选择的颜色
    46. //将用户选择的颜色作用到文本上
    47. //ui->textEdit->setTextColor(c); //设置字体颜色
    48. ui->textEdit->setTextBackgroundColor(c); //设置背景色
    49. }else
    50. {
    51. QMessageBox::information(this, "取消","用户取消了选择颜色");
    52. }
    53. }
    54. //打开按钮对应的槽函数
    55. void Widget::on_openBtn_clicked()
    56. {
    57. QString fileName = QFileDialog::getOpenFileName(this, //父组件
    58. "选择文件", //对话框标题
    59. "./", //起始路径
    60. "All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"); //过滤器
    61. //判断是否选中文件
    62. if(fileName.isNull())
    63. {
    64. QMessageBox::information(this,"提示","用户取消了选择文件");
    65. return;
    66. }
    67. qDebug()<//得到文件路径
    68. //文件操作
    69. //1、实例化一个文件对象
    70. QFile file(fileName);
    71. //2、打开文件
    72. if(!file.isOpen()) //如果该文件没有被打开,则打开文件
    73. {
    74. //调用打开文件操作
    75. if(!file.open(QFile::ReadWrite))
    76. {
    77. QMessageBox::critical(this, "失败","文件打开失败");
    78. return; //文件打开失败
    79. }
    80. }
    81. //3、读写操作
    82. QByteArray msg = file.readAll();
    83. //4、关闭文件
    84. file.close();
    85. //将读取的文本展示在ui界面
    86. ui->textEdit->setText(msg);
    87. }

    四、发布软件

    1> 配置环境变量,将qt安装路径的bin目录放入系统环境变量中

    2> 在编译器中,以release的方式运行一下自己的程序

    3> 将影子目录中的release文件夹下的可执行文件进行复制到一个新的文件夹下

    4> 在新的文件夹的空白处,按着shift + 右击 :在此处打开powershell窗口

    5> 输入如下指令后回车

    6> 此时就已经成功发布了,可以将该文件夹打包发给别人,别人下载后,解压运行可执行程序即可

  • 相关阅读:
    servlet介绍
    保护环GuardRing(基于IC617)
    REGEXP函数正则表达式
    Linux教程:如何安装redis服务并搭建三主三从集群部署环境
    集合判空CollectionUtils.isEmpty()
    仪表板支持水印设置,数据集新增脱敏规则支持,DataEase开源数据可视化分析平台v1.17.0发布
    MyBatis多条件查询、动态SQL、多表操作、注解开发详细教程
    16-CSS3
    一种超轻量级神经网络加速器实现
    Adaptive autosar DM 诊断管理
  • 原文地址:https://blog.csdn.net/weixin_54147737/article/details/132995909