• QT基础第一天 (1)QT,GUI(图形用户接口)开发


    其实关于c++还有一个STL的内容,由于比较繁多,我后面来更,今天我们说界面开发,

    当然学东西我们不能只学怎么用,直接去学函数和参数,我们应该了解本质。

    现在我们开始说今天的内容。

    一.图形显示原理

    1.像素点,分辨率,RGB,透明度,显存...

    二.Linux上如何编程显示器

    linux帧缓存(frambuffer)

    编程:其实就是操作/dev/fb*文件

    举个例子,如何画点,横线,竖线

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. int main(int argc, char *argv[])
    19. {
    20. int ret ;
    21. struct fb_var_screeninfo fbi_var;//定义可变参数
    22. struct fb_fix_screeninfo fbi_fix;//定义固定参数
    23. struct fb_cmap cmap;
    24. //打开屏幕设备
    25. int fd = open("/dev/fb0",O_RDWR);
    26. if(fd < 0){
    27. perror("open");
    28. close(fd);
    29. }
    30. //得到虚拟屏幕参数
    31. ret = ioctl(fd,FBIOGET_VSCREENINFO,&fbi_var);
    32. if(ret < 0){
    33. perror("ioctl");
    34. return NULL;
    35. }
    36. 3. QT是什么
    37. 一堆可用于图形界面开发的C++类库
    38. 4. QT环境的安装(QT IDE)
    39. 1. QT类库
    40. 2. 编程编译环境
    41. printf("fbi.var.xres:%u ,fbi.var.yres:%u ,var.bpp:%d\n",fbi_var.xres
    42. ,fbi_var.yres,fbi_var.bits_per_pixel);
    43. printf("fbi.var.xoffset:%u ,fbi.var.yoffset:%u\n",fbi_var.xoffset
    44. ,fbi_var.yoffset);
    45. //得到实际屏幕参数
    46. ret = ioctl(fd,FBIOGET_FSCREENINFO,&fbi_fix);
    47. if(ret < 0){
    48. perror("ioctl");
    49. return NULL;
    50. }
    51. printf("fbi.fix.smem_start:%lu
    52. ,fbi.fix.smem_len:%u\n",fbi_fix.smem_start ,fbi_fix.smem_len);
    53. printf("line_length: %u\n",fbi_fix.line_length);
    54. //获取显存起始地址
    55. int *addr =
    56. mmap(NULL,fbi_fix.smem_len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    57. if(addr == NULL){
    58. perror("mmap");
    59. ret = munmap(addr,fbi_fix.smem_len);
    60. return NULL;
    61. }
    62. //画竖线
    63. int i = 100;
    64. while(i--)
    65. {
    66. *addr = 0xff00ff00;
    67. addr += 2048; //(得到下一行像素点的显存地址)
    68. }
    69. /*
    70. //画横线
    71. int i = 2048+400;
    72. while(i--)
    73. {
    74. *addr = 0xffff0000;
    75. addr++;
    76. }
    77. */
    78. close(fd);
    79. return 0;
    80. }

    效果图

    这个图不是上面的代码,却是改来的,为了看着更清晰

     

    那说到这里,我们怎么去实现我们想要的图形呢?

    大家可以看出一个一个去操控点,线,面是很麻烦的,这里要感谢外国的两个大学生,他们用一个假期,用c++封装了大量的库供后面人使用,所以 现在我们来进入今天的内容 QT。

    三.QT是什么?

    一堆可用于图形开发的c++库

    四.QT的安装环境

    1. QT类库

    2. 编程编译环境

     3. 创建工程

     

     

     五.必要的补充

    1. QPushButton 和 QLineEdit

    1. #include "widget.h"
    2. Widget::Widget(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. //2. 将控件真正的new出来
    6. //将按钮new出来
    7. bt = new QPushButton;
    8. bt->setText("登录");
    9. //bt->show();
    10. bt->setParent(this);
    11. le = new QLineEdit;
    12. le->setParent(this);
    13. //3. 排版
    14. le->setGeometry(10, 10, 200, 30);
    15. bt->setGeometry(100, 100, 100, 50);
    16. //4. 前后台功能挂接(信号与槽)
    17. /*按钮发出点击信号的时候,当前界面请关闭*/
    18. //connect(bt, SIGNAL(clicked(bool)), this, SLOT(close()));
    19. connect(bt, SIGNAL(clicked(bool)), this, SLOT(le_to_Uper()));
    20. }
    21. Widget::~Widget()
    22. {
    23. }

     

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9. //申明槽函数: 此函数未来可以直接调用 也可以 用信号绑定调用
    10. public slots:
    11. void le_to_Uper()
    12. {
    13. QString str = le->text(); //提取文字
    14. str = str.toUpper(); //文字小写变大写
    15. le->setText(str); //显示文字
    16. }
    17. public:
    18. Widget(QWidget *parent = 0);
    19. ~Widget();
    20. private:
    21. //1. 申明自己界面上想要的“子部件”
    22. //希望界面上有个“按钮”
    23. QPushButton *bt;
    24. //希望界面上有一个 行输入框
    25. QLineEdit *le;
    26. };
    27. #endif // WIDGET_H
    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    2.布局

    1. #include "widget.h"
    2. #include
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. bt = new QPushButton;
    7. bt->setParent(this); //设置父控件:回收和显示和父一起了
    8. bt->setText("登录"); //设置按钮上的文字
    9. QString str = bt->text(); //提取按钮上的文字
    10. qDebug()<<str;
    11. bt->setGeometry(100, 100, 100, 50); //强制设置 控/部件 位置
    12. bt->setFixedSize(10, 10);//设置固定大小
    13. bt->setMinimumSize(10, 10);//设置最小大小
    14. bt->setMaximumSize(10, 10);//设置最大大小
    15. le = new QLineEdit;
    16. le->setParent(this);
    17. le->setText("xxxxxx");
    18. str = le->text(); //提取按钮上的文字
    19. qDebug()<<str;
    20. le->setGeometry(100, 200, 100, 50); //强制设置 控/部件 位置
    21. le->setEchoMode(QLineEdit::Password); //设置回显方式
    22. le->setAlignment(Qt::AlignRight); //设置对其方式
    23. le->setFixedSize(10, 10);//设置固定大小
    24. le->setMinimumSize(10, 10);//设置最小大小
    25. le->setMaximumSize(10, 10);//设置最大大小
    26. }
    27. Widget::~Widget()
    28. {
    29. }
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. Widget(QWidget *parent = 0);
    11. ~Widget();
    12. //申明需要按钮
    13. QPushButton *bt;
    14. //申明需要行编辑框(输入框)
    15. QLineEdit *le;
    16. };
    17. #endif // WIDGET_H
    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    3.QString

    1. #include "widget.h"
    2. #include
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. bt = new QPushButton("1");
    7. bt1 = new QPushButton("2");
    8. le = new QLineEdit;
    9. QVBoxLayout *vbox = new QVBoxLayout;
    10. vbox->addWidget(le);
    11. vbox->addWidget(bt);
    12. vbox->addWidget(bt1);
    13. setLayout(vbox);
    14. connect(bt, SIGNAL(clicked(bool)), this, SLOT(xxx()));
    15. connect(bt1, SIGNAL(clicked(bool)), this, SLOT(xxx()));
    16. }
    17. Widget::~Widget()
    18. {
    19. }
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public slots:
    11. void xxx()
    12. {
    13. //0.提取按钮(可能来自于任意的按钮)
    14. QPushButton *xbt = static_cast( sender() );
    15. //1.提取按钮的文字
    16. QString str = xbt->text();
    17. //2.追加显示在行编辑框
    18. //a.提取原来的文字
    19. QString str1 = le->text();
    20. //b.拼接新文字
    21. str1.append(str);
    22. //c.重新设置回去
    23. le->setText(str1);
    24. int a = str1.toInt(); //字符串转整型数
    25. qDebug()<
    26. }
    27. public:
    28. Widget(QWidget *parent = 0);
    29. ~Widget();
    30. QLineEdit *le;
    31. QPushButton *bt, *bt1;
    32. };
    33. #endif // WIDGET_H
    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    4.打印调试

    1. #include "widget.h"
    2. #include
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. //构造需要的控件
    7. bt = new QPushButton("1");
    8. bt1 = new QPushButton("2");
    9. le = new QLineEdit;
    10. le->setAlignment(Qt::AlignRight);
    11. #if 0
    12. //垂直布局
    13. QVBoxLayout *vbox = new QVBoxLayout; //构造一个布局管理器
    14. vbox->addWidget(le); //将需要布局的控件加入布局管理器
    15. vbox->addWidget(bt);
    16. this->setLayout(vbox); //将布局管理器贴在当前界面
    17. #endif
    18. #if 0
    19. //水平布局
    20. QHBoxLayout *vbox = new QHBoxLayout; //构造一个布局管理器
    21. vbox->addWidget(le); //将需要布局的控件加入布局管理器
    22. vbox->addWidget(bt);
    23. this->setLayout(vbox); //将布局管理器贴在当前界面
    24. #endif
    25. #if 0
    26. //网格布局
    27. QGridLayout *vbox = new QGridLayout; //构造一个布局管理器
    28. vbox->addWidget(le, 0, 0, 1, 2); //将需要布局的控件加入布局管理器
    29. vbox->addWidget(bt, 1, 1);
    30. this->setLayout(vbox); //将布局管理器贴在当前界面
    31. #endif
    32. //混合布局
    33. QHBoxLayout *hbox = new QHBoxLayout;
    34. hbox->addWidget(bt);
    35. hbox->addWidget(bt1);
    36. QVBoxLayout *vbox = new QVBoxLayout;
    37. vbox->addWidget(le);
    38. vbox->addLayout(hbox);
    39. this->setLayout(vbox);
    40. }
    41. Widget::~Widget()
    42. {
    43. }
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. Widget(QWidget *parent = 0);
    11. ~Widget();
    12. QPushButton *bt, *bt1;
    13. QLineEdit *le;
    14. };
    15. #endif // WIDGET_H
    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

  • 相关阅读:
    113. 路径总和ii
    排查 dotNET Core 程序内存暴涨的问题
    两段锁协议
    JavaScript 发布-订阅设计模式实现 React EventBus(相当于vue的$Bus)非父子之间通信
    运维笔记:流编辑器sed命令用法解析
    数字电路与逻辑设计 之 组合电路的设计(多输出电路,全加器,乘法器)
    JS 实现对象监听 - Kaiqisan
    HTML图片标签(2) HTML5+CSS3+移动web 前端开发入门笔记(三)
    T31快启图像效果优化
    HTML5期末大作业:基于HTML+CSS+JavaScript仿蘑菇街购物商城设计毕业论文源码
  • 原文地址:https://blog.csdn.net/fuyuyf/article/details/125989489