Custom editor in QAbstractTableModel - model-view-controller

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 !

Related

Changing window layout in gtk2hs

I am making an applocation to demonstrate some algorithms, and I am using gtk2hs. When the user selects an algorithm, I want the whole window to change (different layout, input, output interface...). How could I do that? For example, is it possible to change the widget in a container? I tried a table but could not changed the content of a cell, so that doesn't seem like a good idea.
Also I want to change the number of input fields according to another input field, and that seems like the same problem for me (removing widgets from a box) but it might be totally different in terms of solution.
Thanks
How to change the content of a box, can you delete a widget from it? If yes, how?
Use widgetDestroy. See for example this tutorial.

How to retrieve a mean number into matlab GUI from cell string?

I am building a matlab GUI to retrieve an average PnL number from a 1047*1 double cell string called pnl_P1 into edit text window called (function Average_PnL_Pair_1_Callback(hObject, eventdata, handles)). What is the simplest or very simple way to do this?
Do you want mean(cellfun(#str2double, pnl_P1))?
If I understand your problem correctly I'd do the following.
Do not store numbers in cell string array, but if you must, use mean(cell2mat(pnl_P1)) to get the mean value. Create a value under handles so you can reach your pnl_P1 vector from anywhere.
handles.pnl_P1 = pnl_P1;
Make sure you always update your handles after each function in your GUI. It is strongly recommended.
% Update handles structure
guidata(hObject, handles);
Insert value into edit box:
set(handles.edit1,'String',mean(cell2mat(handles.pnl_P1)));
handles.edit1 is the tag handle for the edit box you want to update.
What is the tag for your edit box?
Simple: in guide right-click on your edit box, select properties inspector, scroll down to Tag. If it says edit1 then use handles.edit1 and so on.
If you are new to Matlab GUIs I recommend this. They have stopped updating it but it's a great learning source.
I hope this helps.

Modifying Messagebox?

Heloo there? Is there any way to change the color [back and forecolor] of a Messagebox? Iam using VB6.0... Thanks in advance!
To the best of my knowledge, there is no way to do this. There is, however, a typical workaround.
The most common method for creating custom "message boxes" in VB6 is to create a new form in your project that acts specifically as a message box. You Show it when you need it, and then Hide or Unload it when the user clicks "OK" or "Cancel" or whatever. You can size the form the same as (or differently from) a MsgBox, create the buttons you want, make the colors as you choose, etc. Whenever you would have a message box pop up to tell the user something, you will instead call this form and change the text/color/other variables to whatever you need them to be.
This may seem annoying at first, but once you've done it once, it's very easy to see how useful a tool this new form template can become. It's code you might find yourself frequently reusing between various applications.
If you need any help with forms or form events, this is a pretty good basic tutorial which should tell you most of what you need to make this work:
http://www.vb6.us/tutorials/understanding-forms-vb6-tutorial
Good luck!

Is there a SetText message for the Win32 ListBox control?

This is easy in .NET (not my question) but I'm trying to figure out if it is possible to simply change the text of a string in a Win32 list box control given an index.
There is a GetText function that takes an item index but nothing to change the text of an existing item/string. My workaround will be to remove it and add it back in the box (which is also a weird prospect since there is no single command to add a string + item data -- these must be done carefully by inserting the string and then setting the item data on the index of the inserted string, which is tricky (not possible?) with sorting active).
Yes, the lack of a LB_SETITEMTEXT message is a bit weird.
You should put your Delete+Insert+SetData calls between calls to WM_SETREDRAW...
At the risk of being off topic...
I tend to use the ListView control all of the time. You'll want it in report view to mimic a listbox, and, as a plus, it supports multiple columns.
Oh.. and it has a LVM_SETITEM Message :)
http://msdn.microsoft.com/en-us/library/bb761186(v=VS.85).aspx
Although this question is old, but I think this documentation presented by Microsoft will be able to answer anyone questions based on this one.
So according to Microsoft documentation which you can find here
Changes the text of a list-view item or subitem. You can use this
macro or send the LVM_SETITEMTEXT message explicitly.
void ListView_SetItemText(
hwndLV,
i,
iSubItem_,
pszText_
);
And it also presents other macros for managing the list box. You can build a wrapper around this macros to simplify handling list view controls, etc.

Is there a simple way to combine a text and icon in an NSCell in Cocoa?

I'm trying to create a very simple selection list widget based on NSOutlineView. However, I'm having a hard time figuring out how to display an icon and a label right next to it, which is really the expected behavior in all the mainstream implementations of that kind of widget out there (iTunes, mail, Finder,...).
So far I am just binding two separate cells, but then when I'm expanding the tree, the icon cell grows larger and a gap appears between the icon and its accompanying label. I know I can probably overcome this problem by extending NSCell and provide a custom class, but as what I'm trying to achieve is really the standard thing, I can't be resigned to accept that there isn't a simpler solution.
Candide
Sadly, there isn't a 'text and icon' cell that you can just use, fresh out of the box as you would like. However, when I was working on a project, I found that Apple released some sample code that implements this, since it is such a common idiom.
This can be found here, specifically ImageAndTextCell.h/m
It can help teach you about UI customization by reading through this example, but scratching that, just dropping the ImageAndTextCell straight into your project should do just fine.
You need to create ImageAndTextcell to combine text and icon..
you can create ImageAndTextcell like this Sample Project

Resources