N
N
Nikita Kuzovkov2021-07-27 14:20:31
Python
Nikita Kuzovkov, 2021-07-27 14:20:31

How to write user selected QComboBox value in PyQt5?

import sys
import os

from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class Constructor(QWidget):
    def __init__(self):
        super(Constructor, self).__init__()
        self.load_ui()

    def load_ui(self):
        loader = QUiLoader()
        path = os.path.join(os.path.dirname(__file__), "form.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()


if __name__ == "__main__":
    app = QApplication([])
    widget = Constructor()
    widget.setWindowTitle("Constructor")
    widget.show()
    sys.exit(app.exec_())

I have a form.ui file in the same directory as this file (main.py). In the .ui file I have a ComboBox widget named "CBsettings". How to write the values ​​of this widget while the application is running? (the value that the user will select)
You need to somehow refer to the widget class and throughout the work to extract values ​​from there
I searched for a very long time, but I only found a way to find the address (I don’t know how to use it later)
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Kuzovkov, 2021-07-27
@StivenHolland

from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.uic import loadUi


class m(QWidget):
    def __init__(self):
        self.app = QApplication([])
        super().__init__()
        loadUi('form.ui', self)
        self.CBsettings.currentIndexChanged.connect(self.change)
        self.show()
        self.app.exec()

    def change(self, index):
        print(self.CBsettings.itemText(index))

m()

PS solution by comment l0cked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question