I setup a Main window with below codes :
import Main_BookSpace
class MainWindowClass(QtWidgets.QMainWindow, Ui_MainWindow, QtWidgets.QAction):
def __init__(self):
super(MainWindowClass, self).__init__()
self.setupUi(self)
# SQL Database relate
self.database = Main_database.DataBaseRelate()
# init space booking sheet
self.space_booking_class = Main_BookSpace.BookingSpaceClass(self.tableWidget, self.database)
self.tableWidget.itemClicked.connect(self.space_booking_class.table_click_action)
In another python file , there is a class: BookingSpaceClass.
In this class I will get some data from database , and put these data into the table widget in the Main window :
class BookingSpaceClass:
def __init__(self, theTable, database):
self.theTable = theTable
self.database = database
def table_click_action(self):
indexes = self.theTable.selectionModel().selectedIndexes()
data_id = self.theTable.item(indexes[0].row(), 23).text()
return_data = self.database.read_data_with_id("spacebooking", data_id)
print(return_data)
How can I transfer the data which get from table_click_action to Main window's widget?
It's not clear from your example why the BookingSpaceClass needs to exist at all. It's just storing references to the exact same gui widgets that the main class does. The table_click_action method could just as easily be on the main class. You should probably connect the itemClicked signal to a slot on the main class and move everything from BookingSpaceClass into the main class, or at least call the BookingSpaceClass helper methods from the main class instead of using signals.
self.tableWidget.itemClicked.connect(self.on_itemClicked)
#QtCore.pyqtSlot()
def on_itemClicked(self)
return_data = self.space_booking_class.table_click_action()
Related
I want to call a father method inside a child class method but I'm having some trouble and missing some points.
I'm able to call parent's method just inside def__init__(self)
How to solve?
I tried:
class MainWindow(QMainWindow):
ws = websocket.WebSocket()
threadpool = QThreadPool()
pippo = "pippo"
def __init__(self):
super().__init__()
tabs = QTabWidget()
tabs.setTabPosition(QTabWidget.North)
tabs.setMovable(False)
tabs.setDocumentMode(True)
self.voices = Widget("Voices", self)
tabs.addTab(self.voices, "Voices")
self.setCentralWidget(tabs)
def do_something(self):
print ('doing something!')
class Widget(QWidget):
def __init__(self, name, parent=None):
super().__init__(parent)
self.ui = Ui_Widget()
self.ui.setupUi(self)
self.name=name
self.lan = str(config_ini("language"))
self.username = str(config_ini("user"))
print(self.parent().pippo) #HERE MY CODE WORKS
self.parent().do_something() #HERE MY CODE WORKS
self.ui.InitializeButton.clicked.connect(self.clickedInitialize)
#Pressione tasto Initialize
def clickedInitialize(self):
self.parent().do_something() #HERE MY CODE ___DON'T___ WORKS
and I receive this error:
Traceback (most recent call last):
File "C:\Users\IENOGIUS\Documents\Cefla\VoiceCommands\VoiceChecker\voicechecker.py", line 294, in clickedInitialize
self.parent().do_something()
AttributeError: 'PySide6.QtWidgets.QStackedWidget' object has no attribute 'do_something'
As the addTab() documentation explains:
Ownership of page is passed on to the QTabWidget.
This would be true anyway whenever you add a widget to another, making it a child of that new parent.
In this case, it happens when tabs.addTab(self.voices, "Voices"), which reparents the widget to the tab widget (actually, its internal QStackedWidget): the window becomes the ancestor of the Widget instance, the actual parent is the stacked widget.
The structure will be the following:
MainWindow
QTabWidget
QStackedWidget (internally used by QTabWidget to display pages)
Widget
If you want to keep a reference to the main window, just use an instance attribute in the __init__ (eg. self.mainWindow = parent).
Note, though, that child objects should never directly call methods of their ancestors (see "separation of concerns"), and signals also exist for this very reason.
What you should actually do is to connect the signal from the window:
class MainWindow(QMainWindow):
def __init__(self):
# ...
self.voices = Widget("Voices", self)
self.voices.ui.InitializeButton.clicked.connect(self.do_something)
And obviously remove that connection from Widget.
Alternatively, a better approach should use a custom signal instead:
class Widget(QWidget):
do_something_signal = Signal()
# ...
def clickedInitialize(self):
# do whatever you need, then:
self.do_something_signal.emit()
class MainWindow(QMainWindow):
def __init__(self):
# ...
self.voices.do_something_signal.connect(self.do_something)
I suggest you to read more about the Qt object trees and ownerships and also possible proper usage of signals in complex object structures in this related question.
Note: please avoid using code comments that are not necessary for the understanding of the code, especially if in languages that are not English. And, really, don't use "pippo".
i have a main GUI-Window from which i open a new Window (FCT-popup) with a buttonclick:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow() # sets ui = to the main window from the ui-file
self.ui.setupUi(self)
[...]
def enter_fct_results(self):
self.FCTpopup = FCT_Window()
self.FCTpopup.show()
In the Window i have a QTable to fill and a button to submit the data and close the window:
class FCT_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_FCT_Window()
self.ui.setupUi(self)
[...]
self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))
def on_submit(self): # event when user clicks
fct_nparray = np.zeros((self.ui.tableFCTinputs.rowCount(), self.ui.tableFCTinputs.columnCount()))
for j in range(self.ui.tableFCTinputs.columnCount()):
for i in range(self.ui.tableFCTinputs.rowCount()):
fct_nparray[i, j] = float(self.ui.tableFCTinputs.item(i, j).text())
return fct_nparray, lambda: self.close()
self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))
The receiving function iin the main window looks like ths:
def store_fct_data(self, data):
self.fct_data = data
Now i just want to understand how i can make either the mainwindow or the pushbutton which opens the 2nd window disabled. Disabling inside enter_fct_results() works, but if i want to enable it again with either store_fct_data or on_submit provides errors like this:
self.ui.pushButton_FCTresults.setEnabled(1)
self.ui.pushButton_submitFCT.clicked.connect(lambda: MainWindow.store_fct_data(MainWindow, self.on_submit()[0]))
AttributeError: type object 'MainWindow' has no attribute 'ui'
I dont think i have understood it here how to deal with multiple windows and stuff. For example how would i change a the color of a button in the main window by using a button in window2. How do i access the widgets? if i am inside the same Window i do that easily by
self.ui.pushbutton.setText("New Text")
I dont get how to access items and attributes across Windows :( Can you help me?
Access to attributes of another instance
There is a fundamental difference between disabling the button of the second window in enter_fct_results and what you tried in the lambda: in the first case, you're accessing an instance attribute (for instance, self.FCTpopup.ui.pushButton), while in the second you're trying to access a class attribute.
The self.ui object is created in the __init__ (when the class instance is created), so the instance will have an ui attribute, not the class:
class Test:
def __init__(self):
self.value = True
test = Test()
print(test.value)
>>> True
print(Test.value)
>>> AttributeError: type object 'Test' has no attribute 'value'
Provide a reference
The simple solution is to create a reference of the instance of the first window for the second:
def enter_fct_results(self):
self.FCTpopup = FCT_Window(self)
self.FCTpopup.show()
class FCT_Window(QMainWindow):
def __init__(self, mainWindow):
QMainWindow.__init__(self)
self.mainWindow = mainWindow
self.ui.pushButton_submitFCT.clicked.connect(self.doSomething)
def doSomething(self):
# ...
self.mainWindow.ui.pushButton.setEnabled(True)
Using modal windows (aka, dialogs)
Whenever a window is required for some temporary interaction (data input, document preview, etc), a dialog is preferred: the main benefit of using dialogs is that they are modal to the parent, preventing interaction on that parent until the dialog is closed; another benefit is that (at least on Qt) they also have a blocking event loop within their exec() function, which will only return as soon as the dialog is closed. Both of these aspects also make unnecessary disabling any button in the parent window. Another important reason is that QMainWindow is not intended for this kind of operation, also because it has features that generally unnecessary for that (toolbars, statusbars, menus, etc).
def enter_fct_results(self):
self.FCTpopup = FCT_Window(self)
self.FCTpopup.exec_()
class FCT_Window(QDialog):
def __init__(self, parent):
QMainWindow.__init__(self, parent)
self.ui.pushButton_submitFCT.clicked.connect(self.doSomething)
def doSomething(self):
# ...
self.accept()
The above makes mandatory to recreate the ui in designer using a QDialog (and not a QMainWindow) instead. You can just create a new one and drag&drop widgets from the original one.
i finally found my mistake: It was the place of the signal connection. It has to be right before the 2nd window is opened:
def enter_fct_results(self):
self.FCTpopup = Dialog(self.fct_data)
self.FCTpopup.submitted.connect(self.store_fct_data)
self.FCTpopup.exec_()
With this now i can send the stored data from the mainwindow to the opened window, import into the table, edit it and send it back to the main window on submit.
I have the following problem with my pyqt:
Assuming i create an object within the Qt Designer and save it as an .ui file. Then i use pyuic to convert it to an .py file. Because i want to integrate a new module into a given program, this is the favorite way to go (because later the .ui files will be converted automatically at startup to .py files).
If i have a look at my .py file i see the following for the window:
class Ui_SubWindow(object):
def setupUi(self, SubWindow):
SubWindow.setObjectName(_fromUtf8("SubWindow"))
....
i have a RemoteWindow class as MainWindow where the SubWindow is initiated:
class RemoteWindow(QtGui.QMainWindow):
def __init__(self, subcore):
super(RemoteWindow, self).__init__(subcore.core.gui)
self.subcore = subcore
self.ui = Ui_SubWindow()
Now i have a core program:
class SubCore(object):
def __init__(self, core, identity, number):
...
self.gui = RemoteWindow(self)
self.newController = NewController(self.gui)
and the new controller class:
class NewController(object):
def __init__(self, subwindow):
self.subwindow = subwindow
self.ui = subwindow.ui
from my controller i want to call a .findChild() on that window
submitFrame = self.ui.findChild(QtGui.QFrame, "frameSubmit")
, but all i get is an:
AttributeError: 'Ui_SubWindow' object has no attribute 'findChild'
I assume this is, because the class Ui_SubWindow is not a child class of some QObject but of an object, am i right?
self.ui is the same as subwindow.ui, where subwindow is an instance of RemoteWindow which has as .ui argument the Ui_SubWindow Class.
Is there any chance to make the pyuic or the Qt Designer to make this SubWindow a child of QObject, without manipulating the automatically generated .py file?
You don't need to use findChild at all, because pyuic will automatically create attributes in the ui object for all the widgets defined in Qt Designer. The attribute names are taken from the objectName. So all you need is:
submitFrame = self.ui.frameSubmit
Qt Designer creates a design, that is, a class that serves to fill the main widget so the internal widgets are not children of this class, if you want to use findChild you must do it to the widget that returns this class after calling the method setupUi.
class RemoteWindow(QtGui.QMainWindow):
def __init__(self, subcore):
super(RemoteWindow, self).__init__(subcore.core.gui)
self.subcore = subcore
self.ui = Ui_SubWindow()
self.ui.setupUi(self) # Here the widget is filled
[...]
And then you should use it in the following way:
class NewController(object):
def __init__(self, subwindow):
self.subwindow = subwindow
submitFrame = self.subwindow.findChild(QtGui.QFrame, "frameSubmit")
[...]
I recently had to do this same thing and leaving this here in case someone sees this link vs the other related one.
https://stackoverflow.com/a/62340205/1621381
for name, obj in dict(self.__dict__).items():
# print(str(name) + str(obj))
obj_type = str(obj).strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
# obj_type = str(obj).strip("<").rsplit(" ")[0]
# print(obj_type)
# obj_type = obj_str.strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
label_name = "self." + str(name)
try:
label_name = self.findChild(eval(obj_type), name)
print(str(label_name) + ' created')
except:
pass
if not isinstance(obj_type, QObject):
continue
I am designing an application using PyQt that will manage multiple instances of Selenium. Each instance has a QFrame with unique information and controls and can be tabbed through from the main window.
class Instance(QFrame):
def __init__(self):
super().__init__()
self.username = "whatever"
...
self.startButton = QPushButton('Start')
self.startButton.clicked.connect(lambda: self.engineStart())
self.exitButton = QPushButton('Exit')
self.exitButton.clicked.connect(lambda: self.engineExit())
...
How it looks
Users should be able to create and delete instances at will.
Creating a tab is no problem. I have a "+" button set as the QTabWidget's cornerWidget. It is connected to a simple method to add the tab.
class App(QFrame):
def __init__(self):
...
def addNewTab(self):
t = Instance()
self.tabs.addTab(t, t.username)
The problem is, how can I use the "Exit" button from the "inside" Instance class to remove the tabs that are managed from the main window's "outside" class? I need some way to call removeTab()
To do what you want you must create a slot in the main window, and connect it to the clicked signal of the button as shown below:
class App(QFrame):
def __init__(self):
...
def addNewTab(self):
t = Instance()
self.tabs.addTab(t, t.username)
t.exitButton.clicked.connect(self.slot)
def slot(self):
self.tabs.removeTab(your_index)
I am using PyQt4 in Python 3.3, making a GUI and have multiple classes, some of which I don't want running until I have clicked a certain button to do so. How can I connect such a class to only run when the button is clicked, and not on the start-up of the program.
Here is how I am currently connecting this class to my button within another class.
btns.clicked.connect(self.tableshow2)
def tableshow2(self):
table5.show()
This is the first class where the button is.
class CustTableSearch(QtGui.QDialog):
def __init__(self, parent=None):
super(CustTableSearch, self).__init__(parent)
with sqlite3.connect('database.db') as db:
cursor=db.cursor()
num = QtGui.QInputDialog.getText(self, 'Insert TelephoneNumber',
'Enter TeleNum:')
table5 = CustTableSearch()
This is part of the class which the button activates, which runs on start-up of the python shell. I have tried putting this in a function within the class with the button, but then i can't have it show up using .show() (it's a screen with a table).
Assuming that both classes are in the same module, you can create an instance of CustomTableSearch in tableshow2(self) method.
...
def tableshow2(self):
self.table5 = CustomTableSearch(self)
self.table5.show()
...
One way to do this is to create the dialog only on demand, rather than creating it straight away when the module is loaded.
class ProfilePage(QtGui.QMainWindow):
def __init__(self):
super(ProfilePage, self).__init__()
self.table5 = None
self.initUI()
def initUI(self):
...
btns.clicked.connect(self.tableshow2)
def tableshow2(self):
if self.table5 is None:
self.table5 = CustomTableSearch()
self.table5.show()