Qt display image in a new window - qt

I am naive to Qt and GUI programming.
Qt jpg image display
The procedure given for displaying an image is working fine and thank you for providing that. But I want to display an image when I click on radiobutton.
I created a slot, and I connected the button click event to the slot (dispImage is my slot). My slot consists only the code which is working to display an image (First answer in this link).
I am able to compile it and run it. But the o/p is not as we desire.
On button click, image window flashes for a sec and disappears.
One more point to share is, I tried the same with windowsflags example present in qt examples.
In this example I want to display the image on the preview window created by us. Even this is also not worked for me.
Please provide me the solution.
Thanks in advance.

This is happening because the method that you're connecting to the slot is creating all of the objects needed to display the image on the stack and they're going out of scope and being destroyed when that method returns. The linked example has the event loop running at the end of the method, so the objects don't go out of scope until the program exits.
You can fix this by making the necessary objects member variables of a class that has application lifetime.

Stu's answer above is correct.
If you don't understand what he's talking about, you may want to get a little bit more familiar with C++ before jumping into Qt. The example you are citing is different from what you are trying to do. It's a main() function whose stack variables aren't going to go out of scope until the process exits (that's when main() returns). The stack variables in a method go out of scope as soon as the method returns.
Using the example you cite as a template for your method, you need to declare the QGraphicsView object as a class variable in the header file for your radioslot object. This will make it so it stays in scope until your radioslot object is destroyed.

Related

Xamarin.Mac -- AppDelegate DidFinishLaunching -- Is the "Main Interface" specified in Info.plist already supposed to be loaded?

I have an existing .xib that needs to be displayed as the main page in a Xamarin.Mac application. If I set the "Main Interface" as this existing .xib, it does display but it's not done by the time my DidFinishLaunching override gets called. This is where I expect to do some initialization, but the outlets I specified are all null.
I am new to Xamarin and I'm trying to figure out if I need to override a different call that lets me know that the main page is done loading or if the issue lies elsewhere.
I did see that an NSWindowController has a WindowDidLoad override, but if I set the Main Interface in the info.plist, how does it know what the ViewController is for that view so that my override WindowDidLoad gets called?
I'm thinking I probably need to create the main window manually in DidFinishLaunching, but I can't find any documentation on how to create the main window through an .xib. There is documentation on how to get a storyboard from a nib, but I am not seeing anything on loading a .xib. If someone can find a good reference on how the main window gets initialize (with all the plumbing included) in a Xamarin.Mac application, that may be enough.
Thank you in advance for your time.
The chain of events is roughly:
Main invokes NSApplication.Main
Reads Info.plist to determine what xib/storyboard to load.
Storyboard is loaded and inflated. The view controller has a custom class that points to "ViewController"
Your ViewController is spun up, and WindowDidLoad get called.
DidFinishLaunching is not exactly what you are looking for.
May I suggest take a look at the documentation in question to get a better understand of the full process:
https://developer.xamarin.com/guides/mac/getting_started/hello,_mac/#Anatomy_of_a_Xamarin.Mac_Application

PyQt5 - Signals&Slots - How to enable a button through content change of lineEdit?

since there isn't any useful How-To for beginners of PyQt5, I'm stuck with a little problem about signals & slots.
I do have a lineEdit which will get a directory path by a dialog (which works fine).
If one starts the program this lineEdit is empty by default and that's ok. A button, which shall generate a file list and show it in a list view, exists. It's functionality is implemented already and working fine. This button by default is disabled when the program is started for the first time.
What I want to do now is, enabling the button when something is entered at the lineEdit and disabling it when the content is removed.
I gave it a shot with this:
self.lineEdit_SelectedDirectory.editingFinished.connect(self.pushButton_CreateFileList.setEnabled)
but I get the error that it has not enough arguments, which might be clear because there aren't any provided in this code line.
Again, this was just a guess. How would one do this?
I would love something which takes the content into account like that, that it decides whether the content is empty or not and than sets the appropriate value to the button.
thx in advance for the help.
Christian
Looks like you want the textChanged signal, since that sends the current text:
self.lineEdit_SelectedDirectory.textChanged.connect(
lambda text: self.pushButton_CreateFileList.setEnabled(bool(text)))

QGraphicScene redraw on QGraphicItem move

I'm trying to fiddle a bit with a Qt example regarding 2D Graphics.
Basically it is a small diagram editor. In the example, the author explains that the tracknodes() function is used to update the line's endpoints, when the user drags a connected node into a different position.
This actually does not happen. After looking at the code, I have the feeling it's because there is no event being called after moving the node QGraphicItem, in order to update the link's rendering.
I figured I have to find out where and how the QGraphicItem's movement is handled in order to send a wasDropped signal (or something like that) to a slot that re-renders the link. Does this make sense?
I'm pretty new to Qt/C++ in general, so I don't have a very clear idea on how to achieve this, does anyone have any pointers he/she could share?
He seems to be using the QGraphicsItem::itemChange virtual function to call the tracknodes() function which draws the lines. ItemChange should be called every time a node is moved. It's called in other cases as well but he only uses it for tracking the movement of the QGraphicsItem.

Qt: How to initialize dialog widgets?

I would like to know what the established procedure is for initializing the controls within a Qt custom dialog box. In the code I am writing, the dialog would present a QListView containing directories from an object passed (by reference) to the dialog class during construction. When the dialog is displayed, I obviously want the list to display the directories currently configured in the object.
Where should this be done though? Perhaps in the overridden showEvent() method?
Background: I used to do a lot of MFC programming back in the day, and would have done this sort of stuff in the OnCreate method, or some such, once the window object had been created.
Thankfully Qt doesn't require you to do any hooking to find the moment to create things (unless you want to). If you look over the Qt examples for dialogs, most do all the constructing in the constructor:
http://doc.qt.io/archives/qt-4.7/examples-dialogs.html
The tab dialog example--for instance--doesn't do "on-demand" initializing of tabs. Although you could wire something up via the currentChanged signal:
http://doc.qt.io/archives/qt-4.7/qtabwidget.html#currentChanged
Wizard-style dialogs have initializePage and cleanupPage methods:
http://doc.qt.io/archives/qt-4.7/qwizardpage.html#initializePage
http://doc.qt.io/archives/qt-4.7/qwizardpage.html#cleanupPage
But by and large, you can just use the constructor. I guess the main exception would be if find yourself allocating the dialog at a much earlier time from when you actually display it (via exec), and you don't want to bear the performance burden for some part of that until it's actually shown. Such cases should be rare and probably the easiest thing to do is just add your own function that you call (like finalizeCreationBeforeExec).

Programmatically close an AIR application

I would like to know the correct way to close an AIR application programmatically.
In my Spark WindowedApplication I have:
this.addEventListener( Event.CLOSING, shutdownApp );
and of course an implementation of the shutdownApp method (which basically tidies up temporary files).
This works fine for the top-right close button of the window. However I also have functionality which needs to shutdown the application. Within the code I have called:
NativeApplication.nativeApplication.exit();
However this doesn't trigger the Event.CLOSING method, and so my temporary files are not cleared up. Should I not be calling nativeApplication.exit ? If so, what should I call instead? I'd rather not have to call my shutdownApp method before the NativeApplication.exit() as this doesn't feel quite so elegant.
Can anyone shed any light on the correct way of doing this?
Thanks,
Phil
I know this question has been answered and accepted, but thought I'd share, I use.
stage.nativeWindow.close();
The documentation looks a bit ambiguous on this and I would have the same interpretation that you did. Did you try the close or exit methods on the WindowedApplication?
Something like this, with FlexGlobals and topLevelApplication:
(FlexGlobals.topLevelApplication as WindowedApplication).close();
or
(FlexGlobals.topLevelApplication as WindowedApplication).exit();
Just give an answer here because I searched for a related question and was not able to find sth.
I wanted to do something similar and close an AIR application when the native close button of the document window (spark.components.Window) is pressed thought that the spark WindowedApplication container (the applications main window) is still active to hold and manage the native menu (at application startup it is also used to display a splash screen. It has to stay open since if it is closed the native menu won't show up or be accessible anymore so its visible property is just set to false).
My main problem was the window closing event. Registering it with ActionScript like
this.addEventListener(Event.Closing, windowClosed);
does not work: No closing event was dispatched.
The only way was to register an event handler directly in the s:Window element at start of the MXML file. I just throw in the closing attribute:
closing="window1_closingHandler(event)"
The event was dispatched then and in window1_closingHandler-function I called
NativeApplication.nativeApplication.exit();
That works for me and shuts down the whole application.

Resources