Is there an 'Onload' sort of a signal for QMainWindow? - dialog

PyQt noob here.
I need to call a dialog window as soon as the main window is loaded.
I also need the main window to be displayed in the background when this dialog is shown on top of it.
So, I'm looking for a 'onload' sort of a signal for the main window to call the function which in turn calls the dialog. Is there any other way around?

You can override the QWidget.showEvent for your QMainWindow to achieve the same effect. However, you need to keep track of whether it is the first time the window is shown or not since that method will be called every time the window is displayed after being hidden. Then use a modal QDialog so that the main window is shown in the background, but not enabled.

Related

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?
This is a very peculiar problem, and I haven't been able to find even a question like this.
A button is positioned on the main UI window. When clicked, it hides the parent window, and spawns another window at that position. I need it to work with a single click, for example, I want to press the button, and not release the mouse immediately, but hover over a certain part of the newly spawned window and then release it.
This works perfectly fine if I click the button directly , but the problem is I need to click the button from the main UI window.
I am doing it by sending "mousePressEvent" from the main UI window to the child widget's "mousePressEvent", like this:
def mousePressEvent(self, event): #parent mouse press event
#----
#----
self.FocusedButton.mousePressEvent(event) # child widget
When I activate the button from the mainUI window, the window is immediately hidden and the new window is spawned. However, since I haven't performed mouse release yet, I am stuck into the parent's mouse press event loop. The mouse doesn't invoke paint event and doesn't register mouseMoveEvent until I release the click because the mainUI window is hidden and mouse tracking doesn't work any more.
I have tried setting the flags of the window in every way, (QtCore.Qt.WA_TransparentForMouseEvents,True ) for example, but none of it worked.
The main UI window inherits from "QtWidgets.QWidget" and does not have the clicked method.

How to detect, in a QWidget, closing the main window

I know that similar question was already answered, but my case is a little bit different
Case:
I have a QWidget which contains a QLineEdit and a QListWidget
The QListWidget is a FramelessWindow which means that it is not located in the main window but it appears independently (actually it is a dynamically changing list depending on the content of the QLineEdit filed: "filter as you type")
Problems:
When I close the main window, it will not close the QListWidget automatically. After the main window closed, the QListWidget will be still seen. - I could not find event, in the QWidget, which would work for that purpose. The closeEvent(self, event) is never triggered.
When I move the main window the QListWidget will be still stay in the original position. It will not follow the QLineEdit's position. I could not find event, in the QWidget, which would work for that purpose.
Conditions:
I do not want to detect the changes in the main window. I know that the
closeEvent(self, event)
works in the main window, and I could delegate it to the QListWidget.
BUT I want to write an independent widget, which does not depend on the setting in the main window.
Can somebody help me with telling/suggesting how to detect the window close/window move inside a widget?
If you need the code (pretty long ~300 lines), I can copy it here.

PyQt: How do you clear focus on startup?

When I start up my PyQt GUI, the focus immediately goes to the text box.
I want there to be no focus on any of the buttons at the start of the program, especially not the text box.
Is there a way to remove the focus entirely or at least move the focus to a button or something?
Thanks
clearFocus() seems to work after a certain amount of delay after the window is visible. I also used setFocus() on the QMainWindow and then the textedit field lost focus.
Create a button with dimensions 0 wide by 0 high.
Set it as the default button and also early in the tab order before the other controlls that except focus; but note that it will be triggered if the user presses ENTER in some edit controls.
Call self.ui.yourbutton.setFocus() if desired for example after restore from minimized

Calling OnDraw in MFC SDI application

I am trying to make a MFC SDI application in VC++ to draw different shapes on click of the respective button. We have written our code inside the OnDraw() function. But Ondraw gets automatically called on running the application. We want it to be called only on the onclick of respective button. How do we proceed..?
When a shape button is clicked set a member variable that remembers what you want to draw.
Call Invalidate. This will cause OnDraw to be called by the framework.
In OnDraw check the member variable and draw the shape.
You must permit OnDraw to draw every time it is called by the framework. That is how the window image is restored after being uncovered, unminimized, resized, etc.
You cant prevent that OnDraw is only called when you press a button.
OnDraw in a view is called when WM_PAINT arrives and asks the window to repaint itself. Not executing OnDraw would cause nothing to be drawn at all.
OnDraw is also called when your application is minimized and maximized again, or when the size of the window changes.
Also I can not think about a scenario, where I want drawing only when a button is pressed. You need to be more precise. here.
OnDraw, as xMRi said, is called by the framework ... but if you want to draw something only when you click on a button, why don't you create a member variable, setup when you click the button, and get count when OnDraw is executed ?
void CYourAppView::OnDraw(CDC* pDC)
{
// MFC code
if(m_bButtonWasClicked)
{
// draw what ever you want
}
}

Altering the YUI menu's mousing behavior

I'm using Yui to build a "popup" menu that works a bit differently with the mouse than usual. This is not a ContextMenu, because I want it to respond to left clicks, and the ContextMenu seems bent on responding to right clicks.
Following the examples, if I do this, the menu comes up and everything is close to how I want it:
YAHOO.util.Event.addListener(myClickTarget, 'click', myThingGotClicked);
In my myThingGotClicked function, I manually set the menu's position and show() it.
My problem is that I want to "bind" the menu visibility to the state of the mouse button. That is, on a mouseDown, I want the menu to come up, and on a mouseUp, I want the menu to disappear (selecting the active item, if any). So, listening to the 'click' event doesn't do the right thing, because a "click" is only sent after mouseUp.
The "obvious" solution is to do this:
YAHOO.util.Event.addListener(myClickTarget, 'mousedown', myThingGotClicked);
But this doesn't work. Stepping through in a debugger, you can see that it does actually bring up the menu on a mousedown, but then something immediately hides the menu. At full speed, it looks like nothing happens at all.
Any thoughts?
The problem is that the MenuManager class listens for the mousedown event at the document level and hides all visible Menu instances. So, since you are building a unique sort of Menu implementation, you'll need to stop the propagation of the mousedown event inside your handler so that the MenuManager doesn't handle the event. Here is some pseudo code for you:
var myThingGotClicked = function (event) {
YAHOO.util.Event.stopPropagation(event);
// Do other stuff
};
YAHOO.util.Event.on(myClickTarget, 'mousedown', myThingGotClicked);
Todd
That's a bit closer, as the menu does pop up, but if you try to make a selection in the menu, the text selection of the page underneath goes sort of nuts. I also need to add a mouseup handler, I think, as the menu doesn't go down on mouse release.
What I really want here are menus that work like menus on every version of the Mac OS (until more recently when OS X added the "click to make the menu 'sticky' to the default behavior).

Resources