I'm using Python3 and PyQt5, make my widgets and windows in Qt Designer. What is more, I do not generate .py files from a .ui. I simply load it using next code:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
So, I wanted to know, how do I bind menu bar actions to functions.
Is there any way I can do something like this?
self.getActionByName("actionTest_Action").connect(self.do_something)
It is not necessary to use findChild when using loadUi since this method adds the object to the attributes of the class using the objectName as a name, for example in this particular case a cleaner code than the other answer is:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
self.actionTest_Action.triggered.connect(self.test)
def test(self):
print("Test")
So, answering my own question..
One way to do this, is to find an action by using FindChild(QAction, "ActionName") function, and then bind a function using connect() function
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('UI/Qt/source/MainWindow.ui', self)
action = self.findChild(QAction, "actionTest_Action")
action.triggered.connect(self.test)
def test(self):
print("Test")
Related
I want to hide a window immediately after it is created. It works only if I do this with the help of button or something.
class Example(QWidget):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.hide() # doesn't work
self.btn = QPushButton('Hide', self)
self.btn.clicked.connect(self.click) # works
self.btn.show()
def click(self): # works
self.hide()
Apparently it seems that the code should work. What may be happening is that you are calling show() after creating the object. For example:
example = Example()
example.show()
Read this answer about hide() and show(): What's the difference in Qt between setVisible, setShown and show/hide
You can use QtCore.QTimer
class Example(QWidget):
def __init__(self, app):
QWidget.__init__(self)
QTimer.singleShot(0, self.hide)
class SomNetwork(object):
def __init__(self, dataset):
# some parameters that are not important here
pass
def one_step_learn(self, k, sigma_0, gamma_0, alfa, mcolor,population_of_ids):
pass
def learn(self):
pass
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_QSom()
self.ui.setupUi(self)
self.frame = MyFrame(self)
self.setCentralWidget(self.frame)
self.frame.start()
self.show()
class MyFrame(QtGui.QFrame):
simulationSpeed = 5000
def __init__(self, parent):
super(Ramka, self).__init__(parent)
self.init_Board()
def init_Board(self):
self.timer = QtCore.QBasicTimer()
I would like to be able to interact with SomNetwork class in order to be able to call its methods from within inside of the MyFrame class methods. Are there any special design patterns that would suit this example the most or should I just add instance of the class as a field to the MyFrame class.
Thanks of your help in advance!
I don't know if your question is heading towards this, but for your network you could try to subclass a QtCore.QObject instead. On the whole you should instance your network as a member of your QMainWindow, so your frame and the net can interact via signals and slots.
... By the way, there is a little flaw in your code, when you call the constructor of a QWidget in the subclass of a QMainWindow.
I am new in programming and I have created a simple application with one class in Python and PySide which manipulates phone bill csv files. Now I want an option for mobile too.
How can I add a menubar, when my class inherits from QWidget? Should I write another class which inherits from QMainWindow and then make an instance of my first class as a central widget? Is this the right way to do this?
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
....
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
widget = MyWidget()
self.setCentralWidget(widget)
...
There's no need for a QMainWindow, you can simply create a QMenuBar in your widget.
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.menu=QtGui.QMenuBar()
self.menu.addAction("do something")
layout=QtGui.QVBoxLayout()
layout.addWidget(self.menu)
A QMainWindow is basically a widget which already has a layout with a menu bar, a toolbar, a status bar, etc. If you don't need all of those functionality, you can use a simple QWidget and add only what you want.
First of all let me tell you that I am new to Qt and also to Python.
I am using Qt(Taurusdesigner) to create my GUIs.
After starting Qt(Taurusdesigner), I generate my python code for that particular GUI using:
taurusuic4 -x -o file.py file.ui
or
pyuic4 -x -o file.py file.ui
After executing this command on command line I am able to generate python file, but the auto generated classes looks like:
class MainWindow(object):
def setupUi(self, MainWindow):
Where as when I am searching for any help on Google I find class written like:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
What would I do to generate 2nd type of class file using Qt(Taurusdesigner)??
Why there is a syntax difference in my class and class which are written for help on internet.
Please Help regarding this.
Thanks in advance.
The ui module generated by taurusuic4/pyuic4 should be imported into your main application. You do not need to use the -x option, and obviously you should choose a better module name than "file":
taurusuic4 -o mainwindow.py file.ui
Your main application module should look something like this:
from PyQt4.QtGui import QMainWindow
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.handleButton)
def handleButton(self):
print('Hello World!')
This approach means that all the widgets from Qt(Taurus) Designer end up as attributes of the MainWindow class. Another approach is to have the ui elements within a separate namespace:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.handleButton)
setupUI and __init__ are two methods of the class MainWindow. For one class, there can be any number of method and you can put them in whatever order you like.
There is always an __init__ method, it's called a constructor. This method is called when you create an object (for example when you do myWindow=MainWindow()). It's usually put at the beginning because it'll be call first. Specifically for QT, you have to call the parent's constructor with super.
setupUI is the method created by your designer, to take care of layout and such. It should be called in the constructor.
Your code should look like:
class MainWindow(object):
def setupUi(self, MainWindow):
#code made by the designer
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#some code
def another_method(self):
#some more code
I am making an application in Qt using PySide. In my main.py I have a class called Program which sets up the ui and I have another class which has functions pertaining to a certain area. For instance I have an area which has a start and a clear button. I define the functions in class RunArea and connect the signals in class Program but nothing happens. Here is the code.
class Program(QtGui.QMainWindow, Interface.Ui_MainWindow):
def __init__(self, parent=None):
super(Program, self).__init__(parent)
self.setupUi(self)
self.runArea = RunArea()
self.startButton.clicked.connect(self.runArea.start)
self.clearButton.clicked.connect(self.runArea.clear)
class RunArea(QtGui.QMainWindow, Interface.Ui_MainWindow):
def __init__(self, parent=None):
super(RunArea, self).__init__(parent)
self.setupUi(self)
def start(self):
self.log.setPlainText("log entry")
def clear(self):
self.runTree.clear()
What I expect to happen is that "log entry" will be put in a QTextEdit(defined in setupUi) when I click start. And when I click clear a TreeWidget with name runTree will be cleared. I know that the signals are working, but nothing is showing up. May someone please explain why it is not working?
I don't know exactly why/how your code isn't working, although I suspect it has something to do with the inheritance not referring to the same object in the both classes.
However, this is how this should be done:
class Program(QtGui.QMainWindow, Interface.Ui_MainWindow):
def __init__(self, parent=None):
super(Program, self).__init__(parent)
self.setupUi(self)
self.runArea = RunArea(self)
self.startButton.clicked.connect(self.runArea.start)
self.clearButton.clicked.connect(self.runArea.clear)
class RunArea():
def __init__(self, parent=None):
self.parent = parent
def start(self):
self.parent.log.setPlainText("log entry")
def clear(self):
self.parent.runTree.clear()