analitics

Pages

Wednesday, November 25, 2026

PySide6 : intro sample on pydroid3

Today, I started a new series of examples and tutorials with PySide6. The source code for today is tested on pydroid3 android I.D.E.
The result is this:
See the source code:
#Pydroid run qt
import sys
from PySide6.QtCore import Qt, QTimer
from PySide6.QtWidgets import (
    QApplication,
    QCheckBox,
    QComboBox,
    QGroupBox,
    QHBoxLayout,
    QLabel,
    QLineEdit,
    QListWidget,
    QMainWindow,
    QMessageBox,
    QProgressBar,
    QPushButton,
    QRadioButton,
    QSlider,
    QSpinBox,
    QStatusBar,
    QTableWidget,
    QTableWidgetItem,
    QTabWidget,
    QTextEdit,
    QVBoxLayout,
    QWidget,
)

HIGH_CONTRAST_QSS = """
QMainWindow, QWidget {
    background-color: #F4F6F9;
    color: #111827;
    font-size: 14px;
}

QTabWidget::pane {
    border: 1px solid #94A3B8;
    border-radius: 6px;
    background-color: #F4F6F9;
    top: -1px;
}

QTabBar::tab {
    background-color: #E2E8F0;
    color: #1E293B;
    border: 1px solid #94A3B8;
    border-bottom: none;
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    padding: 8px 14px;
    margin-right: 4px;
    font-weight: bold;
}

QTabBar::tab:selected {
    background-color: #FFFFFF;
    color: #1D4ED8;
    border-color: #1D4ED8;
}

QGroupBox {
    background-color: #FFFFFF;
    border: 1px solid #94A3B8;
    border-radius: 6px;
    margin-top: 12px;
    padding: 12px 10px 10px 10px;
    font-weight: bold;
}

QGroupBox::title {
    subcontrol-origin: margin;
    subcontrol-position: top left;
    left: 10px;
    padding: 0 6px;
    color: #1D4ED8;
    background-color: #F4F6F9;
}

QLabel {
    color: #111827;
    font-weight: normal;
}

QLineEdit, QComboBox, QSpinBox, QListWidget, QTableWidget, QTextEdit {
    background-color: #FFFFFF;
    color: #111827;
    border: 1.5px solid #64748B;
    border-radius: 5px;
    padding: 6px;
    selection-background-color: #1D4ED8;
    selection-color: #FFFFFF;
}

QLineEdit:focus, QComboBox:focus, QSpinBox:focus, QListWidget:focus, QTableWidget:focus, QTextEdit:focus {
    border: 2px solid #1D4ED8;
}

QLineEdit:read-only {
    background-color: #E2E8F0;
    color: #475569;
}

QComboBox QAbstractItemView {
    background-color: #FFFFFF;
    color: #111827;
    selection-background-color: #1D4ED8;
    selection-color: #FFFFFF;
}

QPushButton {
    background-color: #1D4ED8;
    color: #FFFFFF;
    border: none;
    border-radius: 5px;
    padding: 8px 12px;
    font-weight: bold;
}

QPushButton:hover {
    background-color: #1E40AF;
}

QPushButton:pressed {
    background-color: #172554;
}

QCheckBox, QRadioButton {
    color: #111827;
    spacing: 6px;
}

QCheckBox::indicator, QRadioButton::indicator {
    width: 18px;
    height: 18px;
    border: 1.5px solid #334155;
    background-color: #FFFFFF;
}

QCheckBox::indicator:checked, QRadioButton::indicator:checked {
    background-color: #1D4ED8;
    border-color: #1D4ED8;
}

QProgressBar {
    background-color: #E2E8F0;
    color: #111827;
    border: 1.5px solid #64748B;
    border-radius: 5px;
    text-align: center;
    font-weight: bold;
}

QProgressBar::chunk {
    background-color: #15803D;
    border-radius: 3px;
}

QHeaderView::section {
    background-color: #DBEAFE;
    color: #1E3A8A;
    padding: 6px;
    border: 1px solid #94A3B8;
    font-weight: bold;
}

QStatusBar {
    background-color: #E2E8F0;
    color: #111827;
    border-top: 1px solid #94A3B8;
    font-weight: bold;
}
"""


class ComplexGuiWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PySide6 - Contrast Ridicat")
        self.resize(520, 680)

        self.tabs = QTabWidget()
        self.setCentralWidget(self.tabs)

        self.status_bar = QStatusBar()
        self.setStatusBar(self.status_bar)
        self.status_bar.showMessage("Aplicatie pornita - Tema cu contrast ridicat.")

        self.tabs.addTab(self._build_controls_tab(), "Controale")
        self.tabs.addTab(self._build_data_tab(), "Tabel & Lista")
        self.tabs.addTab(self._build_editor_tab(), "Editor & Log")

        self.timer = QTimer(self)
        self.timer.timeout.connect(self._on_timer_tick)

    def _build_controls_tab(self):
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(10)

        input_group = QGroupBox("Date de intrare")
        input_layout = QVBoxLayout(input_group)

        self.text_input = QLineEdit()
        self.text_input.setPlaceholderText("Introdu un titlu sau o valoare...")
        self.text_input.setMinimumHeight(40)

        row_select = QHBoxLayout()
        self.category_combo = QComboBox()
        self.category_combo.addItems(["Optiune A", "Optiune B", "Optiune C", "Optiune D"])
        self.category_combo.setMinimumHeight(40)
        self.category_combo.currentTextChanged.connect(
            lambda txt: self.status_bar.showMessage(f"Selectat: {txt}")
        )

        self.amount_spin = QSpinBox()
        self.amount_spin.setRange(1, 1000)
        self.amount_spin.setValue(10)
        self.amount_spin.setPrefix("Cant: ")
        self.amount_spin.setMinimumHeight(40)

        row_select.addWidget(self.category_combo, 2)
        row_select.addWidget(self.amount_spin, 1)

        input_layout.addWidget(self.text_input)
        input_layout.addLayout(row_select)
        layout.addWidget(input_group)

        options_group = QGroupBox("Selectii si Moduri")
        options_layout = QVBoxLayout(options_group)

        chk_row = QHBoxLayout()
        self.chk_active = QCheckBox("Activ")
        self.chk_active.setChecked(True)
        self.chk_notify = QCheckBox("Notificari")
        self.chk_readonly = QCheckBox("Doar citire")
        self.chk_readonly.toggled.connect(self.text_input.setReadOnly)

        chk_row.addWidget(self.chk_active)
        chk_row.addWidget(self.chk_notify)
        chk_row.addWidget(self.chk_readonly)

        radio_row = QHBoxLayout()
        self.radio_fast = QRadioButton("Rapid")
        self.radio_normal = QRadioButton("Normal")
        self.radio_eco = QRadioButton("Eco")
        self.radio_normal.setChecked(True)

        radio_row.addWidget(self.radio_fast)
        radio_row.addWidget(self.radio_normal)
        radio_row.addWidget(self.radio_eco)

        options_layout.addLayout(chk_row)
        options_layout.addLayout(radio_row)
        layout.addWidget(options_group)

        progress_group = QGroupBox("Progres si Nivel")
        progress_layout = QVBoxLayout(progress_group)

        slider_row = QHBoxLayout()
        self.slider_label = QLabel("Nivel: 40%")
        self.slider_label.setStyleSheet("font-weight: bold;")
        self.level_slider = QSlider(Qt.Horizontal)
        self.level_slider.setRange(0, 100)
        self.level_slider.setValue(40)
        self.level_slider.valueChanged.connect(self._on_slider_change)

        slider_row.addWidget(self.slider_label)
        slider_row.addWidget(self.level_slider)

        self.progress_bar = QProgressBar()
        self.progress_bar.setRange(0, 100)
        self.progress_bar.setValue(40)
        self.progress_bar.setMinimumHeight(28)

        timer_btn_row = QHBoxLayout()
        self.btn_start_timer = QPushButton("Porneste Auto-Progres")
        self.btn_start_timer.setMinimumHeight(40)
        self.btn_start_timer.clicked.connect(self._toggle_timer)

        self.btn_add_record = QPushButton("Adauga in Tabel & Lista")
        self.btn_add_record.setMinimumHeight(40)
        self.btn_add_record.clicked.connect(self._add_record)

        timer_btn_row.addWidget(self.btn_start_timer)
        timer_btn_row.addWidget(self.btn_add_record)

        progress_layout.addLayout(slider_row)
        progress_layout.addWidget(self.progress_bar)
        progress_layout.addLayout(timer_btn_row)
        layout.addWidget(progress_group)

        layout.addStretch()
        return tab

    def _build_data_tab(self):
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(8)

        lbl_list = QLabel("Lista elemente:")
        lbl_list.setStyleSheet("font-weight: bold;")
        layout.addWidget(lbl_list)

        self.item_list = QListWidget()
        self.item_list.addItems(["Element initial 1", "Element initial 2"])
        self.item_list.itemClicked.connect(
            lambda item: self.status_bar.showMessage(f"Element lista: {item.text()}")
        )
        layout.addWidget(self.item_list, 1)

        lbl_table = QLabel("Tabel date:")
        lbl_table.setStyleSheet("font-weight: bold;")
        layout.addWidget(lbl_table)

        self.data_table = QTableWidget(0, 4)
        self.data_table.setHorizontalHeaderLabels(["Nume", "Categorie", "Cant.", "Mod"])
        self.data_table.horizontalHeader().setStretchLastSection(True)
        layout.addWidget(self.data_table, 2)

        btn_row = QHBoxLayout()
        btn_del = QPushButton("Sterge rand selectat")
        btn_del.setMinimumHeight(40)
        btn_del.clicked.connect(self._delete_selected_row)

        btn_clear_all = QPushButton("Goleste tot")
        btn_clear_all.setMinimumHeight(40)
        btn_clear_all.clicked.connect(self._clear_data)

        btn_row.addWidget(btn_del)
        btn_row.addWidget(btn_clear_all)
        layout.addLayout(btn_row)

        return tab

    def _build_editor_tab(self):
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(8)

        lbl_log = QLabel("Zona text / Jurnal evenimente:")
        lbl_log.setStyleSheet("font-weight: bold;")
        layout.addWidget(lbl_log)

        self.log_editor = QTextEdit()
        self.log_editor.setPlaceholderText("Aici apar actiunile sau poti scrie text liber...")
        layout.addWidget(self.log_editor)

        btn_row = QHBoxLayout()
        btn_dialog = QPushButton("Afiseaza Dialog Info")
        btn_dialog.setMinimumHeight(40)
        btn_dialog.clicked.connect(self._show_info_dialog)

        btn_clear_log = QPushButton("Curata Jurnal")
        btn_clear_log.setMinimumHeight(40)
        btn_clear_log.clicked.connect(self.log_editor.clear)

        btn_row.addWidget(btn_dialog)
        btn_row.addWidget(btn_clear_log)
        layout.addLayout(btn_row)

        return tab

    def _on_slider_change(self, value):
        self.slider_label.setText(f"Nivel: {value}%")
        self.progress_bar.setValue(value)

    def _toggle_timer(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn_start_timer.setText("Porneste Auto-Progres")
            self.status_bar.showMessage("Timer oprit.")
        else:
            self.timer.start(150)
            self.btn_start_timer.setText("Opreste Auto-Progres")
            self.status_bar.showMessage("Timer activ.")

    def _on_timer_tick(self):
        val = self.level_slider.value() + 2
        if val > 100:
            val = 0
        self.level_slider.setValue(val)

    def _get_selected_mode(self):
        if self.radio_fast.isChecked():
            return "Rapid"
        if self.radio_eco.isChecked():
            return "Eco"
        return "Normal"

    def _add_record(self):
        name = self.text_input.text().strip() or f"Articol #{self.data_table.rowCount() + 1}"
        category = self.category_combo.currentText()
        qty = str(self.amount_spin.value())
        mode = self._get_selected_mode()

        row_idx = self.data_table.rowCount()
        self.data_table.insertRow(row_idx)
        self.data_table.setItem(row_idx, 0, QTableWidgetItem(name))
        self.data_table.setItem(row_idx, 1, QTableWidgetItem(category))
        self.data_table.setItem(row_idx, 2, QTableWidgetItem(qty))
        self.data_table.setItem(row_idx, 3, QTableWidgetItem(mode))

        summary = f"{name} | {category} | Cant: {qty} | Mod: {mode}"
        self.item_list.addItem(summary)
        self.log_editor.append(f"[ADAUGAT] {summary}")
        self.status_bar.showMessage(f"Adaugat: {name}")
        self.text_input.clear()

    def _delete_selected_row(self):
        row = self.data_table.currentRow()
        if row >= 0:
            self.data_table.removeRow(row)
            self.log_editor.append(f"[STERS] Randul {row + 1} din tabel.")
            self.status_bar.showMessage("Rand sters.")
        else:
            self.status_bar.showMessage("Selecteaza un rand din tabel mai intai.")

    def _clear_data(self):
        self.data_table.setRowCount(0)
        self.item_list.clear()
        self.log_editor.append("[RESET] Lista si tabelul au fost golite.")
        self.status_bar.showMessage("Date golite.")

    def _show_info_dialog(self):
        QMessageBox.information(
            self,
            "Sumar Stare",
            f"Randuri in tabel: {self.data_table.rowCount()}\n"
            f"Nivel curent: {self.level_slider.value()}%\n"
            f"Activ: {'Da' if self.chk_active.isChecked() else 'Nu'}",
        )


def main():
    app = QApplication(sys.argv)
    app.setStyleSheet(HIGH_CONTRAST_QSS)
    window = ComplexGuiWindow()
    window.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()