pyqt keypress event in lineedit - python-3.x

i have created a untitled ui where exists one lineedit.
now i want to get the value of whatever i type in lineedit immediately
i figured out it could be done using keypressevent but i exactly didn't understand how to use it in lineedit now
from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow, Ui_Dialog):
def event(self, event):
if type(event) == QtGui.QKeyEvent:
print (event.key())
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
#here i want to get what is keypressed in my lineEdit
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
this is my untitled.py code that i have imported
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(662, 207)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 30, 113, 27))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

If you want to get the whole text in the line-edit as it is entered, you can use the textEdited signal. However, if you just want to get the current key that was pressed, you can use an event-filter.
Here is how to use both of these approaches:
class MainWindow(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.lineEdit.installEventFilter(self)
self.lineEdit.textEdited.connect(self.showCurrentText)
def eventFilter(self, source, event):
if (event.type() == QEvent.KeyPress and
source is self.lineEdit):
print('key press:', (event.key(), event.text()))
return super(MainWindow, self).eventFilter(source, event)
def showCurrentText(self, text):
print('current-text:', text)

Related

Outputting text in a form with flow using signals

Need help with the following task:
There is a main window MainWindow and there is a separate class of a stream in which there is a text. Tell me how to use signals on the plainTextEdit form, which is located on the main form, to display text through signals?
Main window code:
import sys
from PySide6.QtWidgets import *
from Controller.Potok_Controller import Potok_Controller
from View.ui_potok import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.open_window)
def open_window(self):
self.myThread = Potok_Controller()
# self.myThread.mysignal.connect(self.sendText)
self.myThread.start()
if __name__ == '__main__':
app = QApplication()
window = MainWindow()
window.show()
sys.exit(app.exec())
UI code of the main window:
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'untitledkonQbZ.ui'
##
## Created by: Qt User Interface Compiler version 6.3.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QMainWindow, QPlainTextEdit,
QPushButton, QSizePolicy, QVBoxLayout, QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.frame = QFrame(self.centralwidget)
self.frame.setObjectName(u"frame")
self.frame.setFrameShape(QFrame.StyledPanel)
self.frame.setFrameShadow(QFrame.Raised)
self.verticalLayout_2 = QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.plainTextEdit = QPlainTextEdit(self.frame)
self.plainTextEdit.setObjectName(u"plainTextEdit")
self.verticalLayout_2.addWidget(self.plainTextEdit)
self.verticalLayout.addWidget(self.frame)
self.frame_2 = QFrame(self.centralwidget)
self.frame_2.setObjectName(u"frame_2")
self.frame_2.setFrameShape(QFrame.StyledPanel)
self.frame_2.setFrameShadow(QFrame.Raised)
self.verticalLayout_3 = QVBoxLayout(self.frame_2)
self.verticalLayout_3.setObjectName(u"verticalLayout_3")
self.pushButton = QPushButton(self.frame_2)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout_3.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.frame_2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"\u0421\u0442\u0430\u0440\u0442 \u043f\u043e\u0442\u043e\u043a\u0430", None))
# retranslateUi
Thread class code:
import time
from PySide6.QtCore import QThread
class Potok_Controller(QThread):
def __init__(self, parent=None):
super(Potok_Controller, self).__init__(parent)
def txt(self):
txt1 = 'test1'
time.sleep(1)
txt2 = 'test2'
time.sleep(1)
txt3 = 'test3'
def run(self):
self.txt()
You just need to implement the sendtext method that you have commented out in your open_window method, and then create a signal that accepts a string parameter in your Potok_Controller class and emit the signal from the threads run method.
For Example:
class Potok_Controller(QThread):
mysignal = Signal([str]) # create signal
def __init__(self, parent=None):
super(Potok_Controller, self).__init__(parent)
def txt(self):
txt1 = 'test1'
time.sleep(1)
txt2 = 'test2'
time.sleep(1)
txt3 = 'test3'
return txt1 + txt2 + txt3
def run(self):
self.mysignal.emit(self.txt()) # emit the signal with text as parameter
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.open_window)
def sendText(self, text): # create the sendText method
self.ui.plaintextedit.setPlainText(text)
def open_window(self):
self.myThread = Potok_Controller()
self.myThread.mysignal.connect(self.sendText) # uncomment this
self.myThread.start()

