Search code examples
pyqtsignals-slots

Signal/Slot help-- setting a signal to a slot outside of the current class


I'm trying to populate a table (present in the main window) from a slider that's located in a widget in a separate class. I can't seem to get it to work...what's the best way to go about doing this?
Here's my current code:

class Widget(QWidget):
    def __init__(self,filename,parent=None):
        super(Widget,self).__init__(parent)
        self.resize(900,900)
        self.layout=QVBoxLayout(self)


        frame=Frame(filename)
        self.image=pg.ImageView()
        self.image.setImage(frame.data)
        self.image.setCurrentIndex(0)

        fileheader=FileHeader(filename)
        self.slider=QSlider(self)
        self.slider.setOrientation(Qt.Horizontal)
        self.slider.setMinimum(1)
        self.slider.setMaximum(fileheader.numframes)
        self.slider.sliderMoved.connect(self.sliderMoved)

        self.layout.addWidget(self.image)
        self.layout.addWidget(self.slider)


    def sliderMoved(self,val):
        print "slider moved to:", val
        fileheader=FileHeader(filename)
        idx=val
        frame=fileheader.frameAtIndex(idx)
        self.image.setImage(frame.data)



class MainWindow(QMainWindow):
    def __init__(self, filename, parent=None):
        super(MainWindow,self).__init__(parent)

        self.initUI(filename)

    def initUI(self,filename):
        self.filetable=QTableWidget()

        self.frametable=QTableWidget()

        self.imageBrowser=Widget(filename)
        self.imagesplitter=QSplitter(Qt.Horizontal)
        self.tablesplitter=QSplitter(Qt.Horizontal)
        self.imagesplitter.addWidget(self.imageBrowser)
        self.tablesplitter.addWidget(self.imagesplitter)
        self.tablesplitter.addWidget(self.filetable)
        self.tablesplitter.addWidget(self.frametable)
        self.setCentralWidget(self.tablesplitter)

        exitAction=QAction(QIcon('exit.png'),'&Exit',self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        openAction=QAction(QIcon('open.png'),'&Open',self)
        openAction.setShortcut('Ctrl+O')


        menubar=self.menuBar()
        fileMenu=menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        fileMenu.addAction(openAction)

        self.fileheader=FileHeader(filename)
        self.connect(self.frametable,  
                     SIGNAL("Widget.sliderMoved(idx)"),
                     self.fileheader.frameAtIndex(idx))
        self.frameheader=self.fileheader.frameAtIndex(0)
        self.populate()


    def populate(self):
        self.filetable.setRowCount(len(self.fileheader.fileheader_fields))
        self.filetable.setColumnCount(2)
        self.filetable.setHorizontalHeaderLabels(['File Header','value'])
        for i,field in enumerate(self.fileheader.fileheader_fields):
            name=QTableWidgetItem(field)
            value=QTableWidgetItem(unicode(getattr(self.fileheader,field)))
            self.filetable.setItem(i,0,name)
            self.filetable.setItem(i,1,value)

        self.frametable.setRowCount(len(self.frameheader.frameheader_fields))
        self.frametable.setColumnCount(2)
        self.frametable.setHorizontalHeaderLabels(['Frame Header','Value'])
        for i,fields in enumerate(self.frameheader.frameheader_fields):
            Name=QTableWidgetItem(fields)
            Value=QTableWidgetItem(unicode(getattr(self.frameheader,fields)))
            self.frametable.setItem(i,0,Name)
            self.frametable.setItem(i,1,Value)

I know the "connect" is wrong-- I'm very new to PyQt and Python in general, so I'm not quite sure where to start.


Solution

  • Since self.imageBrowser is your Widget class, it will have the slider attribute which has the sliderMoved signal. You just need a few more dots.

    self.imageBrowser.slider.sliderMoved.connect(self.fileheader.frameAtIndex)
    

    The way you have it organized is correct though. Your main window composes your custom widgets and binds the connections together.

    Though because you have a data source, and also a QTableWidget that will need to be updated, you probably need to wrap the steps up into a little method:

    def initUI(self,filename):
        ...
        self.imageBrowser.slider.sliderMoved.connect(self._handle_slider_moved)
        # initialize it the first time during the window set up
        self._handle_slider_moved(0)
    
    def _handle_slider_moved(self, val):
        # update the data source
        self.fileheader.frameAtIndex(val)
        # update the second data source 
        self.frameheader=self.fileheader.frameAtIndex(0)
        # now refresh the tables
        self.populate()