I am trying to connect the click() signal of a button to my own function. The button is in a widget that I created with QT Designer. I load the .ui file with QUiLoader like so:
class MyWidget(QtGui.QMainWindow):
def __init__(self, *args):
QtGui.QMainWindow.__init__(self, *args)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
file.open(QtCore.QFile.ReadOnly)
self.myWidget = loader.load(file, self)
file.close()
self.setCentralWidget(self.myWidget)
btn = self.myWidget.findChild(QtGui.QPushButton, "HelloWorldButton")
btn.clicked.connect(self.slot1)
def slot1(self):
print "Received"
Is this the correct way to connect to button clicked() signal? I see that I can wire up signals and slots directly in Qt Designer, but how do I prepare and get to such wire-ups in the code?
Side question: The code above works, but the main window shows in the wrong size. How do I ensure that it appears in the right size? Should I do this with minimum height/width constraints?
Use Signals and Slots Editing Mode for connecting predefined Qt signals directly to predefined Qt slots.
So for "Close" button on a simple dialog, you can just drag a connection from the button to the dialog, select the clicked() signal and the reject() slot, click "OK", and there would be nothing more to do.
For signals and/or slots you want to define yourself, you do not need to "prepare" anything in Designer beforehand. Everything should be done in your own code.
Your example already demonstrates this fairly well, but the connection could be done much more simply and cleanly, like this:
self.myWidget.HelloWorldButton.clicked.connect(self.slot1)
As for your main window having the "wrong size": it's difficult to tell from the code you've shown, but it may be because you did not set a layout in the widget that you are loading.
BTW: is there a specific reason you're using QUiLoader? Compiling python modules using pyuic4 is much more flexible, and you can learn a lot from the code that is generated.
EDIT
For me, setting a layout on the main form cures the resizing problem you are talking about.
If you don't know how to do that: in Designer, right-click on a blank part of the main form, and then select Layout/Layout in a Grid from the menu (there's also a button on the toolbar for this).
Once you've done that, resizing the form will automatically stretch it to fit the contained widgets.
I edited the .ui file:
Close QT designer
Edit the .ui in the slot/signal area
Run it
Related
I want to use btn1 to send a signal to the slot function of deviceTypeSelect, but btn1 can only be associated with QStackedWidget.
There are two problems.
The first is conceptual: Designer cannot know anything about custom slots that are going to be implemented in a promoted widget, and it even should not. The connection to those slots can only be achieved programmatically, and that's for good reasons: Designer cannot "inspect" (nor it should) the source of the files that will be promoted, and it's also completely possible that one would promote a widget and use different classes with the same design files.
The other problem is technical: the "pages" of multipage widgets like QStackedWidget or QTabWidget cannot be used as signal targets on Designer. I don't know if it's just a missing feature or it's by design (knowing how multipage widget plugins are dealt with I wouldn't bet about the it), but unfortunately it really is not possible.
If you want to connect to standard QWidget slots (setEnabled() etc.), set a dummy boxed layout on the page, and add another QWidget in it, but if you use custom slots, as explained before, those must be manually connected from your code.
So I have a script running inside another program (The Foundry's Hiero) and I'm just making a new QWidget object, and calling self.show()
Now, I can set it to self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint), so my window will stay on top of the main window, even if you click on something in the main window.
The problem is, this is a sort of popup window that you configure settings in, and it triggers other QWidget popups. If I set my window to WindowStaysOnTopHint, those subdialogs that my widget triggers end up beneath my widget.
Is there a way in PySide/PyQt to make a window stay on top/keep focus from the main application window in particular, but not everything?
You can use the QApplication.focusChanged signal to raise your widget up when Hiero's main window is selected. Then you would just need to remove the WindowStaysOnTopHint flag.
I'm not familiar with Hiero's API, but I'm guessing you could try something like:
def raiseMyWidget(old, new):
if new == hiero.ui.mainWindow():
myWidget.raise_()
QtWidgets.QApplication.instance().focusChanged.connect(raiseMyWidget)
Hope this helps! You can take advantage of the old parameter or some other means to make sure that your widget isn't raised above the others as well.
I have a GUI which looks like the one in the picture:
It was designed in Qt designer now what i want is to make the layout like the one in the picture below by clicking the customize button basically hiding/showing a part of layout.
Now when i hit close button it needs to revert back to the initial state. How can i do this in the qt designer(if possible)
If not possible can anyone tell me how to do it Pyqt.
Will the command link button(qt designer) work for this function to happen??
You could connect signals and slots:
connect customizeButton, clicked() to customizeLayout, show()
connect customizeButton, clicked() to customizeButton, hide()
connect closeButton, clicked() to customizeLayout, hide()
connect closeButton, clicked() to customizeButton, show()
I guess you get the idea?
You will most likely have to provide some default settings in your program to initially hide the customizeLayout, as I fear you cannot hide stuff by default from the Designer (but I'm not sure about this - just see if there's a "visible" attribute).
I want to make a new PyQt window show up and hide the current window when I click a button. After playing around for hours, I haven't found a good way to do this.
When I try to show a new QDialog and hide the current one, the program closes. It works if another window is open. I believe this is because it is viewing the last window as closing and terminates, or maybe the new QDialog is falling off the stack.
My current method involves storing the QDialog as a variable in the current dialog, then showing it and hiding current dialog. This seems like a total hack, though.
Example below:
def _createQuestion(self):
# initialization of the UI and everything goes on in the constructor
self.dialog = QuestionBanks()
self.dialog.show()
self.hide()
Is there a better way of switching a single window to a distinct different single window in PyQt4?
I have a ctrl+f dialog and when I press the window, it causes the main window to lose focus. Is there a way to have both windows have focus?
Kind of, but not really. Only one window can claim focus on the desktop/operating system level. You want this to be your application or main PyQT object. Then, what you want to do is define a new window type (QObject) that will be treated like a toolbox/dialog within the parent application. This will set a child-like focus attribute. Your application will retain focus on the desktop level, and now you have another inner-focus attribute for windows spawned from within your app.
QtGui QStyleOptionToolBox
StackOverflow: pyqt popup window