Search code examples
pythonqwidgetpyside2

Absolute Position Widgets


Very confused, I've viewed multiple other stack overflow pages:

class launcherWidget( QMainWindow ) :
    def __init__(self) :
        QMainWindow.__init__(self)
        self.setWindowTitle("Scouting Software Launcher")
        #self.setGeometry(100, 100, 100, 100)
        self.table = QTableWidget()
        self.table.setColumnCount(3)
        self.table.setHorizontalHeaderLabels(["Save Name", "Mod Date","Creation Date"])
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        #self.layout = QHBoxLayout()
        #self.layout.addWidget(self.table)
        #self.setLayout(self.layout)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.table.verticalHeader().setVisible(False)
        self.fill_table_with_list([["savename","mod date","creat"],["c","b","a"]])
        #self.table.resize(100,100)
        self.table.setGeometry( QRect( 50, 50, 100, 100 ) )
        #self.table.move(50,50)
        #self.show()
        self.b0 = QPushButton("New")
        self.b0.clicked.connect(self.newClicked)
        #self.layout.addWidget(self.b0,)
        #self.b0.move(0,0)
        self.b1 = QPushButton("Rename")
        self.b1.clicked.connect(self.renameClicked)
        #self.layout.addWidget(self.b1)
        #self.b1.move(300,500)
    def newClicked(self) :
        print("placeholder")
    def renameClicked(self):
        r = self.table.currentRow() 
        savename = str( self.table.item(r,0).text() )           
    def fill_table_with_list (self, data=[[]]) : 
        for row in reversed(data) :
            saveName = QTableWidgetItem(row[0])
            modDate = QTableWidgetItem(row[1])
            creaDate =QTableWidgetItem(row[2])
            self.table.insertRow(0)
            self.table.setItem(0, 0, saveName)
            self.table.setItem(0, 1, modDate) 
            self.table.setItem(0, 2, creaDate)   
    @Slot()
    def exit_app(self, checked):
        QApplication.quit()
def onLaunch () :
    app = QApplication(sys.argv)
    window = launcherWidget() # screen = before change
    #window.resize(500, 300) #can always be fullscreened
    window.resize(1820,980)
    window.show()
    sys.exit(app.exec_())
if __name__ == "__main__": 
    onLaunch()

Trying to use absolute positioning and I can't get my table to show up, All I get is a blank window with nothing in it. I'd prefer to not have to use a layout, but so far that seems like the only way to get the table and buttons to show up. All the things I've commented out are attempts at making it work. table.show() seems to just show the table in a separate window. .move() seems to do nothing and recently tried setGeometry() which also doesn't work. I haven't even tried getting the buttons to show up yet, I can't imagine I'd get a different result. I first assumed I just had the cordinates wrong, but I've gone through trial and error to rule that one out


Solution

  • TL; DR; Pass as a parent of the widgets to the window


    A widget is drawn on another widget if it is the son of the widget, in your case neither the QTableWidget nor the QPushButton, in the case of layouts apart from handling the position and size they also establish as the parent of the widgets that it handles to the widget where it is settled down.

    class launcherWidget(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.setWindowTitle("Scouting Software Launcher")
            self.table = QTableWidget(self)
            self.table.setColumnCount(3)
            self.table.setHorizontalHeaderLabels(["Save Name", "Mod Date", "Creation Date"])
            self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
            self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
            self.table.verticalHeader().setVisible(False)
            self.fill_table_with_list([["savename", "mod date", "creat"], ["c", "b", "a"]])
            self.table.setGeometry(QRect(50, 50, 100, 100))
            self.b0 = QPushButton("New", self)
            self.b0.clicked.connect(self.newClicked)
            self.b0.move(0,0)
            self.b1 = QPushButton("Rename", self)
            self.b1.clicked.connect(self.renameClicked)
            self.b1.move(300,500)
        # ...