PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How to send signals/variables between a QDialog and Main Window

 July 07, 2022     class, pyqt, pyqt5     No comments   

Issue

I am currently working on a project that involves graphing text file data into a pyqt graph and I have been running into problems with a subclass QDialog box. My goal is to have the QDialog box use a combox to choose between different data sets to graph (The code below shows the "steering angle" setting being chosen). The problem lies with how to make it so that when the Create Graph button is pressed (Found in the QDialog Class), it runs the createGraph(self): function in the main class. I dont know how to work classes that well so I dont know how to make this work.

If anyone has any pointers on either how to get this working, how to properly structure a PYQT Program or how to make it more efficient, I'm all ears.

Thank you for your time!

Main Window Code:

class MainWidget(QMainWindow):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.activateWindow()
        self.raise_()
        self.setupGraph()

        self.dockcheck = 0
        self.graphcheck = 0

        self.setWindowTitle("Drag and Drop Test")
        self.resize(1200, 800)

        self.setAcceptDrops(True)

        self.LBLDragAndDrop = QLabel("Drag And Drop Files Here")
        self.LBLDragAndDrop.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)

        if self.graphcheck == 0:
            self.setCentralWidget(self.LBLDragAndDrop)

        self.path3 = "C:\\Users\\steph\\OneDrive\\Documents\\SAA Wing\\Coding\\Can Bus Data Reading\\Temporary Saves"

        self.treeview = QTreeView()
        self.treeview.setAnimated(True)

        self.fileModel = QFileSystemModel()
        self.fileModel.setRootPath(self.path3)
        self.indexRoot = self.fileModel.index(self.fileModel.rootPath())

        self.treeview.setModel(self.fileModel)
        self.treeview.setRootIndex(self.fileModel.index(self.path3))
        self.treeview.setColumnWidth(0, 250)

        self.treeview.doubleClicked.connect(self.onSelectionChanged)
        #self.treeview.doubleClicked.connect(self.openDialog)

       

    ####################################################################################################################
    # Dialog Box
    ####################################################################################################################

    def onSelectionChanged(self, index):
        self.selectionPath = self.sender().model().filePath(index)

        self.selectionFilename = (self.selectionPath.split("/")[-1])


        IDList = ("ID 00d0","ID 00d1","ID 00d3","ID 00d4","ID 0140","ID 0141","ID 0360","ID 0361")

        if self.selectionFilename in IDList:

            if self.selectionFilename == "ID 00d0":
                editDialog = Dialog00d0()
                editDialog.exec_()

####################################################################################################################
    # Graphing data
    ####################################################################################################################

    def createGraph(self):
        self.graphcheck = 1

        if self.graphcheck == 1:
            self.setCentralWidget(self.scroll_area)

        ################################################################################################################
        # ID 00D0 Creating Graph
        ################################################################################################################

        if self.selectionFilename == "ID 00d0":
            self.df00d0 = pd.read_table(self.selectionPath, header=None , delim_whitespace=True, dtype=object)
            self.df00d0.columns = ['Timestamp','ID',"B0","B1","B2","B3","B4","B5","B6","B7"]
            self.df00d0.dropna(inplace=True)

            self.SA = np.array([], dtype=float)
            self.LatAcc = np.array([], dtype=float)
            self.LonAcc = np.array([], dtype=float)
            self.ComAcc = np.array([], dtype=float)
            self.Time00d0 = np.array([], dtype=float)
            self.Timestamp00d0 = np.array([], dtype=float)

            ############################################################################################################
            # Getting Time Stamps
            ############################################################################################################

            for item in self.df00d0['Timestamp']:
                self.Time00d0 = np.append(self.Time00d0, datetime.fromtimestamp(float(item)).strftime("%H:%M:%S.%f")[:-4])
                self.Timestamp00d0 = np.append(self.Timestamp00d0, float(item))
 
 ############################################################################################################
            # Steering Angle Graph
            ############################################################################################################

            if self.combobox00d0.currentText() == "Steering Angle":

                SA_ = (((self.df00d0['B1']) + (self.df00d0['B0'])).apply(int, base=16) * 0.1)

                for item in SA_:
                    if item > 6000:
                        self.SA = np.append(self.SA, round((item - 6553.6), 1))
                    else:
                        self.SA = np.append(self.SA, round(item))

                y_value = self.SA

Here is the QDialog Box class code:

class Dialog00d0(QDialog):
    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout()
        hlay = QHBoxLayout()
        self.setLayout(self.layout)

        self.setWindowTitle("Create Graph")

        label = QLabel("Data Type")

        self.combobox00d0 = QComboBox()
        self.combobox00d0.addItem("Steering Angle")
        self.combobox00d0.addItem("Latitudinal Acceleration")
        self.combobox00d0.addItem("Longitudinal Acceleration")
        self.combobox00d0.addItem("Combined Acceleration")

        self.BTNCreateGraph = QPushButton("Create Graph")
        self.BTNCancel = QPushButton("Cancel")

        hlay.addWidget(self.BTNCreateGraph)
        hlay.addWidget(self.BTNCancel)

        self.layout.addWidget(label)
        self.layout.addWidget(self.combobox00d0)
        self.layout.addLayout(hlay)

        self.BTNCreateGraph.clicked.connect("I need the self.creatGraph here")

        self.BTNCancel.clicked.connect("self.close")

Solution

I imagine this will help you. The pyqtSignal() argument tells you what information you want to carry. In this case, I'm passing a text. Good luck, I hope I helped.

import sys
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication
from PyQt5.QtWidgets import QPushButton, QVBoxLayout
from PyQt5 import QtCore, QtGui

class MainWidget(QMainWindow):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        button = QPushButton("Button to open dialog")
        button.clicked.connect(self.button_clicked)
        self.setCentralWidget(button)
        self.show()

    def button_clicked(self):
        dlg = Dialog00d0()
        dlg.signEmit.connect(self.createGraph)
        dlg.exec()

    def createGraph(self, _str):
        print('Now Im here')
        print(_str)

class Dialog00d0(QDialog):
    signEmit = QtCore.pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.BTNCreateGraph = QPushButton("link to createGraph()")
        self.layout.addWidget(self.BTNCreateGraph)
        self.setLayout(self.layout)
        self.BTNCreateGraph.clicked.connect(self.BTNCreateGraph_clicked)

    def BTNCreateGraph_clicked(self):
        self.signEmit.emit('But I passed by')

app = QApplication(sys.argv)
win = MainWidget()
app.exec()


Answered By - Jonas Vieira de Souza
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing