Why is my PyQt6 QPushButton not clicking in my QTableWidget and QWidget on my MainWindow?

61 views Asked by At

Need to understand the origin of the issue as to why my pyqt6 QPushButton does not seem to be clicking or functioning even though I have done everything right in my eyes. I have researched and read online, extensively, as to the issue I am having. No place seems to isolate the issue. So I am posting on this site now as a final try before I give up, and change my implementation. self.btn.clicked.connect(self.add_click) This is the line that seems to not execute properly. It seems to never call the self.add_click method in the class I created.

Various renaming of functions names. Moreover, tried playing around with passing the mainWindow reference to the QWidget and QpushButton widget objects upon creation. Thinking it is an issue with relation to the MainWindow and the QtableWidget. The overall program has a mainWindow, QTableWidget is the central widget to the mainWindow. Then the QWidget appears to the right of the QtableWidget were it has like 8 QLineEdit widgets that this "Add Button" will submit text upon a click. But the click never yields an action.

import sys
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QTableWidget,
    QTableWidgetItem, QDockWidget, QFormLayout,
    QLineEdit, QWidget, QPushButton, QSpinBox,
    QMessageBox, QToolBar, QInputDialog
)
from PyQt6.QtCore import Qt,QSize
from PyQt6.QtGui import QIcon, QAction

import students

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.s_x_pane = 200
        self.s_y_pane = 100
        self.s_window_width = 800
        self.s_window_height = 600
        self.setGeometry(self.s_x_pane, self.s_y_pane, self.s_window_width, self.s_window_height)
        self.setWindowTitle("Orton Gillingham")
        self.table = None
        self.students = None
        self.menubars = {}
        self.menu_options = {}
        self.mainwindow_menu()
        self.show()

    def mainwindow_menu(self):
        """

        :return:
        """
        # Create menu bar
        self.menubars["m1"] = self.menuBar()


        # Student Menu functionality
        # All students view
        self.menu_options["mo1"] = self.menubars["m1"].addMenu('Students Menu')
        newStudentsAction = QAction('&View All Students', self)
        newStudentsAction.setShortcut('Ctrl+S')
        newStudentsAction.setStatusTip('Presenting all Students')
        newStudentsAction.triggered.connect(self.studentsClick)
        self.menu_options["mo1"].addAction(newStudentsAction)

        # a single student view
        newStudentAction = QAction('&View a Specific Student', self)
        newStudentAction.setShortcut('Ctrl+P')
        newStudentAction.setStatusTip('Presenting the Desired Student')
        newStudentAction.triggered.connect(self.studentClick)
        self.menu_options["mo1"].addAction(newStudentAction)


        # Student Menu Functionality
        # a single student lessons view
        self.menu_options["mo2"] = self.menubars["m1"].addMenu('Student Lessons')
        newLessonAction = QAction("&View a Student's Lessons", self)
        newLessonAction.setShortcut('Ctrl+A')
        newLessonAction.setStatusTip('Presenting a specific Students Lessons View')
        newLessonAction.triggered.connect(self.aStudentsLessonsClick)
        self.menu_options["mo2"].addAction(newLessonAction)


        # Word List Menu Functionality
        # all words lists
        self.menu_options["mo3"] = self.menubars["m1"].addMenu('Words List')
        newWordsListAction = QAction("&View all words lists available", self)
        newWordsListAction.setShortcut('Ctrl+W')
        newWordsListAction.setStatusTip('Presenting all words lists available')
        newWordsListAction.triggered.connect(self.wordsListsClick)
        self.menu_options["mo3"].addAction(newWordsListAction)

        # viewing a specific words list
        newWordListAction = QAction("&View a specific word list", self)
        newWordListAction.setShortcut('Ctrl+R')
        newWordListAction.setStatusTip('Presenting a words list')
        newWordListAction.triggered.connect(self.wordListsClick)
        self.menu_options["mo3"].addAction(newWordListAction)


        # Unknown additional feature (No Name) Menu Functionality
        self.menu_options["mo4"] = self.menubars["m1"].addMenu('No Name')
        self.menu_options["mo1"].triggered.connect(self.noNameClick)

    def studentsClick(self):
        """

        :return:
        """
        self.students = [
            {'First Name': 'John', 'Last Name': 'Doe', 'Lesson #': 25, 'Current Sound': 'long a'},
            {'First Name': 'Jane', 'Last Name': 'Doe', 'Lesson #': 22, 'Current Sound': 'long a'},
            {'First Name': 'Alice', 'Last Name': 'Doe', 'Lesson #': 22, 'Current Sound': 'long a'},
        ]
        desired_column_widths = [125, 125, 90, 90]
        num_of_col = len(desired_column_widths)
        row_keys = list(self.students[0].keys())
        field_names = ["First Name", "Last Name", "Age", "Lesson #", "Mother's Name", "Father's Name", "Phone #"]
        btn_name = "Add"
        window_name = "New Student"
        t = myTable(self.students, num_of_col, desired_column_widths, row_keys, self)
        w = myQwiget(field_names, btn_name, window_name, self)



