Feb-02-2019, 08:23 PM
Hi
I try to use a QAbstractListModel for creating two color models (background_color, foreground_color) with mutual exclusion of the respectively selected color in my PyQt5 GUI. Now I can't get any further.
My code so far:
- color.py:
I try to use a QAbstractListModel for creating two color models (background_color, foreground_color) with mutual exclusion of the respectively selected color in my PyQt5 GUI. Now I can't get any further.
My code so far:
- color.py:
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.uic import loadUi from PyQt5.QtCore import QAbstractListModel, QModelIndex, Qt class ColorModel(QAbstractListModel): COLORS = ["Red", "Blue", "Yellow", "Green", "Black", "White"] def __init__(self, parent=None): super().__init__(parent) self.other_color_model = None def data(self, index, role=Qt.DisplayRole): return index.row() def rowCount(self, parent=QModelIndex()): return len(self.COLORS) def flags(self, index): flags = super().flags(index) if index.isValid(): flags |= Qt.ItemIsSelectable flags |= Qt.ItemIsDragEnabled else: flags = Qt.ItemIsDropEnabled return flags def set_other_color_model(self, other_model): self.other_color_model = other_model class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) loadUi("color.ui", self) background_colors = ColorModel() foreground_colors = ColorModel() background_colors.set_other_color_model(foreground_colors) foreground_colors.set_other_color_model(background_colors) def main(): app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_()) if __name__ == '__main__': main()- color.ui:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> <widget class="QWidget" name="centralwidget"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> <widget class="QWidget" name="layoutWidget"> <property name="geometry"> <rect> <x>260</x> <y>80</y> <width>287</width> <height>71</height> </rect> </property> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QLabel" name="label_background_color"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> <property name="text"> <string>Background color:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QComboBox" name="comboBox_background_color"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_foreground_color"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> <property name="text"> <string>Foreground color:</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QComboBox" name="comboBox_foreground_color"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> </widget> </item> </layout> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>28</height> </rect> </property> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> </widget> <widget class="QStatusBar" name="statusbar"> <property name="locale"> <locale language="English" country="UnitedKingdom"/> </property> </widget> </widget> <resources/> <connections/> </ui>What do I have to do next?