Results 1 to 4 of 4

Thread: PyQt5 button click not work in imported class

  1. #1
    Join Date
    Sep 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default PyQt5 button click not work in imported class

    In file index.py this line works fine, but in imported class similar line won't work! I can't understand why. Here is code:

    index.py

    import sys
    from PyQt5 import uic
    from PyQt5.QtWidgets import QMainWindow, QApplication

    class MainWindow(QMainWindow):
    def __init__(self):
    super().__init__()

    # Set up the MainWindow from Designer.
    uic.loadUi("mw.ui", self)

    # Connect up the buttons.
    self.pushButton.clicked.connect(self.BtnClck)

    self.show()

    def BtnClck(self):
    # Set up the ContentWindow from Designer.
    from form1 import form1
    form1(self.mn_general)
    self.mn_general.pushButton_2.clicked.connect(form1 .BtnClck1) #this works fine

    if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

    form1.py

    from PyQt5 import uic

    class form1:
    def __init__(self, obj):
    super().__init__()
    uic.loadUi("form1.ui", obj)

    obj.pushButton.setText('TextChanged on init') #this works fine
    obj.pushButton.clicked.connect(self.BtnClck1) #this NOT works
    obj.pushButton.click() #this make previous line works fine!

    def BtnClck1(self):
    print('SecondWindowPrint')

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: PyQt5 button click not work in imported class

    What doesn't work about it?
    Invalid syntax, error message printed at run time, no message when clicked...?

    Please use [code]...[/code] tags around code.

  3. #3
    Join Date
    Sep 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt5 button click not work in imported class

    Then i click pushButton by mice, it not work, no BtnClck1 method is called and no print-SecondWindowPrint.

    But if i call PushButton click programmatically, it works fine.

    And PushButton works fine if i make connect from index.py

    Here is full code on GitHub github.com/m0x3/test1

  4. #4
    Join Date
    Sep 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt5 button click not work in imported class

    Right answer is put instance of form1 class in a public variable in MainWindow class.
    Need change BtnClck function in Index.py like this:

    Qt Code:
    1. def BtnClck(self):
    2. # Set up the ContentWindow from Designer.
    3. from form1 import form1
    4. self.Form=form1(self.mn_general)
    5. self.mn_general.pushButton_2.clicked.connect(form1.BtnClck1) #this works fine
    To copy to clipboard, switch view to plain text mode 

    Really thanks to Saj Haz from StackOverflow

Similar Threads

  1. How to get the selected row when click the button in it?
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2011, 10:00
  2. "Change widget class on button click" problem
    By utkozanenje in forum Newbie
    Replies: 3
    Last Post: 23rd May 2011, 00:40
  3. How to prohibit click to button
    By somename in forum Qt Programming
    Replies: 8
    Last Post: 27th May 2010, 22:32
  4. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 14:27
  5. QPaintEvent on button click?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 08:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.