Issue
I can't show DataEntryForm class in App class, ie inside QMainWindow. thank you so much.
#QWidget, The widget containing the QTableWidget I want to show
class DataEntryForm(QWidget):
def __int__(self):
super().__init__()
self.item = 0
self.data = {"Phone L": 50.5}
# leftside
self.table = QTableWidget()
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels('Descriptions', 'Price')
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.layout = QHBoxLayout()
self.layout.addWidget(self.table, 50)
self.setLayout(self.layout)
#QMainWindow, I want to show the Data Entry Form class in this class.
class App(QMainWindow):
def __init__(self, widget):
super().__init__()
self.title = 'Hello, world!'
self.setWindowTitle(self.title)
self.resize(1200, 600)
self.menuBar = self.menuBar()
self.fileMenu = self.menuBar.addMenu('File')
# export to csv file action
exportAction = QAction('Export to csv', self)
exportAction.setShortcut('Ctrl+E')
self.setWindowIcon(QIcon('data-analysis_icon-icons.com_52842.ico'))
# exportAction.triggered.connect
# exit Action
exitAction = QAction('Exit', self)
exitAction.setShortcut("Ctrl+Q")
exitAction.triggered.connect(lambda: app.quit())
self.fileMenu.addAction(exportAction)
self.fileMenu.addAction(exitAction)
if __name__ == '__main__':
app = QApplication(sys.argv)
x = DataEntryForm()
ex = App(x)
ex.show()
sys.exit(app.exec_())
Solution
QMainWindow is a special type of QWidget that uses a central widget that shows its main content. While you're correctly creating an instance of your DataEntryForm
, you're just adding that as an argument in the main window constructor, but you're doing nothing with it.
In order to use that widget, use setCentralWidget()
:
class App(QMainWindow):
def __init__(self, widget):
super().__init__()
self.setCentralWidget(widget)
# ...
Tip: avoid similar names for classes and instances of different types; app
is an instance of a QApplication, App
is a QMainWindow class. Use something different and more related for that class, like "MainWindow".
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.