《QT从基础到进阶·七十三》Qt+C++开发一个python编译器,能够编写,运行python程序

1、概述
源码放在文章末尾

该项目利用Qt+C++实现了一个简易的python编译器,类似pycharm或vsCode这样的编译器,该python编译器支持如下功能:

(1)支持编写python程序

(2)编写代码时有代码补全提示

(3)程序运行到每行时该行高亮显示

(4)可以加载python脚本执行

(5)可以在程序运行的过程中随时中断

(6)有输出窗口实时显示程序执行的状态或执行程序的打印显示等

(7)支持在界面上打断点调试,该条在开发中...

下图为Python编译器的demo演示图:

项目分析:

(1)支持编写python程序

cpp 复制代码
class CodeEditor : public QPlainTextEdit
{
    Q_OBJECT

public:
    CodeEditor(QWidget *parent = 0);
    ~CodeEditor();

    //行号和断点绘制
    void lineNumberAreaPaintEvent(QPaintEvent *event);
    //获取行号区域宽度
    int lineNumberAreaWidth();

    //获取位置编号
    int getBlockNumberAtPosition(const QPoint& pos);
    //触发断点
    void toggleBreakpoint(int blockNumber);

这里通过继承QPlainTextEdit类的形式来实现在QPlainTextEdit界面上编写python脚本的功能。在界面执行run的功能时会通过信号槽的形式获取QPlainTextEdit界面上的内容并传入PyRun_SimpleString函数中执行。

(2)补全提示

补全提示的功能直接调用python的jedi库,所有通过pip安装的python库都能够通过jedi的库的补全功能提示出来,非常方便,如果要使用该功能需要在本地电脑安装:pip install jedi

(3)高亮显示

程序运行到每一行时都会高亮显示,或鼠标放在某一行也会高亮显示。

cpp 复制代码
void ShowLine(int line)
{
	QMetaObject::invokeMethod(g_pSmartPython, [=]() {
		QTextCursor tc = g_pScriptPlainTextEdit->textCursor();
		int position = g_pScriptPlainTextEdit->document()->findBlockByNumber(line - 1).position();
		tc.setPosition(position, QTextCursor::MoveAnchor);
		g_pScriptPlainTextEdit->setTextCursor(tc);


		QList<QTextEdit::ExtraSelection> extraSelections;
		QTextEdit::ExtraSelection selection;

		QColor lineColor = QColor(220, 220, 220);

		selection.format.setBackground(lineColor);
		selection.format.setProperty(QTextFormat::FullWidthSelection, true);
		selection.cursor = g_pScriptPlainTextEdit->textCursor();
		selection.cursor.clearSelection();
		extraSelections.append(selection);

		g_pScriptPlainTextEdit->setExtraSelections(extraSelections);
		}, Qt::BlockingQueuedConnection);

执行每一行时都会触发ShowLine回调函数,内部获取行号并进行颜色标注该行实现高亮显示。

(4)加载python脚本

(5)随时中断运行的程序

(6)当程序运行过程中点击stop会触发

cpp 复制代码
int MyTrace(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg)
{
	if (g_isExection)
		return 0;

	if (g_isAbort)
	{
		if (!m_isInterrupt)
		{
			qDebug() << "User abort.";
			//PyErr_SetString(PyExc_KeyboardInterrupt, "User abort.");

			if (scriptThreadState)
			{
				PyGILState_STATE gstate = PyGILState_Ensure();
				PyThreadState_SetAsyncExc((unsigned long)scriptThreadState->thread_id, PyExc_KeyboardInterrupt);
				PyGILState_Release(gstate);
			}
			m_isInterrupt = true;

在程序运行过程中会一直调用MyTrace函数追踪状态,当点击stop会执行PyThreadState_SetAsyncExc((unsigned long)scriptThreadState->thread_id, PyExc_KeyboardInterrupt);

该程序会强制中断python程序。

(7)断点调试程序

目前已经实现了在界面上打断点,效果如下:

断点单步调试功能还在开发中...

源码下载

相关推荐
做怪小疯子3 小时前
华为笔试0429
python·numpy
Warson_L3 小时前
Dictionary
python
王老师青少年编程4 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【哈夫曼贪心】:合并果子
c++·算法·贪心·csp·信奥赛·哈夫曼贪心·合并果子
叼烟扛炮5 小时前
C++第二讲:类和对象(上)
数据结构·c++·算法·类和对象·struct·实例化
寒山李白5 小时前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
样例过了就是过了6 小时前
LeetCode热题100 最长公共子序列
c++·算法·leetcode·动态规划
2401_832365526 小时前
JavaScript中rest参数(...args)取代arguments的优势
jvm·数据库·python
Sirius.z6 小时前
第J3周:DenseNet121算法详解
python
谭欣辰6 小时前
C++ 排列组合完整指南
开发语言·c++·算法
2301_779622416 小时前
Go语言怎么用信号量控制并发_Go语言semaphore信号量教程【入门】
jvm·数据库·python