qq_37468571 2023-08-01 14:57 采纳率: 0%
浏览 58
已结题

python调用Qt动态库(带界面)


#include <QApplication>
#include "testexe_dll.h"
#include "iostream"


#ifdef __cplusplus
extern "C" {
#endif
    TESTEXE_DLL_EXPORT int addFunc(int a, int b)
    {
        int result = a + b;
        std::cout<<"-----------";
        return result;
    }
    TESTEXE_DLL_EXPORT void show_dialog(TestExe_dll *dll)
    {
        dll->showDialog();
    }
    TESTEXE_DLL_EXPORT QWidget* get_dialog(TestExe_dll *dll)
    {
        return dll->getDialog();
    }
    TESTEXE_DLL_EXPORT void init(TestExe_dll *dll)
    {
        dll->initWin();
    }
#ifdef __cplusplus
}
#endif

QWidget* g_widget;
Form *dlgForm = NULL;

int argc;
char* argv[] = {};
QApplication a(argc, argv);


TestExe_dll::TestExe_dll()
{
}

int TestExe_dll::showDialog()
{
    dlgForm->show();
    return a.exec();
}

void TestExe_dll::initWin()
{
    dlgForm = new Form();
    g_widget = dlgForm;
    dlgForm->hide();
}

QWidget *TestExe_dll::getDialog()
{
    return g_widget;
}

除第三个方法以外,其他方法python可以调用成功。
问题:python如何接收QWidget指针类型?(python 零基础)
ps:QWidget是Qt中的窗口类,我将Qt窗口返回出去,供外部获取窗口内的控件。

以下是调用过程及报错信息:AttributeError: 'int' object has no attribute 'children'
Children()是Qt中QWidget中的方法,通过get_dialog()返回QWidget给widget,widget为int类型,故报错int中没有children属性,我需要一个QWidget来接收get_dialog(),但是我不知道在python中如何操作。

from PyQt6.QtWidgets import QApplication,QMainWindow,QWidget,QPushButton
from ctypes import *
import sys
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtWidgets import QApplication
if __name__ == "__main__":
    App = QApplication(sys.argv)    # 创建QApplication对象,作为GUI主程序入口

    dll_path = "E:\\testQtProject\\testExe_dll\\bin\\testExe_dll.dll"
    lib = cdll.LoadLibrary(dll_path) # 加载Qt动态库
    lib.init() # 库初始化

    widget = lib.get_dialog() # 获取窗口,返回值为QWidget*
    print(widget)

    listW = widget.children()
    print(listW)

    widget.show()
    sys.exit(App.exec())   # 循环中等待退出程序

报错信息:
Traceback (most recent call last):
File "C:\Users\dell\PycharmProjects\untitled\testQt.py", line 17, in
listW = widget.children()
AttributeError: 'int' object has no attribute 'children'

  • 写回答

9条回答 默认 最新

  • 技术宅program 2023-08-01 15:18
    关注
    获得1.05元问题酬金

    报错翻译
    文件“C:\Users\dell\PycharmProjects\untitle\testQt.py”,第17行,位于
    listW=widget.children()
    AttributeError:“int”对象没有属性“children”

    评论

报告相同问题?

问题事件

  • 系统已结题 8月9日
  • 修改了问题 8月1日
  • 创建了问题 8月1日

悬赏问题

  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22
  • ¥15 R语言中lasso回归报错