In my application I have 2 QMainWindows. The UI file generated with pyuic5 and stored in separate files. When I'm clicked button in MainWindow opens a second window called BlockWindow. In this window in init func stored method "loaddata" and its loads the data in QTableWidget. However when you open second window nothing happens. But if you are debug separately file BlockWindow(second window) it works perfectly. Can you suggest what the problem actually is?
MainWindow.py
from MainWindowUI import Ui_MainWindow
from BlockWindowUI import Ui_BlockWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.loaddata)
self.ui.action.triggered.connect(self.openJournal)
self.ui.action_4.triggered.connect(exit)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT nn, name, metering, volume, date_end, responsible, txt_name from dbo.Card_plan"
result = conn.execute(query).fetchall()
self.ui.tableWidget.clearSelection()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
def openJournal(self):
self.BlockWindow = QtWidgets.QMainWindow()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self.BlockWindow)
self.BlockWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
BlockWindow.py
from BlockWindowUI import *
class BlockWindow(QtWidgets.QMainWindow, Ui_BlockWindow):
def __init__(self):
super(BlockWindow, self).__init__()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self)
self.ui.pushButton_3.clicked.connect(self.dels)
self.ui.pushButton_4.clicked.connect(BlockWindow.close)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT * from dbo.Block"
result = conn.execute(query).fetchall()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
CodePudding user response:
In your MainWindow
class try changing your openJournal
method to this:
def openJournal(self):
self.BlockWindow = BlockWindow()
self.BlockWindow.show()
And don't forget to import your class to MainWindow.py:
from BlockWindow import BlockWindow