PyQt5 program runs but does not display anything - widgets does not show [duplicate]

First of all, similar questions have been answered before, yet I need some help with this one.
I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second).
I fiddled with the code copied from this question: PyQT on click open new window, and I wrote this code:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
but on_pressed, this error message appears:
AttributeError: 'Second' object has no attribute 'exec_'
(design1 and design2 have been derived from the Qt designer.)
Any thought would be appreciated.
Here I'm using the show method.
Here is a working example (derived from yours):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked method, like so:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialogs = list()
def on_pushButton_clicked(self):
dialog = Second(self)
self.dialogs.append(dialog)
dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Show sub window in main window

Can't work out how to embed a window in a main window using classes:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Qt4 tutorial using classes
This example will be built
on over time.
"""
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, MainWindow):
super(Form, self).__init__()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This is supposed to be the single most basic bit of code using classes. How do I get the second window to show please.
As ekhumoro already pointed out, your widget needs to be a child of your mainWindow. However, I do not think that you need to call show for the widget, since it anyways gets called as soon as its parent (MainWindow) calls show. As mata pointed out correctly, the proper way to add a Widget to a MainWindow instance is to use setCentralWidget. Here is a working example for clarification:
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, parent):
super(Form, self).__init__(parent)
self.lbl = QtGui.QLabel("Test", self)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.setCentralWidget(new_window)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

pyQt: how to pass information between windows

I have two windows, both containing one button and one lineEdit. I want to create a "ping - pong" communication between both windows. At first, I write something in the lineEdit of the first window, press the button, and a second window appears.
I want the message written in the lineEdit of the first window to appear to the lineEdit of the second window. (and vice versa).
this is the code for the creation of the First window, derived from Qt Designer:
# -*- coding: utf-8 -*-
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(331, 249)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.PushButtonFirst = QtGui.QPushButton(self.centralwidget)
self.PushButtonFirst.setGeometry(QtCore.QRect(140, 180, 131, 27))
self.PushButtonFirst.setObjectName(_fromUtf8("PushButtonFirst"))
self.lineEditFirst = QtGui.QLineEdit(self.centralwidget)
self.lineEditFirst.setGeometry(QtCore.QRect(130, 50, 113, 27))
self.lineEditFirst.setObjectName(_fromUtf8("lineEditFirst"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionNew = QtGui.QAction(MainWindow)
self.actionNew.setObjectName(_fromUtf8("actionNew"))
self.actionOpen = QtGui.QAction(MainWindow)
self.actionOpen.setObjectName(_fromUtf8("actionOpen"))
self.actionClose = QtGui.QAction(MainWindow)
self.actionClose.setObjectName(_fromUtf8("actionClose"))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.PushButtonFirst.setText(_translate("MainWindow", "PushButtonFirst", None))
self.actionNew.setText(_translate("MainWindow", "New", None))
self.actionOpen.setText(_translate("MainWindow", "Open", None))
self.actionClose.setText(_translate("MainWindow", "Close", None))
this is the code for the creation of the Second window, derived from Qt Designer:
# -*- coding: utf-8 -*-
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(329, 260)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.PushButtonSecond = QtGui.QPushButton(self.centralwidget)
self.PushButtonSecond.setGeometry(QtCore.QRect(130, 190, 121, 27))
self.PushButtonSecond.setObjectName(_fromUtf8("PushButtonSecond"))
self.lineEditSecond = QtGui.QLineEdit(self.centralwidget)
self.lineEditSecond.setGeometry(QtCore.QRect(120, 80, 113, 27))
self.lineEditSecond.setObjectName(_fromUtf8("lineEditSecond"))
MainWindow.setCentralWidget(self.centralwidget)
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.PushButtonSecond.setText(_translate("MainWindow", "PushButtonSecond", None))
and this is the main code:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.PushButtonFirst.clicked.connect(self.on_PushButtonFirst_clicked)
self.dialog = Second(self)
def on_PushButtonFirst_clicked(self):
self.my_text_First = self.lineEditFirst.text()
pass_text(self)
self.dialog.show()
def pass_text(obj):
obj.lineEditSecond.setText('OK')
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I receive this message:
'First' object has no attribute 'lineEditSecond'
which is quite logical, since pass_text() is a function of the First class. Anyway, I can't think any workaround.
Any thought would be appreciated.
You pretty much got it working. When I get an attribute error and can't figure it out, I use print type(object) and print dir(object) as a first-line of debugging to double check that the object is what I think it is, and to inspect all of its attributes.
The problem is you were not passing the second dialog whose text you wanted to set. I fixed this, and made a few other minor changes to your First class:
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.PushButtonFirst.clicked.connect(self.on_PushButtonFirst_clicked)
self.partnerDialog = Second(self)
def on_PushButtonFirst_clicked(self):
self.partnerDialog.lineEditSecond.setText(self.lineEditFirst.text())
self.partnerDialog.show()
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
self.PushButtonSecond.clicked.connect(self.on_PushButtonSecond_clicked)
self.partnerDialog = parent #otherwise, recursion
def on_PushButtonSecond_clicked(self):
self.partnerDialog.lineEditFirst.setText(self.lineEditSecond.text())
self.partnerDialog.show()
I have tightened it up to keep things more encapsulated and easier for debugging/thinking/posting here.

How to trigger text in a widget using PyQt

This may look like a long question but it is actually really short, I've just decided to copy all the working code here.
I have a main window and a Tip of the Day widget.
I generated both the UI using the PyQt Designer.
I can open the Tip of the Day widget from the main window menu but I'm not able to make the buttons work:
I'd like to replace some text in the Tip of the Day widget when the previous and the next buttons are clicked.
I have the following main window called MainWindow.py:
from PyQt4 import QtCore, QtGui
from MainWindowUi import Ui_MainWindow
from FormUi import Ui_Form
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
# Main window user interface elements
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Main window signal/slot connections
self.setupConnections()
#QtCore.pyqtSlot()
def showTipDialog(self):
'''Trig dialog Tip'''
form = QtGui.QDialog()
form.ui = Ui_Form()
form.ui.setupUi(form)
form.exec_()
def setupConnections(self):
'''Signal and Slot Support'''
self.connect(self.ui.actionTip_of_the_Day, QtCore.SIGNAL('triggered()'), self.showTipDialog)
I have the following main.py:
import sys
from PyQt4 import QtGui
from MainWindow import MainWindow
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have the following main window UI called MainWindowUi.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created: Thu May 21 20:26:31 2015
# by: PyQt4 UI code generator 4.11.3
#
# 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(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuHelp = QtGui.QMenu(self.menubar)
self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionTip_of_the_Day = QtGui.QAction(MainWindow)
self.actionTip_of_the_Day.setObjectName(_fromUtf8("actionTip_of_the_Day"))
self.menuHelp.addAction(self.actionTip_of_the_Day)
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.menuHelp.setTitle(_translate("MainWindow", "Help", None))
self.actionTip_of_the_Day.setText(_translate("MainWindow", "Tip of the Day", None))
I have the following widget form UI FormUi.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Form.ui'
#
# Created: Thu May 21 23:57:41 2015
# by: PyQt4 UI code generator 4.11.3
#
# 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_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(418, 249)
self.verticalLayout = QtGui.QVBoxLayout(Form)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton_previous = QtGui.QPushButton(Form)
self.pushButton_previous.setObjectName(_fromUtf8("pushButton_previous"))
self.horizontalLayout.addWidget(self.pushButton_previous)
self.pushButton_next = QtGui.QPushButton(Form)
self.pushButton_next.setObjectName(_fromUtf8("pushButton_next"))
self.horizontalLayout.addWidget(self.pushButton_next)
self.verticalLayout.addLayout(self.horizontalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.lineEdit.setText(_translate("Form", "Here a tip I'd like to replace by pressing the buttons below.", None))
self.pushButton_previous.setText(_translate("Form", "Previous Tip", None))
self.pushButton_next.setText(_translate("Form", "Next Tip", None))
Please run main.py in order to open the main window and click Help > Tip of the Day to open the widget.
Thanks for the attention.
You have a MainWindow class (which you instantiate from the main() function) which you have written to instantiate your Ui_MainWindow class (thus creating the GUI) and link a button to a method which pops up the dialog.
Now just apply the same logic to the dialog. Instead of creating a QDialog() directly in showTipDialog, instead instantiate a subclass QDialog. Write the subclass in a similar way to what you've done for MainWindow. Connect the clicked signals from the prev/next pushbuttons to appropriate methods (which you write) that change the contents of the QLineEdit.

Resources