简体   繁体   English

如何在QT4 Designer创建的QMainWindow中为按钮创建插槽/信号

[英]How to create a slot / signal for a button in a QMainWindow created by QT4 Designer

I want to have a python 2 program using qt4 which prints to terminal when a button is pressed in a QMainWindow. 我想要一个使用qt4的python 2程序,当在QMainWindow中按下按钮时,该程序会打印到终端。 How to create a slot / signal for a button in a QMainWindow created by QT4 Designer? 如何在QT4 Designer创建的QMainWindow中为按钮创建插槽/信号?

I have tried everything I can think of and I don't understand how to apply what I have found online to this problem. 我已经尝试了所有可以想到的方法,但是我不知道如何将我在网上发现的内容应用于该问题。 I am very new to python and it is likely the cause, please excuse me if this is obvious, but I'm very confused. 我对python非常陌生,这很可能是原因,如果这很明显,请原谅,但我很困惑。 The tutorials I've found are qt4 for C++, not python. 我发现的教程是针对C ++的qt4,而不是python。 If nothing else, at least the next person will be able to learn from this. 如果没有别的,至少下一个人将能够从中学习。

Simplified Example Issue 简化示例问题

The error I get is: 我得到的错误是:

$ python ./main.py 
Traceback (most recent call last):
  File "./main.py", line 27, in <module>
    myGUI.connect(myGUI.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")),pushButtonCLICK)
AttributeError: 'QMainWindow' object has no attribute 'pushButton'

main.py : main.py

#!/usr/bin/env python
import sys

import PyQt4 
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QMainWindow

import gui

def pushButtonCLICK():
    print("you pressed the button! Yay! \n")


def main(args):
    return 0

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)

    myGUI = QtGui.QMainWindow()
    ui = gui.Ui_MainWindow()
    ui.setupUi(myGUI)
    myGUI.show()
    myGUI.connect(myGUI.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")),pushButtonCLICK)


    sys.exit(app.exec_())

Design a QMainWindow in QT4 Designer, save as a ".ui" file and convert to python using: 在QT4 Designer中设计一个QMainWindow ,另存为“ .ui”文件并使用以下命令转换为python:

pyuic4 -x ./test.ui -o ./gui.py

gui.ui: : gui.ui ::

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>333</width>
    <height>245</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>50</y>
      <width>85</width>
      <height>30</height>
     </rect>
    </property>
    <property name="text">
     <string>ClickHere</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>333</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

gui.py : gui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file './test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(333, 245)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(120, 50, 85, 30))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 333, 26))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "ClickHere", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
#!/usr/bin/env python
import sys

import PyQt4 
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QMainWindow

import gui

def pushButtonCLICK():
    print("you pressed the button! Yay! \n")


def main(args):
    return 0

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)

    myGUI = QtGui.QMainWindow()
    ui = gui.Ui_MainWindow()
    ui.setupUi(myGUI)
    myGUI.show()
    myGUI.connect(ui.pushButton,QtCore.SIGNAL("clicked()"),pushButtonCLICK)


    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM