Addressing a styling issue not accessable from QT Designer - user-interface

I am working on a QTreeWidget and I can't see a property for column size in the edit items... context menu. So I guess the cleanest way to perform this step is to attach application code to a signal (and not inheritance).
What signal do I attach to. More specifically, What signal should I attach to, to execute code that should be part of visual styling which should be editable from a GUI Builder, but isn't, or is to elaborate to model in a GUI builder. I guess i'm looking for a signal that is a one off call meant for further styling.
edit:
I guess I'll stick to inheritance, still if I shouldn't, or if there is indeed a one-off signal please post, and state merits :D

Related

Implementing a Custom Cocoa Event Tracking Loop

I'm working on a custom cross platform UI library that needs a synchronous "ShowPopup" method that shows a popup, runs an event loop until it's finished and automatically cancels when clicking outside the popup or pressing escape. Keyboard, mouse and scroll wheel events need to be dispatched to the popup but other events (paint, draw, timers etc...) need to be dispatched to their regular targets while the loop runs.
Edit: for clarification, by popup, I mean this kind of menu style popup window, not an alert/dialog etc...
On Windows I've implemented this fairly simply by calling GetMessage/DispatchMessage and filtering and dispatching messages as appropriate. Works fine.
I've much less experience with Cocoa/OS X however and finding the whole event loop/dispatch paradigm a bit confusing. I've seen the following article which explains how to implement a mouse tracking loop which is very similar to what I need:
http://stpeterandpaul.ca/tiger/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/chapter_5_section_4.html
but... there's some things about this that concern me.
The linked article states: "the application’s main thread is unable to process any other requests during an event-tracking loop and timers might not fire". Might not? Why not, when not, how to make sure they do?
The docs for nextEventMatchingMask:untilDate:inMode:dequeue: states "events that do not match one of the specified event types are left in the queue.". That seems a little odd. Does this mean that if an event loop only asks for mouse events then any pressed keys will be processed once the loop finishes? That'd be weird.
Is it possible to peek at a message in the event queue without removing it. eg: the Windows version of my library uses this to close the popup when it's clicked outside, but leaves the click event in the queue so that clicking outside the popup on a another button doesn't require a second click.
I've read and re-read about run loop modes but still don't really get it. A good explanation of what these are for would be great.
Are there any other good examples of implementing an event loop for a popup. Even better would be pseudo-code for what the built in NSApplication run loop does.
Another way of putting all this... what's the Cocoa equivalent of Windows' PeekMessage(..., PM_REMOVE), PeekMessage(..., PM_NOREMOVE) and DispatchMessage().
Any help greatly appreciated.
What exactly is a "popup" as you're using the term? That term means different things in different GUI APIs. Is it just a modal dialog window?
Update for edits to question:
It seems you just want to implement a custom menu. Apple provides a sample project, CustomMenus, which illustrates that technique. It's a companion to one of the WWDC 2010 session videos, Session 145, "Key Event Handling in Cocoa Applications".
Depending on exactly what you need to achieve, you might want to use an NSAlert. Alternatively, you can use a custom window and just run it modally using the -runModalForWindow: method of NSApplication.
To meet your requirement of ending the modal session when the user clicks outside of the window, you could use a local event monitor. There's even an example of just such functionality in the (modern, current) Cocoa Event Handling Guide: Monitoring Events.
All of that said, here are (hopefully no longer relevant) answers to your specific questions:
The linked article states: "the application’s main thread is unable to process any other requests during an event-tracking loop and
timers might not fire". Might not? Why not, when not, how to make
sure they do?
Because timers are scheduled in a particular run loop mode or set of modes. See the answer to question 4, below. You would typically use the event-tracking mode when running an event-tracking loop, so timers which are not scheduled in that mode will not run.
You could use the default mode for your event-tracking loop, but it really isn't a good idea. It might cause unexpected re-entrancy.
Assuming your pop-up is similar to a modal window, you should probably use NSModalPanelRunLoopMode.
The docs for nextEventMatchingMask:untilDate:inMode:dequeue:
states "events that do not match one of the specified event types are
left in the queue.". That seems a little odd. Does this mean that if
an event loop only asks for mouse events then any pressed keys will be
processed once the loop finishes? That'd be weird.
Yes, that's what it means. It's up to you to prevent that weird outcome. If you were to read a version of the Cocoa Event Handling Guide from this decade, you'd find there's a section on how to deal with this. ;-P
Is it possible to peek at a message in the event queue without removing it. eg: the Windows version of my library uses this to close
the popup when it's clicked outside, but leaves the click event in the
queue so that clicking outside the popup on a another button doesn't
require a second click.
Yes. Did you notice the "dequeue:" parameter of nextEventMatchingMask:untilDate:inMode:dequeue:? If you pass NO for that, then the event is left in the queue.
I've read and re-read about run loop modes but still don't really get it. A good explanation of what these are for would be great.
It's hard to know what to tell you without knowing what you're confused about and how the Apple guide failed you.
Are you familiar with handling multiple asynchronous communication channels using a loop around select(), poll(), epoll(), or kevent()? It's kind of like that, but a bit more automated. Not only do you build a data structure which lists the input sources you want to monitor and what specific events on those input sources you're interested in, but each input source also has a callback associated with it. Running the run loop is like calling one of the above functions to wait for input but also, when input arrives, calling the callback associated with the source to handle that input. You can run a single turn of that loop, run it until a specific time, or even run it indefinitely.
With run loops, the input sources can be organized into sets. The sets are called "modes" and identified by name (i.e. a string). When you run a run loop, you specify which set of input sources it should monitor by specifying which mode it should run in. The other input sources are still known to the run loop, but just ignored temporarily.
The -nextEventMatchingMask:untilDate:inMode:dequeue: method is, more or less, running the thread's run loop internally. In addition to whatever input sources were already present in the run loop, it temporarily adds an input source to monitor events from the windowing system, including mouse and key events.
Are there any other good examples of implementing an event loop for a popup. Even better would be pseudo-code for what the built in
NSApplication run loop does.
There's old Apple sample code, which is actually their implementation of GLUT. It provides a subclass of NSApplication and overrides the -run method. When you strip away some stuff that's only relevant for application start-up or GLUT, it's pretty simple. It's just a loop around -nextEventMatchingMask:... and -sendEvent:.

What is the equivalent of python's "window.get_property("visible") in GTK+ (C)?

In order to show some window upon a clicking a button in the main window, i have to use "on_delete_event" signal to show or hide the appropriate windows. I know the pygtk "window.get_property("visible")". I searched the documentation there is no such thing as gtk.get
_property all i found was "gtk.mnemonics.get_visible or something like this.
I'm pretty sure somebody has used show and hide stuff in GTK+(C)
The functions you want are g_object_get() and g_object_set(). These apply to all GObjects, not just GtkWidget.
In the case of GtkWidget's "visible" property there is also a gtk_widget_get_visible() function (and a gtk_widget_is_viisble() function that also checks all parents of a GtkWidget) and an equivalent gtk_widget_set_visible().
Remember that GtkWindow is derived from GtkWidget; the former will use the latter's methods and properties where appropriate.

Is there a common navigation convention in an MVVM Light Windows Phone application?

As my app gets bigger I am finding that the same type of entity is appearing more and more throughout, typically in a listbox.
When someone taps on the entity, I want to navigate to the detailed view of this entity.
An example of this would be a product: it appears in a product list by category, it appears in search results, or it might appear as a saved favourite. In my instance, all of these would come from a table in local SQLCE storage.
Every time I surface the entity in some way that someone could tap on it, I basically cut the same code: subscribe to the SelectionChange event on the listbox, extract the entity, use MVVM Light messaging to signal that the selected entity has changed, then call NavigationService to do its business.
Has anyone worked on something similar and, if so, what have you came up with? Or more importantly, if there's some MVVM Light toolkit convention that I'm overlooking, could you please point me in the right direction?
Are you calling the NavigationService from code-behind or the View Model now? If the former, you could check the WP7 Navigation arcticle by LBugnion for triggering it from the VM. If you combine that with the EventToCommand behavior (like in here) you should be able to create a nice and clean "MyEntitySelected" command in the VM which takes the entity as a parameter.
I think the question here is, how do I deal with this without cut/paste the same code each time? If this is the case, maybe trying to package the entity/listbox thing in a XAML usercontrol! Take a look at this: http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps
Then if the representation needs to be different, you could have a look at this: http://www.windowsphonegeek.com/articles/Implementing-Windows-Phone-7-DataTemplateSelector-and-CustomDataTemplateSelector ( this example does different styling in one listbox, but you can change this to only apply different style on each different page with a listbox on it )

Why Dialog is not a Component in Scala Swing?

This question arises because I want to capture LostFocusEvent from Dialog. Unfortunately, this event is not fired for non Component derived components. What I want to ask is what is the aim of Dialog not derived from Component, but RichWindow, as compared to the Swing library?
I think really the best way to get the answer is to contact Ingo Maier directly, but you may be able to deduce the some of the thinking behind from his blog article The scala.swing package in 2.8 and beyond:
Window hierarchy
This is how the Java AWT/Swing window hierarchy looks like:
While AWT's Frame and Dialog share the common base class Window, the Swing extensions have no subclass relationship or extend a common interface even though they share common functionality not present in AWT. In an attempt to straighten things up a little, we redesigned the window base hierarchy in scala.swing which now looks as follows:
[...]
This design lets us factor common wrapper code into class RichWindow.
So I am guessing that this is a deliberate design by the author stating that Windows are in fact not components in the same way frames and buttons are, and can be dropped on to other containers.

Distinguish between designer and runtime code

I have two processes which exange messages each other.
Process A is a normal (i.e non-qt) program which sends messages to process B.
Process B is a QT GUI application showing received messages into a text box.
I created a customized widget (called ShowMessages) which inherits from QPlainTextEdit and reads messages from a pipe when a timer expires, and appends them in the text box.
Code is not really designed like this (which would be pretty bad design I think), but it's just to make things simple here.
For reasons I won't tell, process A cannot be shut down while I'm creating the form using qt-designer.
Problem is that while I'm using qt designer (thus selecting the ShowMessages widget and putting it within the window) the widget begins to show messages, even if I'm in the designer. This feature is cool but the problem is that when I save the form, already present messages are saved in .ui file, which results in turn in bad behaviour when I start process B (because process starts showing messages I received during the creation phase).
I could clean the text box just after process B starts, but I think that avoiding messages to be present in the .ui file is much better. What I want is to be able to write code like this for the widget:
if <I'm not in the designer>
timer = QtCore.QTimer(self)
QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("on_timer()"));
timer.start(2000)
Is there an qt function to know if I'm in the designer? Do you think that distinguishing between designer and runtime code is "bad"?
(Sorry for mistakes, but English is not my primary language)
Maybe your widget should have a flag for whether it's "active" and default it to False so while you're in the designer, it doesn't do anything at all. In code you would set it to active when you want to see the messages. Then you also have the ability to turn it off in other scenarios as well.
But I have to say, it sounds like you're putting "controller" code into a "view" widget which can and probably will spell trouble for you down the road (including the current Qt designer problem you're having now).
Consider reading up on the MVC (model-view-controller) design pattern, if you haven't already.
Update:
To be fair, your question did ask how to detect whether you're in designer :)
http://doc.trolltech.com/4.3/designer-creating-custom-widgets.html#creating-well-behaved-widgets
To give custom widgets special
behavior in Qt Designer, provide an
implementation of the initialize()
function to configure the widget
construction process for Qt Designer
specific behavior. This function will
be called for the first time before
any calls to createWidget() and could
perhaps set an internal flag that can
be tested later when Qt Designer calls
the plugin's createWidget() function.
According to the doc, you basically could set your "inDesignerFlag" to true in the initialize() function of your widget. Then detect that flag where required in your widget's code.

Resources