找不到有关使用Python绑定QtDesigner4的方法

2024-10-01 19:15:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经厌倦了在python类(python2.7&PyQt4)中寻找处理加载的小部件的方法文件.ui(图形用户界面设计器)

代码

form_class = uic.loadUiType("MyPythonProgram.ui")[0]

class MyWindowClass(QtGui.QMainWindow, form_class):

def __init__(self, parent = None):
    QtGui.QMainWindow.__init__(self, parent)
    self.setupUi(self)
    self.btn_buscar.clicked.connect(self.buscar)

def addingResultsToQListView(self):
    for item in SomeList:
        self.listView.addItem(item) ###It's not correct, but cannot find the right one

def onListItemClicked():
    getItem = listView.currentItem().text() ###It's not correct, but cannot find the right one

def buscar(self):
    getEditText = self.textEdit.toPlainText()
    ### Don't know how to do this function. I want to get the edittext to search on some website and retrieve the results into a list. then the list will be added to QlistView (just found C++ methods, not for python)
    #Finally        
    getEditText = '' ###After click on 'btn_buscar', want to clear this field 

app = QtGui.QApplication(sys.argv)
MyWindow = MyWindowClass(None)
MyWindow.show()
app.exec_()

它可以帮助您获取一些文档,或者帮助您实现python应用程序的混合(如果可能的话,可以是Android),在我向您展示的过程中分别保留.ui和.py层。你知道吗

这是我的.ui以获取更多信息:

UI QT DESIGNER4

'问题' 如何将python函数与.ui上的元素绑定?我尝试了太多的方法,但没有找到合适的方法。需要知道如何处理QlistView和Qedittext。。。谢谢


Tags: theto方法selfformuiinitdef
1条回答
网友
1楼 · 发布于 2024-10-01 19:15:14

我就是这样做的:在UI的构造(加载)和更改其内容之间分离类。你知道吗

form_class = uic.loadUiType("MyPythonProgram.ui")[0]

class MyWindowClass(QtGui.QMainWindow, form_class):   
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

class myGui:
    def __init__(self):
        self.gui = MyWindowClass() # that's the trick!
        # self.addindResultsToQListView() # doesn't work, because I don't have your list items
        self.gui.btn_buscar.clicked.connect(self.buscar)
        self.editText = None

    def show(self):
        self.gui.show()

    def addingResultsToQListView(self):
        for item in SomeList: # you need to specify this `list` before this works!
            self.gui.listView.addItems(item) 

    def buscar(self):
        self.editText = self.gui.textEdit.text()
        self.gui.textEdit.setText("") 

app = QtGui.QApplication(sys.argv)
MyWindow = MyGui()
MyWindow.show()
app.exec_()

诀窍是引用MyWindowClass,它是gui的构造函数,因此也是gui本身,作为类中控制gui内容的对象(myGui)。你知道吗

在顶层调用myGui,然后调用MyWindowClass作为对象self.gui。从那时起,每当您想在GUI中寻址某个对象时,您就将其命名为self.gui.,并添加QObject。你知道吗

我也试着理解你想为pushBotton做什么。TextEdit的内容(在PyQt中称为lineEditbtw)存储在变量self.editText中,该变量被初始化为None。之后,从用户内容中清除lineEdit。你知道吗

相关问题 更多 >

    热门问题