class myTable:
    """
    table_contents = collection of data needing to be presented in a tabular format. traditionally a collection of
    students, in the contents of this program. However, could be expanded out in the more abstract sense.

    column_count = number of columns that will be presented in the table.

    column_widths = the desired widths of the columns so that the characters presented are not carried over after a \n

    row_keys = field names of each column in the table. This is an array of strings
    """
    def __init__(self, table_contents, column_count, column_widths, row_keys, main_window):
        self.table_column_count = column_count
        self.column_widths = column_widths
        self.table_keys = row_keys
        self.table_contents = table_contents
        self.main_window = main_window
        self.table = QTableWidget(self.main_window)
        self.main_window.setCentralWidget(self.table)
        self.table.setColumnCount(column_count)
        self.set_table_column_widths()
        self.table.setHorizontalHeaderLabels(self.table_contents[0].keys())
        self.table.setRowCount(len(self.table_contents))
        self.create_row_composition()



    def set_table_column_widths(self):
        """

        Method computes the column widths of each column in the table.
        """
        for i in range(len(self.column_widths)):
            self.table.setColumnWidth(i, self.column_widths[i])


    def create_row_composition(self):
        """

        Method is responsible for computing and installing the necessary data to be present in the desired rows
        and columns of the table.
        """
        row = 0
        for e in self.table_contents:
            for col in range(self.table_column_count):
                self.table.setItem(row, col, QTableWidgetItem(str(e[self.table_keys[col]])))
                col += 1
            row += 1



class myQwiget:

 def __init__(self, name_of_fields, button_name, name_of_right_window, main_window):
        self.main_window = main_window
        self.form = QWidget()
        self.layout = QFormLayout(self.form)
        self.form.setLayout(self.layout)
        self.name_of_fields = name_of_fields
        self.button_name = button_name
        self.name_of_right_window = name_of_right_window

        self.btn = None
        self.qwidget_references = []
        self.setup_widget()
        self.dock = QDockWidget(self.name_of_right_window)
        self.dock.setFeatures(QDockWidget.DockWidgetFeature.NoDockWidgetFeatures)
        self.main_window.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.dock)
        self.toolbar = QToolBar('main toolbar')
        self.toolbar.setIconSize(QSize(16, 16))
        self.main_window.addToolBar(self.toolbar)
        delete_action = QAction(QIcon('./assets/remove.png'), '&Delete', self.main_window)
        self.toolbar.addAction(delete_action)
        self.dock.setWidget(self.form)


    def setup_widget(self):
        """

        :return:
        """

        for i in range(len(self.name_of_fields)):
            self.qwidget_references.append(QLineEdit(self.form))

        for i in range(len(self.name_of_fields)):
            self.layout.addRow(self.name_of_fields[i] + " :", self.qwidget_references[i])

        self.btn = QPushButton(self.button_name, parent=self.main_window)
        self.btn.clicked.connect(self.add_click)
        self.layout.addRow(self.btn)



    def add_click(self):
        """

        :return:
        """
        print("add button clicked")
        #self.get_student_field_data()
0

There are 0 answers