1

我正在尝试从位于单独类中的小部件中的滑块填充表(存在于主窗口中)。我似乎无法让它工作......这样做的最佳方法是什么?
这是我当前的代码:

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)

我知道“连接”是错误的——我对 PyQt 和 Python 很陌生,所以我不太确定从哪里开始。

4

1 回答 1

3

由于self.imageBrowser是您的 Widget 类,因此它将具有具有 sliderMoved 信号的滑块属性。你只需要多几个点。

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

你组织它的方式是正确的。您的主窗口组成您的自定义小部件并将连接绑定在一起。

虽然因为您有一个数据源,还有一个需要更新的 QTableWidget,但您可能需要将这些步骤包装成一个小方法:

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()
于 2012-07-19T04:14:33.937 回答