QTableWidget子窗口如何放在父窗口前面或父窗口下面?

Posted

技术标签:

【中文标题】QTableWidget子窗口如何放在父窗口前面或父窗口下面?【英文标题】:How to put QTableWidget child window in front of parent window or below the parent window? 【发布时间】:2020-10-13 17:19:32 【问题描述】:

我正在尝试制作一个简单的输入表单,它接受某些输入并在表格中显示输出。 根据代码,我得到以下输出。

但我正在寻找以下输出。

以下是我尝试过的代码sn-p。

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *

# Subclass QMainWindow to customize your application's main window
class MainWindow(QWidget):

        def __init__(self):
                super().__init__()

                self.initUI()

        def initUI(self):

                self.setWindowTitle("Check Distance Travelled By Vehicles")

                self.vehicleNamelbl = QLabel('VehicleName : ')
                self.vehicleNamecombo = QComboBox()
                self.vehicleNamecombo.addItem('SwitftDzire')

                self.dateEdit    = QDateEdit()
                self.dateEdit.__init__(calendarPopup=True)
                self.dateEdit.setDateTime(QtCore.QDateTime.currentDateTime())
                self.dateEdit.editingFinished.connect(lambda: date_method())

                self.petrolCB = QCheckBox('Petrol')
                self.petrolCB.setChecked(True)
                self.dieselCB = QCheckBox('Diesel')
                self.dieselCB.setChecked(False)

                self.petrolCB.stateChanged.connect(self.checkpetrolCB)
                self.dieselCB.stateChanged.connect(self.checkdieselCB)

                self.submitBtn    = QPushButton('Submit ')
                # adding action to the date when enter key is pressed
                self.submitBtn.clicked[bool].connect(self.collecInput)

                grid = QGridLayout()
                grid.setSpacing(10)

                grid.addWidget(self.vehicleNamelbl,1,0)
                grid.addWidget(self.vehicleNamecombo,1,1)
                grid.addWidget(self.dateEdit,1,2)
                grid.addWidget(self.petrolCB,1,3)
                grid.addWidget(self.dieselCB,1,4)
                grid.addWidget(self.submitBtn,1,5)

                # geometry of the main window
                qtRectangle = self.frameGeometry()
                # center point of screen
                centerPoint = QDesktopWidget().availableGeometry().center()
                # move rectangle's center point to screen's center point
                qtRectangle.moveCenter(centerPoint)
                 # top left of rectangle becomes top left of window centering it
                self.move(qtRectangle.topLeft())

                self.setLayout(grid)
                self.show()

                # method called by date edit 
                def date_method():
                        print('Inside date_method')
                        # getting the date 
                        value = self.dateEdit.date()
                        print(value)
                        print(value.toPyDate())

        def checkpetrolCB(self,checked):
                if checked :
                        print('Petrol Vehicle Is Selected')
                        self.OdFlag = 1
                else:
                        print('Petrol Vehicle Is De-Selected')

        def checkdieselCB(self,checked):
                if checked :
                        print('Diesel Vehicle Is Selected')
                        self.OdFlag = 2
                else:
                        print('Diesel Vehicle Is De-Selected')

        def collecInput(self) :

                print('Submit Button Pressed!! Inputs Selected')

                print(self.vehicleNamecombo.currentText())
                print(self.dateEdit.date().toPyDate())

                if self.petrolCB.isChecked() == True and self.dieselCB.isChecked() == False :
                    print('Petrol Vehicle Is Selected')

                if self.dieselCB.isChecked() == True and self.petrolCB.isChecked() == False :
                    print('Diesel Vehicle Is Selected')

                if self.petrolCB.isChecked() == True and self.dieselCB.isChecked() == True :
                    print('Both Petrol And Diesel Vehicle Are Selected')
                    msgBox = QMessageBox()
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setText('Select Either Petrol Or Diesel')
                    msgBox.setWindowTitle("Alert PopUp")
                    returnValue = msgBox.exec_()
                    return

                # Call A Module To Get The List Of Files Present As per The Input
                vehicleFileList = [ 'dist_SwitftDzire_CityA.13OCT2020','dist_SwitftDzire_CityB.13OCT2020','dist_SwitftDzire_CityC.13OCT2020']

                print('Back to collecInput')
                print(vehicleFileList)
                print('Num Of Files Found : '+str(len(vehicleFileList)))

                numOfFiles = len(vehicleFileList)

                if numOfFiles == 0 : # No Files Have Been Detected
                    print('No Files Found')
                    msgBox = QMessageBox()
                    msgBox.setIcon(QMessageBox.Critical)
                    msgBox.setText('No Files Found')
                    msgBox.setWindowTitle("Alert PopUp")
                    returnValue = msgBox.exec_()

                else: # Atleast 1 File Is Detected
                    print('Populating table entries')
                    table = MyTable(vehicleFileList, numOfFiles, 2, self)
                    # add Qt.Window to table's flags 
                    table.setWindowFlags(table.windowFlags() | Qt.Window)
                    table.show()
class MyTable(QTableWidget):
        def __init__(self, vehicleFileList, *args):
                QTableWidget.__init__(self, *args)
                self.data = vehicleFileList
                self.setWindowFlags(Qt.WindowStaysOnTopHint)
                self.resizeColumnsToContents()
                self.resizeRowsToContents()
                self.horizontalHeader().setStretchLastSection(False)
                self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                self.setHorizontalHeaderLabels(['Available Vehicle Data Files','Missing Files'])

                print('Inside MyTable')
                print(vehicleFileList)

                rowCount=0

                for item in vehicleFileList :
                        print(item)
                        self.setItem(rowCount,0,QTableWidgetItem(item))
                        rowCount+=1


def main():
        app = QApplication(sys.argv)
        window = MainWindow()
        sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我需要进行哪些更改才能获得我选择的窗口定位?

【问题讨论】:

【参考方案1】:

注意:第二个窗口不是第一个窗口的子窗口。

这个想法是计算第一个窗口的几何形状并使用该信息来修改第二个窗口的几何形状:

table = MyTable(vehicleFileList, numOfFiles, 2, self)
# add Qt.Window to table's flags
table.setWindowFlags(table.windowFlags() | Qt.Window)
table.resize(self.width(), table.sizeHint().height())
table.window().move(self.geometry().bottomLeft())
table.show()

【讨论】:

以上是关于QTableWidget子窗口如何放在父窗口前面或父窗口下面?的主要内容,如果未能解决你的问题,请参考以下文章

关闭jQUERY中的弹出窗口后如何刷新父页面? [复制]

如何将子视图从其父视图的父视图置于顶部

如何在父窗口中运行子窗口

子窗口关闭,父窗口如何刷新

jsp点击右上角的X关闭子窗口刷新父窗口该如何实现呢?

MFC中怎么让子窗体大小随着父窗口的大小变化而改变?