Issue
I have been creating an application which consists of two tabs. To understand more easily what I want, in Tab1 there is a Start button and by clicking on that I want a table to be added in tab2. There are already some widgets in Tab1 and Tab2 which should be left unchanged. The table should be added to a GroupBox.
import sys
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Application'
self.setWindowTitle(self.title)
self.tab_widget = MyTabWidget(self)
self.setCentralWidget(self.tab_widget)
self.showFullScreen()
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
#Initialize tab screen
self.tabs = QTabWidget()
self.one_tab = TabOne()
self.two_tab = TabTwo()
self.tabs.resize(300,200)
#Add tabs
self.tabs.addTab(self.map_tab,QIcon("./icons and images/tab_map.png"), ('Tab1'))
self.tabs.addTab(self.address_tab,QIcon("./icons and images/tab_addresses.png"), ('Addresses'))
#Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
class TabOne(QWidget):
def __init__(self):
super().__init__()
self.startButton()
#Push button at the center
def startButton(self):
button = QPushButton("Start", self)
button.move(655, 415)
button.clicked.connect(self.main)
self.show()
def main():
#Wants to add A table in Tab2
class TabTwo(QWidget):
def __init__(self):
super().__init__()
#Want to add a table after clicking the start button
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
I will add a Table with the help of a JSON file.
Solution
You have to learn to separate the parts, the method that creates the table in TabTwo should not be done in TabOne, and why shouldn't that task be in TabOne? Imagine that you must add the table (or do another action) to another tab when the button is pressed, because if you continue with your logic you will have to rewrite that task which is not scalable.
The idea is to use a signal to notify that a table should be added and connected to a TabTwo method that creates and adds the table.
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
lay = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.one_tab = TabOne()
self.two_tab = TabTwo()
self.one_tab.clicked.connect(self.two_tab.add_table)
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(
self.one_tab, QIcon("./icons and images/tab_map.png"), ("Tab1")
)
self.tabs.addTab(
self.two_tab, QIcon("./icons and images/tab_addresses.png"), ("Addresses")
)
lay.addWidget(self.tabs)
class TabOne(QWidget):
clicked = pyqtSignal()
def __init__(self):
super().__init__()
self.startButton()
def startButton(self):
button = QPushButton("Start", self)
button.move(655, 415)
button.clicked.connect(self.clicked)
self.show()
class TabTwo(QWidget):
def __init__(self):
super().__init__()
self._lay = QVBoxLayout(self)
@pyqtSlot()
def add_table(self):
table = QTableWidget(4, 4)
self._lay.addWidget(table)
Update:
If additional information is required, then when creating the signal, the types of data to be transmitted must be established:
class MyTabWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
lay = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.one_tab = TabOne()
self.two_tab = TabTwo()
self.one_tab.clicked.connect(self.two_tab.add_table)
self.tabs.resize(300, 200)
# Add tabs
self.tabs.addTab(
self.one_tab, QIcon("./icons and images/tab_map.png"), ("Tab1")
)
self.tabs.addTab(
self.two_tab, QIcon("./icons and images/tab_addresses.png"), ("Addresses")
)
lay.addWidget(self.tabs)
class TabOne(QWidget):
clicked = pyqtSignal(int, int)
def __init__(self):
super().__init__()
self.startButton()
def startButton(self):
button = QPushButton("Start", self)
button.move(655, 415)
button.clicked.connect(self.onClicked)
self.show()
@pyqtSlot()
def onClicked(self):
self.clicked.emit(5, 5)
class TabTwo(QWidget):
def __init__(self):
super().__init__()
self._lay = QVBoxLayout(self)
@pyqtSlot(int, int)
def add_table(self, rows, columns):
table = QTableWidget(rows, columns)
self._lay.addWidget(table)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.