I'm generating a MATLAB GUI using GUIDE, but I want to create fields when a user clicks on a button. Is there any way to dynamically add new GUI objects in the callbacks?
One way to accomplish this is to create the GUI objects at the start, but set their "Visibility" property to "off". Then, when the user clicks on a button, you set the "Visibility" property back to "on". This way, you won't be making new GUI objects while the GUI is running, you would simply be changing which parts of it are visible or not.
EDIT: If you don't know how many new GUI objects you need until run-time, this is how you would add the new GUI objects to the handles structure (where hFigure is a handle to the GUI figure):
p = uicontrol(hFigure,'Style','pushbutton','String','test',...
'Callback',#p_Callback); % Including callback, if needed
handles.test = p; % Add p to the "test" field of the handles structure
guidata(hFigure,handles); % Add the new handles structure to the figure
You would then of course have to write the callback function for the new GUI object (if it needs one), which might look something like this:
function p_Callback(hObject,eventdata)
handles = guidata(gcbf); % This gets the handles structure from the figure
...
(make whatever computations/changes to GUI are needed)
...
guidata(gcbf,handles); % This is needed if the handles structure is modified
Functions of interest that I used in the above code are: GUIDATA (for storing/retrieving data for a GUI) and GCBF (get handle of parent figure of the object whose callback is currently executing).
Using UICONTROL, you will be able to add 'fields' (called uicontrols or widgets).
You will want to specify the style to get edit boxes, buttons, etc...
You may actually want to have all the widgets already there in GUIDE and then just change the visibility or enabled property as needed.
You can find my video tutorials on GUI building in MATLAB here:
http://blogs.mathworks.com/videos/category/gui-or-guide/
This should cover this and many related topics in GUI building.
Related
I am trying to port to Python 3.x a small gui application that I have written in Matlab.
The app contains a Matlab uitable, with which you can program bitfields of registers of a microcontroller. Actually you can create a panel which groups some register-bitfields that are important to you for debugging at a specific point in time (kind-of a watch window in a compiler IDE).
It looks like this:
Gui Table used to write to register bitfields of a microcontroller
So, each row of the table can be used to program a specific register bitfield.
There are 4 columns:
1. Register name (drop down list)
2. Bitfield name (drop down list)
3. Value to write (string)
4. Format (Hex or Dec drop down list, irrelevant here)
In order to use this application, one first clicks on the register name drop down list, where all registers are shown (detail: using a text box one can apply filters to narrow down the list size).
When the register is selected, the drop down list of the bitfield column updates automatically so that it contains only the bitfields of the chosen register. This happens with the help of a callback function.
Then, the user has to enter a value and a format, and only if all cells contain valid content, then a register write command is issued (via some debugger interface).
This worked OKish; sometimes the bitfields list was actually the one of the previous register added in the table, but this could be detected and have the entry cleared so that the user can try again. Also when the number of rows becomes very high and scroll bars appear, then if the user tries to enter a register at the bottom, every time they click on the drop down list, they can choose an entry and then the scroll bar position will automatically change showing the beginning of the table. This makes the process of entering registers quite cumbersome. As far as I know, the uitable of Matlab did not have accessible properties to control this behavior.
Since I am a hw engineer with limited sw technologies knowledge, I am wondering if there is a natural way to support this in Python 3.x, say with some structured (e.g. xml) data container that can naturally map to a gui component, without so much callback programming and data validation. The ideal behavior would be that the user starts to type directly at the register name drop down list (not possible in Matlab), and dynamically gets a filtered version of the register names list.
I am completely new to python, just installed Anaconda. I have found some interesting classes in PyQt:
QListView Class,
QListWidget Class,
QTableView Class,
QTableWidget Class.
However, I would like to have the combined functionality of the tableview with a listview, as is the case with the uitable in Matlab. Or even better, a text edit input that turns into a drop down list after typing a few letters.
The pyqtgraph.tablewidget seems to augment the functionality of QTableWidget, but I think this is still not what I need.
So, if the above is not possible, or would involve heavy programming, maybe all I need is to change approach and have a single separate search box with autocompletion, which looks into a "flattened" version of the registers database, and returns results in the form my_register_1.my_bitfield_1 (maybe allowing the user to search simultaneously at both register and bitfield names). When the user clicks on one item of the "autocompletion list", then the selected entry is mapped to the currently selected line in the tableview, adding both the register name and bitfield in read-only table cells. The "value to write" cell should still be editable, and when it gets valid data it should trigger register write command...
I would appreciate if you could guide me where to look. Thanks!
You should use the Qt model/view architecture. This allows you to have a representation of your data (the "model"), which is separate from how that data is represented (the "view"). So you set up a table of data, and then set up a view to represent that data. When you switch between different data sets, you tell the view to display the new data set. The tutorial I linked to explains this in some detail
I have a very big Tcl/Tk application with a lot of widget. In order to allow easy access to modify widgets configurations without having to type it in the console per widget/configuration parameter I want to build a dialog for that.
To do so I need an option to easily select the widget to be configured.
I thought to write a function which lets the user to click any widget in the application (any dialog) and retrieves the widget path.
Any ides?
You can convert a global-coordinate (e.g., from a <Button-1> binding's %X and %Y) to a widget name using winfo containing:
bind . <Button-1> {
set w [winfo containing %X %Y]
puts "You clicked on $w"
}
Be aware that this can interact quite significantly with other bindings! You may need to investigate using a grab (carefully; global grabs can cause trouble!) and configuring the -cursor in order to tell users what is going on. It's quite do-able, but some thought may be necessary to make it work the way you want.
(Did you know that winfo containing is a scripted interface to the functionality at the core of most drag-and-drop handling? It uses exactly the system for mapping positions to windows…)
I'd like to obtain a reference to underlying java components for the controls I have in my GUI so as to customize their appearance.
I know about findjobj from Yair Altman which works really well:
myLink = uicontrol('String', '<html><u>Button that looks like a link.</u></html>');
jObj = findjobj(myLink);
jObj.setContentAreaFilled(0);
Unfortunately this solution is quite slow when there are a "lot" of controls to customize (because it has to parse the full hierarchy of objects in the figure and this for each control to customize).
Moreover the figure must be visible (else controls are not instantiated and java references cannot be found). Plus it must be moved of screen to avoid users to touch it while findjobj is running (sometimes make things crash because findjobj somehow relies on position of controls to find them while internally calling also drawnow which updates positions) ...
On some machines, even with only a few controls to customize, it can be up to 10 seconds before to have the figure to appear (most of the time is spent in findjobj).
I also know about uicomponent again from Yair Altman to directly create controls and get the handle to the underlying java component in one shot:
[myLink, jObj] = uicomponent('Style', 'JButton', 'String', '<html><u>Button that looks like a link.</u></html>');
jObj.setContentAreaFilled(0);
Unfortunately here the parent property can only be a figure and of course my controls are placed in gui layout containers to handle resizing and many other things nicely (and gui layout containter are not valid hg-handles for uicomponent to work)...
So was wondering if there could be any other fast solution to get underlying java components for controls in my GUI ? ... NB: I mainly only need to have buttons that looks like hyperlinks or animated gif (i.e. borderless buttons with htlm text/img inside).
This isn't a direct answer to your question, but I've built several GUIs that are based around GUI Layout Toolbox and that contain Java swing components. I typically arrange things so that the GUI Layout container (HBox, VBox, Grid etc) has a uipanel as a child, and then the uipanel has the Java swing component as a child.
You can typically parent a Java component to the uipanel in exactly the same way as parenting it to a figure (unlike a GUI Layout container), and it's no problem to parent a uipanel to a GUI Layout container.
So, for example, to add a button with a dropdown menu (no menu items, so it won't do anything, but just to illustrate):
>> u = uipanel;
>> ddbuttonclass = 'com.mathworks.widgets.DropdownButton';
>> ddbutton = javaObjectEDT(ddbuttonclass);
>> [jddbutton, hjddbutton] = javacomponent(ddbutton, [30,30, 60, 30], u);
Now you can parent u to a GUI Layout container, and you get all the nice resizing.
I'm not that familiar with Yair's uicomponent, but if you can get the handle of the java component somehow, you should be able to use something like the above.
PS If you want his direct input, #Yair is sometimes active on SO - he may get notified if I mention his name. If you're doing a lot of Java/MATLAB GUI work, I'd also recommend buying his book.
UICOMPONENT was designed to be a direct replacement of both Matlab's built-in UICONTROL and JAVACOMPONENT functions. This means that you can directly place a UICOMPONENT within panels, even those created by the GUI Layout toolbox.
You might need to cast the layout panel's handle to double (double(hPanel)) on some Matlab releases but that's about it:
[myLink, jObj] = uicomponent('Parent',hPanel, ...);
[myLink, jObj] = uicomponent('Parent',double(hPanel), ...); % on some Matlab releases
You could also use JAVACOMPONENT directly, but it doesn't really give you any benefits over UICOMPONENT, since UICOMPONENT uses JAVACOMPONENT under the hood and also adds important functionality (such as ensuring the component is placed on the EDT, and merging important properties from the Matlab wrapper).
As for FINDJOBJ, you can speed it up a bit by specifying the target object class using the 'class' parameter. But if your figure contains hundreds of controls it might still be slow. To this day, close to 10 years after my first version of FINDJOBJ, I still do not know of a direct way to get the underlying Java object. I assume there is one that is used internally by MathWorks, but I do not know it.
As #SamRoberts mentioned, this is all discussed in my book...
For the last few days i have been trying to figure out the best way to get AutomationElement for a specific control in a vb6 application.
My initial way of doing so was by doing a search with the following condition:
new PropertyCondition(AutomationElement.NameProperty, controlName)
I was under the assumption that this was working correctly for about a week in a little test VB6 application.
but i few days ago i realized something... when i dragged a vb6 textbox into the form, the 'Name' property and 'Text' property were both set to 'Text1'
So when i searched with:
new PropertyCondition(AutomationElement.NameProperty, 'Text1')
it return the correct element, but if i then went and set the 'Text' property to '' the same search would bring nothing back.
Question: Has anyone found a way to get a AutomationElement based on a the VB6 control name
What i have tried:
getting the MSAA equivalent interface and looking at the 'Name' property - Result: ''
http://msdn.microsoft.com/en-us/library/windows/desktop/dd318490%28v=vs.85%29.aspx
getting the control based on other properties (AutomationId, RuntimeId) - Result: AutomationId - not all controls seem to have this property available - RuntimeId - changes each time the app runs
I have looked at alot of different sites the main one is listed below - while some say they have manage to get it working - i don't believe i can see how they do it.. or i just dont understand it :$
http://blogs.msdn.com/b/brianmcm/archive/2006/01/17/getting-the-winforms-id-of-a-control.aspx
While i have access to the demo app, i will not access to the production app as that has been created by a third party.
What i plan on doing from here is to get the Automation element based on their position on the form..
Thank you
Can't comment due to low rep. Do you absolutely HAVE to have an AutomationElement?
You may want to look at invoking [user32.dll] (http://pinvoke.net/default.aspx/user32.EnumChildWindows). Look at FindWindowEx, GetWindow, EnumWindows, EnumChildWindows, GetWindowText, etc.
You need the handle of the parent window, so you can use this loop to get it. From there you can use the other functions to get the information you need about the control.
IntPtr hWnd = IntPtr.Zero;
foreach(var process in System.Diagnostics.Process.GetProcesses())
if(condition)
hWnd = process.Handle;
Comment with the exact information you need out of the VB6 window, and I'll give you better code.
You can use the relational position of the AutomationElement in a specific Window (or any other container for that matter), in order to detect it. For example, if you have 5 TextBox AutomationElements in your main window, and you're certain that the order will not be changing, you could create a PropertyCondition on the TextBox class name, and then use the FindAll method to return a collection of AutomationElements and iterate through it, querying the BoundingRectangle property to find out which is the lowest one (or middle, or any other position, for that matter).
I would create a helper method that would return a Dictionary<int,AutomationElement> with the key being the visual position of the AutomationElement, and the value being the AutomationElement itself.
This way you can avoid using a specific Point on your screen (any window size change or element positioning will easily break your code) while not being bound to the AutomationId property.
Does anyone have an example of using a QWidget as an editor in a QAbstractTableModel?
I have a column which when edited should create a QCombobox with the list of choices.
The docs seem to suggest I need to write a QAbstractItemDelegate and a custom paint function but that seems overkill to simply pop-up a standard QCombobox in Qt::EditRole.
Note - the combo box contents are the same for every row and it only needs to be shown when somebody clicks in the cell.
I know this should be simple but I can't get it to work. It's easy for a QTableWidget based table - but I need it for a very large data table.
The docs seem to suggest I need to write a QAbstractItemDelegate and a custom paint function but that seems overkill to simply pop-up a standard QCombobox in Qt::EditRole.
You don't need to go that far. One way is to subclass QStyledItemDelegate and then override createEditor() so that it returns your prepopulated combo box. Its setEditorData and setModelData functions will probably already suffice if you`re using basic Qt value types.
If you need something more generic that works across many different models, you can create a QItemEditorFactory that associates your editor with the correct type. This also works well with custom types.
When indicated by your view's EditTrigger, your view will get the delegate specific to the cell on which the edit is being invoked and call delegate->createEditor(...) which can then size the combo box according to the options parameter as well as set the current entry to the value specified by the model, although most of this should be handled by the QStyledItemDelegate. Thus, you won't have to worry about the Qt::EditRole directly as the view will handle that.
Did you try and have a look at the following example from Qt :
Spin Box Delegate Example
Maybe it will give you a much clearer view on the subject !
Hope it helps a bit !