【问题标题】:How to add QT functionality to a non-QT driver如何将 QT 功能添加到非 QT 驱动程序
【发布时间】:2020-12-10 21:43:31
【问题描述】:

我有一个正在开发的 QT 应用程序,它本质上是一个 GUI,可以从几个不同的内部开发子模块启动程序,以及在这个应用程序本身内部开发的一些例程。但是,我遇到了障碍。其中一个子模块被开发为能够作为独立库 (.a) 运行。

这个库是使用在命令行上编译的主 cpp 运行的。

main.cpp 的示例代码

// these functions are called inside the library, but implemented in main.cpp for flexibility

void printMsg(String& msg){
    // prints/cout msg
}

int printPrompt(String& str1, String& str2){
    // it prints str1 and str2 as prompt
    // gets the user input from console that is returned as int return value
    // library uses this return value to do various task
}

main(){
    runLibApp()  // this function is in the library that starts the execution
}

我想要弹出窗口来处理消息、提示和用户输入。我确实在主窗口类中定义了一个弹出函数,该函数在其他地方使用,但它只在主窗口类和几个 QT 子类中调用。

由于这个库不是 QT 子类,我不能在其中使用我的 QT 弹出函数。在不破坏库本身的情况下,我无法将函数重新定义为 QT 子类的一部分。

有没有什么方法可以将我的 QT 弹出函数连接到这个库,而不必彻底重做这个库?

【问题讨论】:

  • 也许你可以将它作为一个单独的应用程序运行,并通过重定向标准输入和标准输出来做一些时髦的事情?
  • 在 QProcess 中,您可以读取输出并将输入发送到外部命令行程序。

标签: c++ qt oop


【解决方案1】:

几乎在发布后,我自己找到了答案!在我的驱动程序 cpp 文件中,我添加了标准 Qt 包含,以及 mainwindow.h

在输入/输出函数本身中,我添加了这个方便的函数来获取主窗口指针:

void PrintMsg(String msg){
    MainWindow* gui = MainWindow::getMainWinPtr();
    gui->Popup(msg);
}

完美运行!

【讨论】:

    猜你喜欢
    • 2014-12-17
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 2021-04-13
    • 2019-07-13
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多