I am developping an application with PyQt based on a QWidget. I put my whole app inside a class (named "Example"). Inside this app, I have a button which calls to another QWidget window (named "Report Widget") I designed on QtDesigner and opens it.
The problem is that I don't know how to do this. Here is the method I use (which comes from something I found on internet).
On my main class "Example", a method defines the Button calling for the "Report Widget" :
ReportBtn = QtGui.QPushButton("Generate report")
ReportBtn.clicked.connect(self.ShowReportWidget)
The second line calls the ShowReportWidget method of the Example (main) class :
def ShowReportWidget(self):
self.f = QtGui.QWidget()
self.ReportWidget = ReportWidget(self.f)
self.ReportWidget.show()
This method calls the class ReportWidget, which is a class from the Example (main) class :
class ReportWidget(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self)
self.ui = rw.Ui_ReportWidget()
self.ui.setupUi(parent)
and finally, this class refers to an outer class (Ui_ReportWidget), contained in another separate Python file (the one generated from the QtDesigner .ui file), which I import as rw in the beginning of my script.
The problem is that with this method, when I click on the Report Widget button, a new window pops but is empty. The content of the Ui_ReportWidget class is not loaded.
I hope my question is clear enough.
Thanks
SetupUi takes an Qwidget as input , while the init() function should take the calling QObject as the parent.
def ShowReportWidget(self):
self.f = QtGui.QWidget()
self.ReportWidget = ReportWidget(self)
self.ReportWidget.setupUi(self.f)
self.f.exec_()
class ReportWidget(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self,parent)
def setupUi(self,Widget):
self.ui = rw.Ui_ReportWidget()
self.ui.setupUi(Widget)
Related
I have imported a menu bar button from "Design.ui" and tried to assign functions to it but it seems like there is an error.
Imported design.ui --> uic.loadUi("Design.ui", self)
Then importing the menu button from design.ui self.openFileButton = self.findChild(QStatusBar, "actionOpen")
(Error : 'NoneType' object has no attribute 'triggered' ) Then trying to assign functions in it and getting the error, self.openFileButton.triggered.connect(self.openImage)
Thanks for helping :)
After calling uic.loadUi(), you should be able to to directly use the QWidgets you created in the .ui file instead of calling self.findChild()
For example:
class MainWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
uic.loadUi("Design.ui", self)
self.openFileButton.triggered.connect(self.openImage)
def openImage(self):
# Does some code
Alternatively, you can set the loaded UI as a variable and use the widgets from there.
For Example:
class MainWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.ui = uic.loadUi("Design.ui", self)
self.ui.openFileButton.triggered.connect(self.openImage)
def openImage(self):
# Does some code
I am working on a small GUI project using PyQt4. I have defined one class (inside a separate file) defining the basic functionality of combo_boxes that I have to use and another class to use the functionality for all the combo_boxes.
The code looks something like
class core:
def __init__(self, default_value, window_name):
self.combo_box = QtGui.QComboBox(window_name)
self.combo_box.addItem(default_value)
self.combo_box.addItem("some other value")
self.combo_box.addItem("a third value")
self.combo_box.activated[str].connect(self.set_text)
self.text = default_value
def set_text(self, text):
print text
The main class is something like:
from file import *
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(200, 100, 820, 700)
combo_box_one = core("first", self)
combo_box_two = core("second", self)
#some other methods follow defining the geometry for each combo_box and other functions
def main():
app = QtGui.QApplication(sys.argv)
gui = Window()
sys.exit(app.exec_())
main()
The GUI is working as expected. All the combo_boxes appear as per the defined geometry. However, when on selecting different options, nothing seems to happen. Ideally, I would expect the text on the option to be printed. In fact, when I return the combo_box object to the main class and set it connections there, the change in options is reflected. But when the same thing is done in the coreclass, the changes are not reflected as printed text. Is it a scope related thing? Please help me understand what's happening.
Slots can only be implemented in classes that inherit from QObject, a simple solution is that the core class inherits from QComboBox, since QComboBox inherits from QObject.
class core(QtGui.QComboBox):
def __init__(self, default_value, window_name):
QtGui.QComboBox.__init__(self, window_name)
self.addItem(default_value)
self.addItem("some other value")
self.addItem("a third value")
self.activated[str].connect(self.set_text)
def set_text(self, text):
print(text)
I have a GUI App with a Main Dialog and I added a button to it.
Pushing the button adds another "dialog" where the user has to input some values.
Both Ui-files are written with the QTDesigner and "dialog" has a "QtableWidget" with the object name "tableCo" I am not sure why I cannot change the properties of this tableWidget:
from PyQt4 import QtGui, QtCore, Qt
from Main_Window import Ui_Dialog as Dlg
from dialog import Ui_MyDialog
class MainDialog(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
self.connect(self.buttonOK,
QtCore.SIGNAL("clicked()"), self.onOK)
self.connect(self.buttonAbbrechen,
QtCore.SIGNAL("clicked()"), self.onClose)
self.connect(self.Button,
QtCore.SIGNAL("clicked()"), self.on_Button_clicked)
def on_Button_clicked(self, checked=None):
if checked==None: return
dialog = QtGui.QDialog()
dialog.ui = Ui_MyDialog()
dialog.ui.setupUi(dialog)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
some_list=["A","B","C"]
#a list in another python class from another script that changes so
#the table properties have to be changed dynamically
#here I just take a simple list as an example
#the following two lines do not work (they work if tableCo is an
#object in the Main Dialog
self.tableCo.setColumnCount(len(some_list))
self.tableCo.setHorizontalHeaderLabels(some_list)
def onOK:
...
def onClose:
...
If I push the button i see my "tableCo" widget, but the properties of the header have not changed, and after closing this sub-dialog I get the following error-message
Traceback (most recent call last):
File "C:/gui.py", line 88, in on_Button_clicked
self.tableCo.setColumnCount(len(some_list))
AttributeError: 'MainDialog' object has no attribute 'tableCo'
What do i have to change in my code to configure a Widget in a sub-Dialog?
There are two problems with the code in your on_Button_clicked.
Firstly, you are attempting to call methods after the dialog has closed. When exec_ is called, the dialog enters a blocking loop until the user closes the dialog. When the dialog closes, the following lines will get executed, but the dialog will be immediately garbage-collected after that when the function returns.
Secondly, you are attempting to access methods of the dialog using self, rather than via the local name dialog, which is why you are getting the AttributeError.
You can fix these problems by creating a subclass for the second dialog in the same way that you have for your MainDialog class:
class SubDialog(QtGui.QDialog, Ui_MyDialog):
def __init__(self, some_list, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.tableCo.setColumnCount(len(some_list))
self.tableCo.setHorizontalHeaderLabels(some_list)
class MainDialog(QtGui.QDialog, Dlg):
...
def on_Button_clicked(self, checked=None):
if checked is None: return
dialog = SubQDialog(some_list)
dialog.exec_()
are you sure tableCo has that exact name and it's parented directly to the MainWindow? Seems like the properties are not being updated simply because there is no self.tableCo.
I've created a custom widget in pyqt4 that I've worked on and tested and now am ready to load it into my main window. Since it doesn't show up in designer, I need to manually add it to my main window manually.
My widget uses uic to load the ui file instead of converting it to a py file (it's been quicker less hassle so far) so it looks something like this:
class widgetWindow(QtGui.QWidget):
def __init__(self, parent = None):
super(widgetWindow, self).__init__(parent)
self.ui = uic.loadUi("widget.ui")
#everything else
now in my main class (example for brevity) I create the layout, add the widget to the layout and then add it to the main widget
class main(QtGui.QMainWindow):
def __init__(self, parent = None):
super(main, self).__init__(parent)
self.ui = uic.loadUi("testWindow.ui")
mainlayout = QtGui.QVBoxLayout()
window = widgetWindow(self)
mainlayout.addWidget(window)
centerWidget = QtGui.QWidget()
centerWidget.setLayout(mainlayout)
self.ui.setCentralWidget(centerWidget)
There are no errors thrown, and it will make space for the widget, but it simply won't show anything.
adding in the line window.ui.show() will just pop open a new window overtop the space that it should be occupying on the main window. What am I missing?
Doing some more research into the uic loader, there are two ways to load a ui file. The way I'm using it in the question is one way, the other way is with the uic.loadUiType(). This creates both the base class and the form class to be inherited by the class object instead of just the QtGui.QWidget class object.
widgetForm, baseClass= uic.loadUiType("addTilesWidget.ui")
class windowTest(baseClass, widgetForm):
def __init__(self, parent = None):
super(windowTest, self).__init__(parent)
self.setupUi(self)
This way, the widget can be loaded into another form as expected. As for exactly why, I haven't found that answer yet.
Some more info on the different setup types: http://bitesofcode.blogspot.com/2011/10/comparison-of-loading-techniques.html
Try to add the parent argument into the loadUi statements:
self.ui = uic.loadUi("widget.ui",parent)
self.ui = uic.loadUi("testWindow.ui",self)
And try the following line at the end of your main class.
self.setCentralWidget(centerWidget)
You need to specify that 'centerWidget' is the central widget of the main window.
i.e your code for class main should be have a line like:
self.setCentralWidget(centerWidget)
class main(QMainWindow):
def __init__(self, parent = None):
super(main, self).__init__(parent)
....
self.setCentralWidget(centerWidget)
I'm trying to use pyqt to show a custom QDialog window when a button on a QMainWindow is clicked. I keep getting the following error:
$ python main.py
DEBUG: Launch edit window
Traceback (most recent call last):
File "/home/james/Dropbox/Database/qt/ui_med.py", line 23, in launchEditWindow
dialog = Ui_Dialog(c)
File "/home/james/Dropbox/Database/qt/ui_edit.py", line 15, in __init__
QtGui.QDialog.__init__(self)
TypeError: descriptor '__init__' requires a 'sip.simplewrapper' object but received a 'Ui_Dialog'
I've gone over several online tutorials, but most of them stop just short of showing how to use a non built-in dialog window. I generated the code for both the main window and the dialog using pyuic4. What I think should be the relevant code is below. What am I missing here?
class Ui_Dialog(object):
def __init__(self, dbConnection):
QtGui.QDialog.__init__(self)
global c
c = dbConnection
class Ui_MainWindow(object):
def __init__(self, dbConnection):
global c
c = dbConnection
def launchEditWindow(self):
print "DEBUG: Launch edit window"
dialog = QtGui.QDialog()
dialogui = Ui_Dialog(c)
dialogui = setupUi(dialog)
dialogui.show()
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
conn = sqlite3.connect('meds.sqlite')
c = conn.cursor()
self.ui = Ui_MainWindow(c)
self.ui.setupUi(self)
def main():
app = QtGui.QApplication(sys.argv)
program = StartQT4()
program.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Bonus question: since it looks like you can't pass arguments in pyqt function callbacks, is setting something which would otherwise be passed as an argument (the poorly named "c") to be global the best way to get information into those functions?
I've done like this in the past, and i can tell it works.
assuming your button is called "Button"
class Main(QtGui.QMainWindow):
''' some stuff '''
def on_Button_clicked(self, checked=None):
if checked==None: return
dialog = QDialog()
dialog.ui = Ui_MyDialog()
dialog.ui.setupUi(dialog)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
This works for my application, and I believe it should work with yours as well. hope it'll help, it should be pretty straight forward to do the few changes needed to apply it to your case.
have a good day everybody.
Ui_Dialog should inherent from QtGui.QDialog, not object.
class Ui_Dialog(QtGui.QDialog):
def __init__(self, dbConnection):
QtGui.QDialog.__init__(self)
global c
c = dbConnection
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
Why QtGui.QWidget.__init___ ???
Use insted:
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
You must call __init__ methon from base class (name in parenthesis '()')
QDialog have two useful routins:
exec_()
show()
First wait for closing dialog and then you can access any field form dialog. Second show dialog but don't wait, so to work properly you must set some slot/signals connections to respond for dialog actions.
eg. for exec_():
class Dialog(QDialog):
def __init__(self, parent):
QDialog.__init__(parent)
line_edit = QLineEdit()
...
dialog = Dialog()
if dialog.exec_(): # here dialog will be shown and main script will wait for its closing (with no errors)
data = dialog.line_edit.text()
Small tip: can you change your ui classes into widgets (with layouts). And perhaps problem is that your __init__ should be __init__(self, parent=None, dbConnection)
Because when you create new widget in existing one PyQt may try to set it as children of existing one. (So change all init to have additional parent param (must be on second position)).