Skip to content

Instantly share code, notes, and snippets.

@Anas-jaf
Created October 20, 2023 18:45
Show Gist options
  • Save Anas-jaf/d66e6f57bbb56194f9114f7bd5aa0b3c to your computer and use it in GitHub Desktop.
Save Anas-jaf/d66e6f57bbb56194f9114f7bd5aa0b3c to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QTableView, QHeaderView, QVBoxLayout, QLabel, QPushButton, QStackedWidget
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(1600, 1200) # Increase the size of the second widget
mainLayout = QVBoxLayout()
companies = [('Apple', 'Technology'), ('Facebook', 'Social Media'), ('Google', 'Technology'), ('Amazon', 'E-commerce'),
('Walmart', 'Retail'), ('Dropbox', 'Cloud Storage'), ('Starbucks', 'Coffee'), ('eBay', 'E-commerce'), ('Canon', 'Electronics')]
model = QStandardItemModel(len(companies), 2)
model.setHorizontalHeaderLabels(['Company', 'Category']) # Two columns: Company and Category
for row, (company, category) in enumerate(companies):
item_company = QStandardItem(company)
item_category = QStandardItem(category)
model.setItem(row, 0, item_company)
model.setItem(row, 1, item_category)
filter_proxy_model = QSortFilterProxyModel()
filter_proxy_model.setSourceModel(model)
filter_proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
filter_proxy_model.setFilterKeyColumn(0)
search_field = QLineEdit()
search_field.setStyleSheet('font-size: 35px; height: 60px;')
search_field.textChanged.connect(filter_proxy_model.setFilterRegExp)
mainLayout.addWidget(search_field)
table = QTableView()
table.setStyleSheet('font-size: 35px;')
table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
table.setModel(filter_proxy_model)
mainLayout.addWidget(table)
self.setLayout(mainLayout)
class AuthenticationWidget(QWidget):
def __init__(self, stacked_widget):
super().__init__()
self.setWindowTitle("Authentication Widget")
self.setGeometry(100, 100, 400, 200)
self.stacked_widget = stacked_widget
layout = QVBoxLayout()
label_username = QLabel("Username:")
self.edit_username = QLineEdit()
label_password = QLabel("Password:")
self.edit_password = QLineEdit()
self.edit_password.setEchoMode(QLineEdit.Password) # To hide the password characters
login_button = QPushButton("Login")
login_button.clicked.connect(self.login)
layout.addWidget(label_username)
layout.addWidget(self.edit_username)
layout.addWidget(label_password)
layout.addWidget(self.edit_password)
layout.addWidget(login_button)
self.setLayout(layout)
def login(self):
# Perform authentication here (e.g., check username and password)
# For simplicity, let's assume login is successful
self.stacked_widget.setCurrentIndex(1) # Switch to the AppDemo widget
if __name__ == '__main__':
app = QApplication(sys.argv)
stacked_widget = QStackedWidget()
auth_widget = AuthenticationWidget(stacked_widget)
demo_widget = AppDemo()
stacked_widget.addWidget(auth_widget)
stacked_widget.addWidget(demo_widget)
stacked_widget.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment