0

I'm working on a sign in form for a software where the students needs to select their school from the QComboBox (the combobox will show the school's name) and the software will connect to their school's platform. I'm trying to add data (precisely the platform's link) to each item but after reading several posts on stackoverflow, nothing works for me so far.

Here's the QComboBox's part of my sign in form :

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(717, 571)
        Form.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        Form.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(70, 50, 590, 420))
        self.widget.setStyleSheet("QPushButton#pushButton{\n"
"    background-color: rgb(23, 171, 103);\n"
"    color:rgb(255, 255, 255);\n"
"    border-radius:5px;\n"
"}\n"
"")

        self.comboBox = QtWidgets.QComboBox(self.widget)
        self.comboBox.setGeometry(QtCore.QRect(60, 110, 201, 22))
        font = QtGui.QFont()
        font.setFamily("Proxima Nova Rg")
        self.comboBox.setFont(font)
        self.comboBox.setStyleSheet("")
        self.comboBox.setEditable(False)
        self.comboBox.setCurrentText("Sélectionnez votre établissement")
        self.comboBox.setFrame(False)
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.setItemText(0, "Sélectionnez votre établissement")
        self.comboBox.addItem("")
        self.comboBox.setItemText(1, "LYCEE FRANCAIS RENE DESCARTES")
        self.comboBox.addItem("")
        self.comboBox.setItemText(2, "IFS SINGAPORE ")
        self.comboBox.addItem("")
        self.comboBox.setItemText(3, "IFS SINGAPORE - 2")
        self.comboBox.addItem("")
        self.comboBox.setItemText(4, "LYCEE FRANCAIS INTERNATIONAL DE BANGKOK")
        self.comboBox.addItem("")
        self.comboBox.setItemText(5, "LYCEE FRANCAIS MARGUERITE DURAS")
       
if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        Form = QtWidgets.QWidget()
        ui = Ui_Form()
        ui.setupUi(Form)
        Form.show()
        sys.exit(app.exec())

I've assigned the platform's link as data for each item using this self.comboBox.setItemData

self.comboBox.setItemData(0, "https://platform....")
self.comboBox.setItemData(1, "https://platform....")
self.comboBox.setItemData(2, "https://platform....")
self.comboBox.setItemData(3, "https://platform....")
self.comboBox.setItemData(4, "https://platform....")
self.comboBox.setItemData(5, "https://platform....")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Galileo
  • 1
  • 1

1 Answers1

0

I am not sure what your plan is. But if you want to have the dropbox displayed, you need to put it inside a widget. I have rewritten your code, but still some things don't make much sense. You could just subclass QComboBox and setup your Box and put it inside a Widget.

You need to learn the concept of having QWidgets attached to other QWidgets by adding them to a layout, otherwise they won't be shown, like this.

class SomeWidget(QWidget):
    
    def __init__(self):
        QWidget.__init__(self)  # call the constructor from the inheriting class
        some_other_widget = QComboBox()
        any_layout        = QVBoxLayout()  # others are QHBoxLayout, QGridLayout etc.
        any_layout.addWidget(some_other_widget)  # add your widgets to the layout
        self.setLayout(any_layout)  # add your layout to the Widget your creating

For your code I tried to do the least changes as possible and just to make it running. Quite a few lines won't make much sense in your program

You know can add this Widget inside another Widget to have a window frame, or just remove the self.setWindowFlags line from your program

from PyQt5.QtWidgets import QWidget, QComboBox, QApplication, QVBoxLayout
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QFont
import sys


class Ui_Form(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.setObjectName("Form")
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setGeometry(QRect(70, 50, 590, 420))
        self.setStyleSheet("QPushButton#pushButton{\n"
                                  "    background-color: rgb(23, 171, 103);\n"
                                  "    color:rgb(255, 255, 255);\n"
                                  "    border-radius:5px;\n"
                                  "}\n"
                                  "")

        self.comboBox = QComboBox()
        # self.comboBox.setGeometry(QRect(60, 110, 201, 22))
        font = QFont()
        font.setFamily("Proxima Nova Rg")
        self.comboBox.setFont(font)
        self.comboBox.setStyleSheet("")
        self.comboBox.setEditable(False)
        self.comboBox.setCurrentText("Sélectionnez votre établissement")
        self.comboBox.setFrame(False)
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.setItemText(0, "Sélectionnez votre établissement")
        self.comboBox.addItem("")
        self.comboBox.setItemText(1, "LYCEE FRANCAIS RENE DESCARTES")
        self.comboBox.addItem("")
        self.comboBox.setItemText(2, "IFS SINGAPORE ")
        self.comboBox.addItem("")
        self.comboBox.setItemText(3, "IFS SINGAPORE - 2")
        self.comboBox.addItem("")
        self.comboBox.setItemText(4, "LYCEE FRANCAIS INTERNATIONAL DE BANGKOK")
        self.comboBox.addItem("")
        self.comboBox.setItemText(5, "LYCEE FRANCAIS MARGUERITE DURAS")
        self.setLayout(QVBoxLayout())
        self.layout().addWidget(self.comboBox)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    Form = Ui_Form()
    Form.show()
    Form.resize(717, 571)
    sys.exit(app.exec())
  • 1
    If you have further questions it is better to ask them here, so other people can learn from it too. – Thomas Christopher Davies Aug 26 '21 at 07:52
  • UPDATE : Hi, I managed to find the command which I can use to assign the platform's link as data for each item of the combobox self.comboBox.setItemData I managed to print itemData out but the terminal shows me this instead of the link : Is there a way of showing the link in the terminal ? I know that we can do with the itemText using self.comboBox.currentText but I can't find the command for itemData. – Galileo Aug 26 '21 at 08:39
  • What are you trying to do? Do you want to add further items to the combobox? – Thomas Christopher Davies Aug 26 '21 at 09:08
  • 1
    @Galileo please [edit] your question and try to clarify what you're trying to do, because right now it's a bit difficult to understand what is your problem. If writing in English is difficult for you, find somebody that can help you. Also take your time to follow the [tour] and read [ask] good questions, and remember that StackOverflow is not a forum or a chat, and direct contact between users is generally discouraged, since everything related to a post should be available to anyone: your question should be able to help others too, not only you. – musicamante Aug 26 '21 at 12:40
  • @Galileo use `self.comboBox.currentData()` or `self.comboBox.itemData(self.comboBox.currentIndex())` – eyllanesc Aug 26 '21 at 17:57