SHARE
TWEET

Untitled

a guest Jun 24th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PyQt5 import QtCore, QtGui, QtWidgets
  2.  
  3.  
  4. class Ui_MainWindow(object):
  5.     def setupUi(self, MainWindow):
  6.         MainWindow.setObjectName("MainWindow")
  7.         MainWindow.resize(800, 600)
  8.         self.centralwidget = QtWidgets.QWidget(MainWindow)
  9.         self.centralwidget.setObjectName("centralwidget")
  10.         self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
  11.         self.tableWidget.setGeometry(QtCore.QRect(60, 100, 551, 331))
  12.         self.tableWidget.setObjectName("tableWidget")
  13.         self.tableWidget.setColumnCount(3)
  14.         self.tableWidget.setRowCount(0)
  15.         item = QtWidgets.QTableWidgetItem()
  16.         self.tableWidget.setHorizontalHeaderItem(0, item)
  17.         item = QtWidgets.QTableWidgetItem()
  18.         self.tableWidget.setHorizontalHeaderItem(1, item)
  19.         item = QtWidgets.QTableWidgetItem()
  20.         self.tableWidget.setHorizontalHeaderItem(2, item)
  21.         self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  22.         self.pushButton.setGeometry(QtCore.QRect(60, 450, 75, 23))
  23.         self.pushButton.setObjectName("pushButton")
  24.         MainWindow.setCentralWidget(self.centralwidget)
  25.         self.menubar = QtWidgets.QMenuBar(MainWindow)
  26.         self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
  27.         self.menubar.setObjectName("menubar")
  28.         MainWindow.setMenuBar(self.menubar)
  29.         self.statusbar = QtWidgets.QStatusBar(MainWindow)
  30.         self.statusbar.setObjectName("statusbar")
  31.         MainWindow.setStatusBar(self.statusbar)
  32.  
  33.         self.retranslateUi(MainWindow)
  34.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  35.  
  36.     def retranslateUi(self, MainWindow):
  37.         _translate = QtCore.QCoreApplication.translate
  38.         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  39.         item = self.tableWidget.horizontalHeaderItem(0)
  40.         item.setText(_translate("MainWindow", "Col 1"))
  41.         item = self.tableWidget.horizontalHeaderItem(1)
  42.         item.setText(_translate("MainWindow", "Col 2"))
  43.         item = self.tableWidget.horizontalHeaderItem(2)
  44.         item.setText(_translate("MainWindow", "Col 3"))
  45.         self.pushButton.setText(_translate("MainWindow", "PushButton"))
  46.  
  47. class mainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
  48.     def __init__(self, parent=None):
  49.         super(mainWindow, self).__init__(parent)
  50.         self.setupUi(self)
  51.         self.pushButton.clicked.connect(self.populate_table)
  52.  
  53.     # Combo box creator templates
  54.     def comboBox_populator(self,values):
  55.         combo = QtWidgets.QComboBox()
  56.         for element in values:
  57.             combo.addItem(element)
  58.         return combo
  59.  
  60.     # Action to do on vehcile change
  61.     def onComboChange(self):
  62.         # selVehNames = self.selVehNames
  63.         self.switchTo = self.comboBoxRows[self.counter][0].currentText()
  64.         if self.switchTo == [] or self.switchTo == '':
  65.             pass
  66.         else:
  67.             if self.switchTo == 'a':
  68.                 col1Data3 = ['x','y','z']
  69.             elif self.switchTo == 'b':
  70.                 col1Data3 = ['4','5','6']
  71.             elif self.switchTo == 'c':
  72.                 col1Data3 = ['21','22','23']
  73.             else:
  74.                 pass
  75.             # Update dict with comboboxes
  76.             # col1Data1 = self.comboBoxRows[self.counter][0]
  77.             # col1Data2 = self.comboBoxRows[self.counter][1]
  78.             # self.AllComboBoxes = [col1Data1, col1Data2, col1Data3]
  79.  
  80.             colDataBox3 = self.comboBox_populator(col1Data3)
  81.             print(col1Data3)
  82.             # self.comboBoxRows.update({self.counter:self.AllComboBoxes})
  83.             self.tableWidget.setCellWidget(self.counter,2,colDataBox3)
  84.  
  85.     # Populate the table
  86.     def populate_table(self):
  87.         self.AllComboBoxes = []
  88.         self.comboBoxRows = {}
  89.  
  90.         # Table row information
  91.         rowCountcur = self.tableWidget.currentRow()
  92.         self.tableWidget.insertRow(rowCountcur+1)
  93.         self.counter = rowCountcur + 1
  94.  
  95.         # General Data
  96.         col1Data1 = ['a','b','c']
  97.         col1Data2 = ['e','f','g']
  98.         col1Data3 = ['x','y','z']
  99.  
  100.         # Create combobox
  101.         colDataBox1 = self.comboBox_populator(col1Data1)
  102.         colDataBox2 = self.comboBox_populator(col1Data2)
  103.         colDataBox3 = self.comboBox_populator(col1Data3)
  104.         self.AllComboBoxes = [colDataBox1, colDataBox2, colDataBox3]
  105.         self.comboBoxRows.update({self.counter:self.AllComboBoxes})
  106.  
  107.         # Populate columns
  108.         self.tableWidget.setCellWidget(self.counter,0,self.comboBoxRows[self.counter][0])
  109.         # Populate vehicle list and cell, col 2
  110.         self.tableWidget.setCellWidget(self.counter,1,self.comboBoxRows[self.counter][1])
  111.         # Populate mission order list and cell, col 3
  112.         self.tableWidget.setCellWidget(self.counter,2,self.comboBoxRows[self.counter][2])
  113.  
  114.         self.comboBoxRows[self.counter][0].currentTextChanged.connect(self.onComboChange)
  115.  
  116. if __name__ == "__main__":
  117.     import sys
  118.     app = QtWidgets.QApplication(sys.argv)
  119.     w = mainWindow()
  120.     w.show()
  121.     sys.exit(app.exec_())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top