I'm writing a QtWidget that displays a QComboBox (drop down menu) using PyQt5. However, it's coming up empty. Does anyone know why?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Test(QtWidgets.QWidget):
def __init__(self, formname = "Test"):
super().__init__()
self.formname = formname
self.impute_methods = ["Method 1", "Method 2"]
def setupUi(self, Form):
Form.setObjectName(self.formname)
Form.resize(1154, 902)
self._create_buttons(Form)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def _create_buttons(self, Form):
self.test_box = QtWidgets.QComboBox()
self.test_box.addItems(self.impute_methods)
self.test_box.setGeometry(QtCore.QRect(110, 190, 150, 27))
self.test_box.setObjectName("test_box")
def selectionchange(self, i):
print(i)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate(self.formname, "Test"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
tester = QtWidgets.QDialog()
ui = Test()
ui.setupUi(tester)
tester.show()
sys.exit(app.exec_())
Here is a screenshot of what I see:
set parent for comboBox to place it on Test():
def _create_buttons(self, Form):
self.test_box = QtWidgets.QComboBox(self)
...
and for ui to place it on the dialog:
...
ui = Test()
ui.setParent(tester)
...
Related
I have a QmainWindow called "first page" it has two buttons in it "Signin" and "Signup".Both buttons will open Qdialogs. In my sign in dialog I have two line edits that takes user's input and two buttons "cancel" and "ok". When "ok" is clicked I want to check some validations such as If the line edits are empty then show a QmessageBox on top of the dialog and then close the messagebox after user click "ok" on messagebox.
MY PROBLEM: When the messagebox opens, the dialog closes.
P.S: I've been implementing my code by modifing the QtDesigner code which I just found out was the worst thing to do! so I'm doing everything again. I didn't have this problem back then.
my code:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from first_page import Ui_first_page
from signin_page import Ui_signin_page
from signup_page import Ui_signup_page
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_page()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttons.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
"""Inputs the user's username and password for sign in process and checks validations"""
input_username = self.ui.signin_username_lineEdit.text()
input_password = self.ui.signin_password_lineEdit.text()
# Check if both are filled
if input_username == "":
#MY PROBLEM IS HERE
qtw.QMessageBox.critical(self,"Note", "Please input your username.")
elif input_password == "":
qtw.QMessageBox.critical(self, "Note", "Please input your password.")
else:
# Checks validations and opens mainpage
open_main_page(self, username, status)
def open_main_page(self, username, status):
""" Opens the main page after sign in"""
pass
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_first_page()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.first_page_sign_in_button.clicked.connect(self.open_sign_in_page)
self.ui.first_page_sign_up_button.clicked.connect(self.open_sign_up_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
def open_sign_up_page(self):
self.signup = SignUp()
self.signup.show()
class SignUp(qtw.QDialog):
#Doesn't do anything yet.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_signup_page()
self.ui.setupUi(self)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
I'm very new so if I'm doing anything very wrong please point it out.
thanks in advance.
Mini example of my problem:
Main code:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from mainwindow import Ui_MainWindow
from dialog import Ui_signin_Dialog
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
Ui_MainWindow:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
self.signin_buttonBox.accepted.connect(signin_Dialog.accept)
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
I want the Qmessagebox to open like a popup message instead of closing the dialog.
Try it:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore, QtGui, QtWidgets # +++
#from mainwindow import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
#from dialog import Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
# self.signin_buttonBox.accepted.connect(signin_Dialog.accept) # ---
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
else: # +++
self.accept() # +++
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
I use QT designer to design a page that contains two buttons, a start and an end, but when I click them, there is no response. I put them all in widgets, is that why?
I'm confused. When I click the start or end button,no Change!
PyQt5 When I click the QpushButton ,No change?
Is it because it's inside the widget?
My code is like this.
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 480)
self.widget_6 = QtWidgets.QWidget(Form)
self.widget_6.setEnabled(True)
self.widget_6.setGeometry(QtCore.QRect(240, 250, 151, 68))
self.widget_6.setStyleSheet("background-color: rgb(230, 230, 230);")
self.widget_6.setObjectName("widget_6")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.widget_6)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.start_btn = QtWidgets.QPushButton(self.widget_6)
self.start_btn.setStyleSheet("")
self.start_btn.setObjectName("start_btn")
self.verticalLayout_4.addWidget(self.start_btn)
self.end_btn = QtWidgets.QPushButton(self.widget_6)
self.end_btn.setStyleSheet("")
self.end_btn.setObjectName("end_btn")
self.verticalLayout_4.addWidget(self.end_btn)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.start_btn.setText(_translate("Form", "start"))
self.end_btn.setText(_translate("Form", "end"))
import sys
from PyQt5.Qt import QApplication, QWidget
class Sorter(QWidget, Ui_Form):
"""
demo
"""
def __init__(self, parent=None, *args, **kwargs):
"""
init
"""
super(Sorter, self).__init__(parent, *args, **kwargs)
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = Sorter()
myWin.show()
sys.exit(app.exec_())
What I need from the program is, once I press the TAB from the keyboard, should move to the next field and execute specific function.
The function in the first field is 'ID'(digits) is taken from the user as input. Then, after I press the TAB should extract specific digits, these digits are the 'Birth-date'.
By the 'push-button' is working fine. However, by the tab key is not working and through errors. Please see the code and the image.
I am using PYQT5 and Python 3.7.
Form image
See the for image in this link:
The code
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(282, 130, 181, 41))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(216, 140, 51, 20))
self.Handel_Buttons()
self.keyPressEvent()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "ID"))
self.label_2.setText(_translate("MainWindow", "D.O.B"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def Handel_Buttons(self):
self.pushButton.clicked.connect(self.ID)
def keyPressEvent(self):
print('HI')
if QtCore.Qt.Key_Tab:
self.ID()
def ID(self):
number = self.lineEdit.text()
#number = '279121100762'
#print(int(number[5]))
digit = int(number[5])
digit1 = int(number[6])
digit2 = int(number[3])
digit3 = int(number[4])
digit4 = int(number[1])
digit5 = int(number[2])
#self.textBrowser.append(str[digit,digit1])
self.textBrowser.append ('%d%d/%d%d/19%d%d' % (digit, digit1,digit2,digit3,digit4,digit5))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Many thanks,
The code you provide can not be executed so I take the time to create it from scratch. The basic idea is to intercept the events of the QLineEdit through an eventFilter:
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.id_le = QtWidgets.QLineEdit("279121100762")
self.id_le.installEventFilter(self)
self.dob_le = QtWidgets.QLineEdit()
btn = QtWidgets.QPushButton(
text="Press me",
clicked=self.conversion
)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QFormLayout(central_widget)
lay.addRow("ID", self.id_le)
lay.addRow("D.O.B", self.dob_le)
lay.addRow(btn)
def eventFilter(self, obj, event):
if self.id_le == obj and event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Tab:
QtCore.QTimer.singleShot(0, self.conversion)
return super(MainWindow, self).eventFilter(obj, event)
def conversion(self):
id_value = self.id_le.text()
if len(id_value) > 7:
text = id_value[1:7]
dt = QtCore.QDateTime.fromString(text, "yyddMM")
if dt.isValid():
self.dob_le.setText(dt.toString("dd/MM/yyyy"))
return
print("Invalid conversion")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
This question already has answers here:
Simple python inheritance
(3 answers)
QtDesigner changes will be lost after redesign User Interface
(2 answers)
Closed 4 years ago.
I am trying to add a custom widget to a layout. I can successfully add many PushButtons to my GridLayout, but when I attempt to add the custom widget it does not show.
I have attempted to provide a minimal example:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class moduleForm(QtWidgets.QWidget):
def __init__(self, parent = None):
self.parent = parent
self.setObjectName("moduleForm")
self.resize(300, 400)
self.fModule = QtWidgets.QPushButton("Test")
self.fModule.setGeometry(QtCore.QRect(0, 0, 80, 20))
self.retranslateUi(self.parent)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self, moduleForm):
_translate = QtCore.QCoreApplication.translate
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Rb Controller")
MainWindow.resize(900, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.loMainTab = QtWidgets.QHBoxLayout(self.centralwidget)
self.centralwidget.setLayout(self.loMainTab)
self.saChannels = QtWidgets.QScrollArea(self.centralwidget)
self.saChannels.setWidgetResizable(True)
self.saChannels.setGeometry(QtCore.QRect(10,10,10,10))
self.fButtons = QtWidgets.QFrame(self.centralwidget)
self.fButtons.setFrameShadow(QtWidgets.QFrame.Sunken)
self.pbAddModule = QtWidgets.QPushButton(self.fButtons)
self.pbAddModule.setGeometry(QtCore.QRect(10, 10, 80, 20))
self.pbAddModule.setObjectName("pbAddModule")
self.loButtons = QtWidgets.QHBoxLayout(self.fButtons)
self.loButtons.addWidget(self.pbAddModule)
self.loButtons.addStretch()
self.fButtons.setLayout(self.loButtons)
self.hlwChannelsContents = QtWidgets.QWidget()
self.hlwChannelsContents.setObjectName("hlwChannelsContents")
self.hloChannelsContents = QtWidgets.QHBoxLayout(self.hlwChannelsContents)
self.hloChannelsContents.setObjectName("hloChannelsContents")
self.gloChannelsContents = QtWidgets.QGridLayout()
self.hloChannelsContents.addLayout(self.gloChannelsContents)
self.saChannels.setWidget(self.hlwChannelsContents)
self.loMainTab.addWidget(self.fButtons)
self.loMainTab.addWidget(self.saChannels,1)
for ii in range(10):
for jj in range(10):
self.r_button = QtWidgets.QPushButton("Element %s,%s " % (ii, jj))
self.gloChannelsContents.addWidget(self.r_button,ii,jj)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pbAddModule.clicked.connect(self.createModule)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Rb Controller"))
self.pbAddModule.setText(_translate("MainWindow", "Add Module"))
def createModule(self):
createModule = moduleForm()
self.gloChannelsContents.addWidget(createModule)
createModule.show()
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I have added the super().__init__ function but it is still not working. The PushButton in my custom widget gets displayed if I add it not the custom widget, so all the other code is fine.
If I have:
self.gloChannelsContents.addWidget(createModule.fModule,self.i,self.j)
in createModule then I get a dynamic PushButton, however, if I try to use the custom widget
self.gloChannelsContents.addWidget(createModule,self.i,self.j)
nothing appears.
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class moduleForm(QtWidgets.QWidget):
def __init__(self, row, parent = None): # + row
super().__init__()
self.parent = parent
self.setObjectName("moduleForm")
self.resize(300, 400)
self.fModule = QtWidgets.QPushButton("Test {}".format(row)) # + row
self.fModule.setGeometry(QtCore.QRect(0, 0, 80, 20))
self.retranslateUi(self.parent)
QtCore.QMetaObject.connectSlotsByName(self)
def retranslateUi(self, moduleForm):
_translate = QtCore.QCoreApplication.translate
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.i = 11 # + self.i
MainWindow.setObjectName("Rb Controller")
MainWindow.resize(900, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.loMainTab = QtWidgets.QHBoxLayout(self.centralwidget)
self.centralwidget.setLayout(self.loMainTab)
self.saChannels = QtWidgets.QScrollArea(self.centralwidget)
self.saChannels.setWidgetResizable(True)
self.saChannels.setGeometry(QtCore.QRect(10,10,10,10))
self.fButtons = QtWidgets.QFrame(self.centralwidget)
self.fButtons.setFrameShadow(QtWidgets.QFrame.Sunken)
self.pbAddModule = QtWidgets.QPushButton(self.fButtons)
self.pbAddModule.setGeometry(QtCore.QRect(10, 10, 80, 20))
self.pbAddModule.setObjectName("pbAddModule")
self.loButtons = QtWidgets.QHBoxLayout(self.fButtons)
self.loButtons.addWidget(self.pbAddModule)
self.loButtons.addStretch()
self.fButtons.setLayout(self.loButtons)
self.hlwChannelsContents = QtWidgets.QWidget()
self.hlwChannelsContents.setObjectName("hlwChannelsContents")
self.hloChannelsContents = QtWidgets.QHBoxLayout(self.hlwChannelsContents)
self.hloChannelsContents.setObjectName("hloChannelsContents")
self.gloChannelsContents = QtWidgets.QGridLayout()
self.hloChannelsContents.addLayout(self.gloChannelsContents)
self.saChannels.setWidget(self.hlwChannelsContents)
self.loMainTab.addWidget(self.fButtons)
self.loMainTab.addWidget(self.saChannels,1)
for ii in range(10):
for jj in range(10):
self.r_button = QtWidgets.QPushButton("Element %s,%s " % (ii, jj))
self.gloChannelsContents.addWidget(self.r_button, ii, jj)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pbAddModule.clicked.connect(self.createModule)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Rb Controller"))
self.pbAddModule.setText(_translate("MainWindow", "Add Module"))
def createModule(self):
self.createModule = moduleForm(self.i) # +
self.gloChannelsContents.addWidget(self.createModule.fModule, self.i, 0) # +
self.i += 1 # +
# self.createModule.show() # ???
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
How to let MouseEvent working?
I try to print mouse tracking to label x,y coordinate but always fail. I already using setMouseTracking(True), generate from QtDesigner ui to py.
code Below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 480)
Form.setMouseTracking(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(270, 190, 58, 15))
self.label.setObjectName("label")
self.label.setMouseTracking(True)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "TextLabel"))
def mouseMoveEvent(self, e):
x = e.x()
y = e.y()
text = "x: {0}, y: {1}".format(x, y)
self.label.setText(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Ui_Form is not a widget, so it will not have the mouseMoveEvent method, as the PyQt docs point out you must create a class that inherits the appropriate widget, in this case QWidget, and use the interface provided by Qt Designer:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(640, 480)
Form.setMouseTracking(True)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(270, 190, 58, 15))
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "TextLabel"))
class Form(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setupUi(self)
self.setMouseTracking(True)
def mouseMoveEvent(self, e):
text = "x: {0}, y: {1}".format(e.x(), e.y())
self.label.setText(text)
self.label.adjustSize()
super(Form, self).mouseMoveEvent(e)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Form()
w.show()
sys.exit(app.exec_())