• qt day3


    字体对话框(QFontDialog)、颜色对话框(QColorDialog)、文件对话框(QFileDialog)

    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. void Widget::on_text_clicked()
    14. {
    15. //调用QFontDialog类中的静态成员函数,getFont函数来调取系统提供的字体对话框
    16. bool ok;//用于接收用户是否选择字体
    17. QFont f=QFontDialog::getFont(&ok,//返回是否选中字体
    18. QFont("隶书",10,10,false),//初始字体,大小,宽,下划线
    19. this, //父组件
    20. "选择字体");//对话框标题
    21. //将选中的字体进行使用
    22. if(ok)
    23. {
    24. //选中字体,将字体设置到文本上
    25. //ui->textEdit->setFont(f);//全换
    26. ui->textEdit->setCurrentFont(f);
    27. }else
    28. {
    29. //没有选中
    30. QMessageBox ::information(this,"提示","您取消了选择字体");
    31. }
    32. }
    33. void Widget::on_color_clicked()
    34. {
    35. //可以使用QColorDialog类中的静态成员函数getcolor来调取颜色对话框
    36. QColor c=QColorDialog::getColor(QColor("pink"),//初始颜色
    37. this,//父组件
    38. "选择颜色");//对话框标题
    39. //对选中的颜色判断合法性
    40. if(c.isValid())
    41. {
    42. //颜色合法,直接用即可
    43. ui->textEdit->setTextColor(c); //字体颜色
    44. //ui->textEdit->setTextBackgroundColor(c); //背景色
    45. }
    46. else
    47. {
    48. //颜色不合法
    49. QMessageBox::information(this,"提示","您取消了选择颜色");
    50. }
    51. }
    52. //打开按钮
    53. void Widget::on_open_clicked()
    54. {
    55. ///调用QFileDialog类中的静态成员函数,getOpenFileName来获取文件路径
    56. QString filename=QFileDialog::getOpenFileName(this, //父组件
    57. "选择文件",
    58. "./", //起始路径
    59. "Image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");//过滤器
    60. //判断是否选中文件
    61. if(filename.isNull())
    62. {
    63. QMessageBox::information(this,"提示","您取消了选择文件");
    64. return ;
    65. }
    66. //输出文件路径s
    67. qDebug()<
    68. //1.实例化一个文件对象
    69. QFile file(filename); //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象
    70. //2.判断文件是否存在
    71. if(!file.exists())
    72. {
    73. return;
    74. }
    75. //3.打开文件
    76. if(!file.open(QFile::ReadWrite))
    77. {
    78. return;
    79. }
    80. //4.读取文件中的内容
    81. QByteArray msg =file.readAll();
    82. //将内容展示到ui界面
    83. ui->textEdit->setText(QString::fromLocal8Bit(msg));
    84. //获取文本编辑器中的内容
    85. //ui->textEdit->toPlainText();
    86. //5.关闭文件
    87. file.close();
    88. }
    89. void Widget::on_save_clicked()
    90. {
    91. ///调用QFileDialog类中的静态成员函数,getOpenFileName来获取文件路径
    92. QString filename=QFileDialog::getSaveFileName(this, //父组件
    93. "保存",
    94. "./", //起始路径
    95. "Image File(*.png *.jpg *.bmp);;Text File(*.txt);;All(*.*)");//过滤器
    96. //判断是否选中文件
    97. if(filename.isNull())
    98. {
    99. QMessageBox::information(this,"提示","您取消了选择文件");
    100. return ;
    101. }
    102. //输出文件路径s
    103. qDebug()<
    104. //1.实例化一个文件对象
    105. QFile file(filename); //使用获取到的文件路径,实例化一个文件对象,后期对文件的操作都是基于该对象
    106. //2.判断文件是否存在
    107. if(!file.exists())
    108. {
    109. return;
    110. }
    111. //3.打开文件
    112. if(!file.open(QFile::ReadWrite|QFile::Truncate))
    113. {
    114. return;
    115. }
    116. // //4.读取文件中的内容
    117. // QByteArray msg =file.readAll();
    118. // //将内容展示到ui界面
    119. // ui->textEdit->setText(QString::fromLocal8Bit(msg));
    120. //获取文本编辑器中的内容
    121. QString msg=ui->textEdit->toPlainText();
    122. file.write(msg.toUtf8());
    123. //5.关闭文件
    124. file.close();
    125. }

    消息对话框

    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. void Widget::on_pushButton_clicked()
    14. {
    15. }
    16. void Widget::on_pushButton_3_clicked()
    17. {
    18. //使用QMessageBox实例化一个对象
    19. QMessageBox box(QMessageBox::Information, // 图标
    20. "信息", //对话框标题
    21. "zzzz", //对话框提示信息
    22. QMessageBox::Yes|QMessageBox::No,//对话框的提供的按钮
    23. this); //父组件
    24. box.setDefaultButton (QMessageBox::No);//将no设置成默认按钮
    25. //box.setDetailedText("啦啦啦拉拉阿拉啦");//提示
    26. //执行对话框
    27. int ret = box.exec();
    28. //对用户点击的按钮进行判断
    29. if(ret == QMessageBox::Yes)
    30. qDebug() << "ok";
    31. else
    32. qDebug() << "下次一定";
    33. }
    34. void Widget::on_pushButton_2_clicked()
    35. {
    36. QMessageBox box(QMessageBox::Question,
    37. "问题对话框", //对话框标题
    38. "?", //对话框提示信息
    39. QMessageBox::Yes|QMessageBox::No,//对话框的提供的按钮
    40. this);
    41. //执行对话框
    42. int ret = box.exec();
    43. //对用户点击的按钮进行判断
    44. if(ret == QMessageBox::Yes)
    45. qDebug() << "ok";
    46. else
    47. qDebug() << "下次一定";
    48. }
    49. //警告函数按钮对应的槽函数
    50. void Widget::on_pushButton_4_clicked()
    51. {
    52. int ret=QMessageBox::warning(this,
    53. "警告",
    54. "zky",
    55. QMessageBox::Yes|QMessageBox::No,
    56. QMessageBox::NoButton);
    57. //对用户点击的按钮进行判断
    58. if(ret==QMessageBox::Yes)
    59. qDebug()<<"等着就等着,荤的还是素的";
    60. else
    61. qDebug()<<"对不起";
    62. }
    63. void Widget::on_pushButton_5_clicked()
    64. {
    65. int ret=QMessageBox::critical(this,
    66. "错误",
    67. "写错了",
    68. QMessageBox::Yes|QMessageBox::No,
    69. QMessageBox::Yes);
    70. //对用户点击的按钮进行判断
    71. if(ret==QMessageBox::Yes)
    72. qDebug()<<"知道了";
    73. else
    74. qDebug()<<"不管";
    75. }

    事件处理(核心机制) (wasd移动label)

    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. void Widget::keyPressEvent(QKeyEvent *event)
    14. {
    15. qDebug()<<"键盘按下了"<text()<<"键值为"<key();
    16. switch(event->key())
    17. {
    18. case 'W':
    19. {
    20. if(ui->label->y()<= 0-ui->label->height())
    21. {
    22. ui->label->move(ui->label->x(), this->height());
    23. }
    24. ui->label->move(ui->label->x(),ui->label->y()-5);
    25. }
    26. break;
    27. case 'S':
    28. {
    29. if(ui->label->y()>= this->height()+ui->label->height())
    30. {
    31. ui->label->move(ui->label->x(),0-ui->label->height());
    32. }
    33. ui->label->move(ui->label->x(),ui->label->y()+5);
    34. }
    35. break;
    36. case 'D':
    37. {
    38. if(ui->label->x()>=this->width()+ui->label->width())
    39. {
    40. ui->label->move(0-ui->label->width(), ui->label->y());
    41. }
    42. ui->label->move(ui->label->x()+5,ui->label->y());
    43. }
    44. break;
    45. case 'A':
    46. {
    47. if(ui->label->x()<= 0-ui->label->width())
    48. {
    49. ui->label->move(this->width(),ui->label->y());
    50. }
    51. ui->label->move(ui->label->x()-5,ui->label->y());
    52. }
    53. break;
    54. }
    55. }
    56. void Widget::keyReleaseEvent(QKeyEvent *event)
    57. {
    58. }

  • 相关阅读:
    介绍一个数据血缘的项目 OpenLineage
    Maven是什么? Maven的概念+作用
    phpcms V9实战标签代码记录 - list
    工具推荐:身边人都在用的那些知识管理软件!
    2022.11.16 英语背诵
    【工具篇】SQLite本地数据库在Unity3D的应用
    路由跟踪命令 tracert 命令详解
    MVC您正在查找的资源可能被移除
    单链表详解
    RAD Studio 11.2详解其务实改进(Delphi & C++ Builder)-Alexandria
  • 原文地址:https://blog.csdn.net/zky050213/article/details/132629845