簡體   English   中英

使用C ++,如何在Qt Designer中運行python文件?

[英]Using C++, how can I run a python file in Qt Designer?

我終於決定從WxPython過渡到QT! 我正在使用Qt Designer5.9,但在放置新插槽時遇到問題。 我的目標是按一下GUI上的一個button ,並運行我在另一個python程序中編寫的功能。

在Qt Designer中,我“ go to slot ”,選擇clicked() ,然后出現。

主窗口

void MainWindow::on_pushButton_2_clicked()
{

}

正是我想要的,但是語言錯誤! 我的python很糟糕,更別說其他任何東西了。 因此,通過運行本教程,我知道如果我通過ui->textEdit->append(("Hello World")); 我可以做一些自定義,但是在使用pyuic轉換為.py進行轉換后,它的實現方式並不明顯。 我的函數很容易導入,如下所示,我只需要知道將其放在哪里即可。

import myfunction
myfunction()

誰能給我一個Qt Designer中需要用C ++編寫的示例,以便我可以在.ui轉換后調用python函數嗎?

我不知道為什么需要C ++,您可以在python中做您想做的事情。 在QT Designer中設計UI。 我喜歡避免使用pyuic,我更喜歡使用以下方式,也許您會發現它更好。 假設您的UI文件名為something.ui,並且您已在QT Designer pushButton_2中將按鈕命名,那么python中的代碼將是:

from PyQt4 import QtCore, QtGui, uic
Ui_somewindow, _ = uic.loadUiType("something.ui") #the path to your UI

class SomeWindow(QtGui.QMainWindow, Ui_somewindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_somewindow.__init__(self)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(self.yourFunction)

   def yourFunction(self):
        #the function you imported or anything you want to happen when the button is clicked.

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = SomeWindow()
    window.show()
    sys.exit(app.exec_())

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM