简体   繁体   中英

PyQt QMainwindow call QListWidget from a different file

Dears,

I have a problem with PyQt. For several reasons i have to seperate the QListWidget from the main (qt.py) file. When i run my code only the "mainwindow" shows. It's like the list is didn't even called:

qt.py:

from qt_child import *

class mainwindow(QMainWindow):
    def __init__(self):
        super(mainwindow, self).__init__()
        self.setGeometry(100,100,500,500)
        self.lw = ListWidget()

def window():
    app = QApplication(sys.argv)
    w = mainwindow()   
    
    w.setWindowTitle("PyQt Main")
    w.show()

    sys.exit(app.exec_())
    
if __name__ == '__main__':
   window()

qt_child.py:

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QMainWindow, QListWidget

class ListWidget(QListWidget):
    def __init__(self):
        super(ListWidget, self).__init__()
        self.resize(300,100)
        self.addItem("Item 1")
        self.addItem("Item 2")
        self.addItem("Item 3")
        self.addItem("Item 4")

change these rows

self.lw = ListWidget()
def __init__(self):
super(ListWidget, self).__init__()

to

self.lw = ListWidget(self)
def __init__(self, parent=None):
super(ListWidget, self).__init__(parent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM