尝试写qt模拟器

This commit is contained in:
2026-02-25 21:15:54 +08:00
parent 5d8b3f7bdb
commit 000ab4748e
1328 changed files with 645895 additions and 11 deletions
+131 -3
View File
@@ -1,14 +1,142 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QTextEdit> // 引入QTextEdit头文件
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化hal
for (int y = 0; y < 240; y++)
{
for (int x = 0; x < 320; x++)
{
LCD_buffer[y][x] = rand();
}
}
// 1. 设置窗口基本属性
this->setWindowTitle("QT LVGL"); // 窗口标题
this->resize(900, 600); // 窗口大小
this->setMaximumSize(900, 600);
this->setMinimumSize(900, 600);
// 2. 创建中心窗口(QMainWindow必须有centralWidget
QWidget *centralWidget = new QWidget(this);
this->setCentralWidget(centralWidget);
// 3. 创建QTextEdit组件
debug_output = new QTextEdit(centralWidget);
// 4. 给QTextEdit设置初始属性(可选)
debug_output->setFixedSize(500, 300);
debug_output->move(350, 10);
debug_output->setPlaceholderText("Debug output"); // 占位提示文字
debug_output->setFont(QFont("微软雅黑", 12)); // 设置字体和大小
debug_output->setReadOnly(true); // 是否只读(false为可编辑)
debug_output->setText("Debug output"); // 设置初始文本
// 5. 创建布局并添加QTextEdit
// QVBoxLayout *layout = new QVBoxLayout(centralWidget);
// layout->addWidget(textEdit); // 将QTextEdit加入布局
// layout->setContentsMargins(200, 20, 20, 20); // 设置布局边距
QTimer *hal_tick = new QTimer(this);
hal_tick->start(1);
connect(hal_tick, &QTimer::timeout, [=]()
{
// 每 1ms执行一次
onems_loop();
if(LCD_updata_flag)
{
LCD_updata_flag=0;
this->update();
}
});
workthread *main_thread = new workthread;
main_thread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
// 如果是鼠标左键按下
if (event->button() == Qt::LeftButton)
{
touch_pass = 1;
touch_x = event->x() - 10;
touch_y = event->y() - 10;
char str[32];
sprintf(str, "pass x:%d y:%d", touch_x, touch_y);
this->debug_append(str);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
touch_pass = 0;
this->debug_append("release");
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
touch_x = event->x() - 10;
touch_y = event->y() - 10;
char str[32];
sprintf(str, "move x:%d y:%d", touch_x, touch_y);
this->debug_append(str);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Space:
break;
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Space:
break;
}
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
QPen pen;
pen.setWidth(1); // 设置线宽
for (int y = 0; y < 240; y++)
{
for (int x = 0; x < 320; x++)
{
pen.setColor(QColor(((LCD_buffer[y][x] >> 11) & 0x1f) * 8, ((LCD_buffer[y][x] >> 5) & 0x3f) * 4, (LCD_buffer[y][x] & 0x1f) * 8, 255)); // 常用颜色
p.setPen(pen); // 将画笔给画家
p.drawPoint(x + 10, y + 10);
}
}
}
void MainWindow::debug_append(char *a)
{
debug_output->append(a);
}