Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PyQt4 - Multiple Comboboxes In and Out of QTableWidget



  • The code below takes a segment of analytics data and produces a table of data when a country is pressed in the Popular Countries menu specific to that country. In this example the only countries available are the UK and the NL for the sake of brevity. The data currently only concerns 'Source Language' information but I would like to add a comboBox outside of the table which allows it to change to 'Target Language' and 'Topic' information (see second block of countryAction function in script). I would like to add another external menu that allows me to change all of the internal comboBox menus and I would of course also like the internal comboBox menus to do something...

    The table currently displays the correct data for the 'All Platforms' choice but does not change when a new platform is pressed. It should show the specific data for each platform (see same part of script) when a platform is selected. The external menu mentioned secondly should of course have the same options as the internal platform menu. The external menu options should be 'SourceLangPerCountryAndPlatform', 'TargetLangPerCountryAndPlatform' and 'TopicPerCountryAndPlatform' as seen in the script, and should represent the respective data in the same way for all three. A (badly done) example of what I am after can be seen at the bottom of this query.

    from functools import partial
    from collections import Counter, OrderedDict
    from PyQt4 import QtCore, QtGui
    
    try:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s
    
    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)
    
    class Ui_MainWindow(QtGui.QMainWindow):
    
        def setupUi(self, MainWindow):
            MainWindow.setGeometry(50, 50, 500, 300)
    
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 867, 22))
            self.menubar.setObjectName(_fromUtf8("menubar"))
            self.menuCountry = QtGui.QMenu(self.menubar)
            self.menuCountry.setObjectName(_fromUtf8("menuCountry"))
            self.menuPopular_Countries = QtGui.QMenu(self.menuCountry)
            self.menuPopular_Countries.setObjectName(_fromUtf8("menuPopular_Countries"))
            self.menuPlatform = QtGui.QMenu(self.menubar)
            self.menuPlatform.setObjectName(_fromUtf8("menuPlatform"))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName(_fromUtf8("statusbar"))
            MainWindow.setStatusBar(self.statusbar)
    
            self.actionAll_Countries = QtGui.QAction(MainWindow)
            self.actionAll_Countries.setObjectName(_fromUtf8("actionAll_Countries"))
            self.menuCountry.addAction(self.actionAll_Countries)
            self.menuCountry.addSeparator()
            self.menuCountry.addAction(self.menuPopular_Countries.menuAction())
    
            continents = {}
            countries = {}
            popularcountries = ['United States', 'United Kingdom', 'South Africa', 'Germany', 'India', 'Australia', 'Canada', 'Italy', 'Sweden' ,\
    'Netherlands', 'France', 'New Zealand', 'Belgium', 'Switzerland', 'Norway', 'Brazil', 'Indonesia', 'Russia', \
    'United Arab Emirates', 'Spain', 'Denmark']
            DIR = '/Users/jonathan/Documents/CountryWiseAnalytics/'
            UsersCountry = {('Europe', 'NL', 'Netherlands'): 231, ('Europe', 'GB', 'United Kingdom'): 2636}
            for key, value in UsersCountry.iteritems():
                continent = key[0]
                continentMenu = continents.get(continent)
                if continentMenu is None:
                    continentMenu = self.menuCountry.addMenu(continent)
                    continents[continent] = continentMenu
                country = key[2]
                CT = key[1]
                countryAction = QtGui.QAction(QtGui.QIcon(CT.lower() + '.png'), country, MainWindow)
                countryAction.setStatusTip('uAnalyse ' + country)
                countryAction.triggered.connect(partial(self.countryAction, country, CT))
                if country in popularcountries:
                    print(CT.lower() + '.png')
                self.menuPopular_Countries.addAction(countryAction)
                else:
                    continentMenu.addAction(countryAction)
    
            self.menubar.addAction(self.menuCountry.menuAction())
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def countryAction(self, country, CT):
    
            window = QtGui.QMainWindow(self)
            window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            window.setWindowTitle(self.tr(country))
            window.setWindowIcon(QtGui.QIcon(CT.lower() + '.png'))
    
            SourceLangPerCountryAndPlatform = {('Europe', 'NL', 'Netherlands', 'Android', '2', 'British English'): 1, \
            ('Europe', 'NL', 'Netherlands', 'WebGLPlayer', '2', 'British English'): 156, \
            ('Europe', 'NL', 'Netherlands', 'OSXPlayer', '2', 'British English'): 27, \
            ('Europe', 'GB', 'United Kingdom', 'Android', '20', 'Japanese'): 14, \
            ('Europe', 'GB', 'United Kingdom', 'WindowsPlayer', '92', 'Modern Standard Arabic'): 4, \
            ('Europe', 'NL', 'Netherlands', 'WindowsPlayer', '11', 'Norwegian'): 1, \
            ('Europe', 'GB', 'United Kingdom', 'IPhonePlayer', '20', 'Japanese'): 1, \
            ('Europe', 'NL', 'Netherlands', 'OSXPlayer', '94', 'Scottish Gaelic'): 1, \
            ('Europe', 'GB', 'United Kingdom', 'WebGLPlayer', '36', 'Zulu'): 16}
            TargetLangPerCountryAndPlatform = {('Europe', 'NL', 'Netherlands', 'WindowsPlayer', '11', 'Norwegian'): 1, \
            ('Europe', 'GB', 'United Kingdom', 'IPhonePlayer', '20', 'Japanese'): 1}
            TopicPerCountryAndPlatform = {('Europe', 'DE', 'Germany', 'OSXPlayer', '94', 'driving'): 1, \
            ('North America', 'US', 'United States', 'IPhonePlayer', '36', 'shoppingwords'): 16}
    
            sourcelangcountries = []
            targetlangcountries = []
            topiccountries = []
            platforms = ['All Platforms']
            android = OrderedDict()
            iOS = OrderedDict()
            windows = OrderedDict()
            OSX = OrderedDict()
            webGL = OrderedDict()
            for key, value in sorted(SourceLangPerCountryAndPlatform.iteritems()):
                country_in_data = key[2]
                platform = key[3]
                sourcelang = key[5]
                count = value
                if platform not in platforms:
                    platforms.append(platform)
                if country_in_data == country:
                    if platform == 'Android':
                        android[sourcelang] = count
                    elif platform == 'IPhonePlayer':
                        iOS[sourcelang] = count
                    elif platform == 'WindowsPlayer':
                        windows[sourcelang] = count
                    elif platform == 'OSXPlayer':
                        OSX[sourcelang] = count
                    else:
                        webGL[sourcelang] = count
    
            A = Counter(android)
            B = Counter(iOS)
            C = Counter(windows)
            D = Counter(OSX)
            E = Counter(webGL)
            allplatforms = A + B + C + D + E
    
            window.table = QtGui.QTableWidget()
            window.table.setColumnCount(3)
            window.setCentralWidget(window.table)
            data1 = []
            data2 = []
            for key, value in allplatforms.iteritems():
                data1.append(key)
                data2.append(str(value))
            print data1
            print data2
            combo_box_options = platforms
    
            window.table.setRowCount(len(allplatforms))
    
            for index in range(len(allplatforms)):
                item1 = QtGui.QTableWidgetItem(data1[index])
                window.table.setItem(index,0,item1)
                item2 = QtGui.QTableWidgetItem(data2[index])
                window.table.setItem(index,1,item2)
                combo = QtGui.QComboBox()
                for t in combo_box_options:
                    combo.addItem(t)
                window.table.setCellWidget(index,2,combo)
    
            window.show()
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
            self.menuCountry.setTitle(_translate("MainWindow", "Country", None))
            self.menuPopular_Countries.setTitle(_translate("MainWindow", "Popular Countries", None))
            self.actionAll_Countries.setText(_translate("MainWindow", "All Countries", None))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

    I apologise for the long code but the nature of the question necessitates that I provide a working GUI. If you need me to explain anything before you can answer please leave a comment. Even just helping me insert functionless external comboboxes would be appreciated.

    enter image description here


Log in to reply