Dadep Dadep - 22 days ago 7
Python Question

create main script to use PyQt4 windows with other objects

I am trying to learn how to use python and pyQt.
I have done a window with Qtcreator then I used pyuic4, I have also created a class called

Ruban
I would like to use it with my window interface. In my window I have a button called
nouveauRuban
. I would like to create an object from my class
Ruban
when this button is clicked.

I know my code is wrong, and I don't understand what I'm doing. Any help is very welcome.

# -*- coding: utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mainwindow import Ui_MainWindow
from Ruban import Ruban

class mainTM(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): #, parent=None ??
super (mainTM, self).__init__(self, parent) #(parent) ??
self.createWidgets()
self.nouveauRuban.connect(nouveauRuban, QtCore.SIGNAL(_fromUtf8("clicked()")), self.nvRuban)

def nvRuban(self):
self.ruban=Ruban()
self.ruban.info_ruban()

def createWidgets(self):
self.ui=Ui_MainWindow()
self.ui.setupUi(self)


if __name__== "__main__":
app=QApplication(sys.argv)
myapp=mainTM()
myapp.show()
sys.exit(app.exec_())

Answer

If you're connecting a signal to a slot, you need to define that slot using a decorator:

@QtCore.pyqtSlot()
def nvRuban(self):
    self.ruban=Ruban()
    self.ruban.info_ruban()

Then connect it:

self.nouveauRuban.clicked.connect(nvRuban)