Qt Item Delegate - qt

I am new to Qt and am slowly finding my way around. My goal is to have a QListView of a QFileSytemModel where the names of the files in the icons wraps, similar to the behavior found on any OS where the text gets split if the name is too long.
From perusing the internet, I believe I need to create a custom class that extends QAbstractItemDelegate to do my special drawing and text wrapping. However, I haven't been able to find the default ItemDelegate that the stock QListView class uses out of the box.
The reason I want the default class is so I can poke around and figure out more about the life-cycle of Qt components while working on my own renderer. I was wondering if anyone knew of where the default renderer for the QListView class could be found?

If you want to show icons with text, QListView has a mode to do that, just set the view mode to QListView::IconMode using QListView::setViewMode(). If you still want to customize display features it is right that you should implement a custom item delegate, preferably subclassing QItemDelegate and overriding paint() with your own implementation.

Related

Use Model/View on a QListWidget

Yes, you are right. We have QListView already, it is prefect when we are trying to display simple list with Model/View.
But, QListView has lots of problem when we need to display complex list with rich text and widgets. Just think about a timeline-listview for Facebook or Twitter.
Sure, we can implement our own delegate for rich text or images, but ListView can print static item only. So, there isn't a way to show clickable hyperlink (you can calculate position of the mouse and hyperlink, but it is a really drity work) or load asynchronous images.
Well, QListWidget seems our solution. We can put widgets into it. But. we will lost our Model/View/Delegate architecture, that is terrible!
Now, my solution is writing my listview in QML. Other widget are still native Qt widget. (I don't like a non-native pure QML user interface.)
QML is really flexiable when doing that kind of work. Then export my model, finally put a viewer into my QMainWindow. But coding in two programming languages and trying to communicate with other native widget is really difficult.
So, is there a way to use Qt's Model/View architecture with QListWidget? Or I have to implement them by myself?
QListWidget does use Qt's MVC, as it derives from QListView and...
QListWidget uses an internal model to manage each QListWidgetItem in
the list.
Just use QListWidget::model () const to access the model.

Best way to display dynamic element on a QListView (with Model/View)?

We can implement our own delegate to display rich text or images, but ListView can print static item only. You can't put "real" items into it, you can just paint them.
So, there isn't a way to show clickable hyperlink, ReTweet buttons, or load asynchronous images. Just think about a timeline-listview for Facebook or Twitter. That's what I'm working on.
Now, my solution is writing my listview in QML. Other widget are still native Qt widget. (I don't like a non-native pure QML user interface.)
QML is really flexible when doing that kind of work. Then export my model, finally put a viewer into my QMainWindow. But coding in two programming languages and trying to communicate with other native widget is really difficult.
So, what's the best way to display dynamic element?
The MVC framework is not very good for this kind of work.
To do it properly, you would need to provide a delegate for whatever dynamic types you need to display, and then provide an external mechanism that forces the model to emit dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) whenever these types need redrawing. It gets worse with interactive content because you would need to force an update on mouse overs in order to trigger the delegate painting.
For stuff like this you are better off using QGraphicsScene/View. Rather than rely on a model, each item can take of itself and you still get only essential repaints (via it's BSP structure), plus you have the option of hardware acceleration.

How to use my treeview subclass in qt designer?

I have a class myTreeView which is a subclass of QTreeView, which I am using in other widget and doing layout manually. now I want to include myTreeView in the new widget using designer so that I can avoid layout code. any suggestions/reference, how to do this ?
Place a QTreeView into your layout in Qt Designer. Right click the QTreeView, click Promote to... add a New Promoted Class definition using the form at the bottom of the dialog.
i.e. specify the base class of your derived class as QTreeView, give the widget a name, and specify where Qt Design can find the header file for your derived class.
That should allow you, at a minimum, to place your widget on the form as you lay it out. It will most likely show up as a grey empty box (much like a QWidget) on the layout however when you compile and build a project using your .ui file your widget will appear.

Customize QListWidgetItem

I am creating my first app in QT and wanted to design a list. The listitem has two texts and one icon.
The problem is, i cant find any example or helping material, Only helping link i found is : Customize QListWidgetItem but i dont understand it. Although i have the same problem which this links points to...
What i understand is, i have two options:
1- Customize QListWidgetItem to use with QListWidget
2- Make some delegate to use with QListView
I was hoping to get started with option 1. Now in the link, some reply talks about "myItem" which is taking 2 texts as input. I want to know the implementation of "myItem".
In the link you posted, MyItem is just a normal QWidget. This means that you can create a widget in Qt Designer and then set that new widget you created as the widget that the QListWidgetItem should use for display. In the above example, MyItem takes two strings because there is a vertical layout with two labels in it (that's my assumption at least).
You should also note, and is discussed in the above link, that taking the approach of setting an item widget to use for every item in the list is an expensive thing to do in terms of performance and memory consumption. Because a QListWidget is a QListView, you can set an item delegate on it just like any other view and come out with a lighter weight solution (one instantiation of class vs. one instantiation for every item in your list)

Customize List View Item in Nokia Qt App

I am writing a simple file browser app with Nokia Qt4.7 on Symbian^3 platform. I can display the directory/file list in the listview widget using QFileSystemModel. But I want to customize the list view item. And I am using QItemDelegate to do the trick overriding sizHint and paint functions. I want to display a checkbox in the end of every item (ListMode) or in the right down corder of the icon(IconMode). How can I do it?
Thanks.
I'd suggest you to reimplement QItemDelegate::paint function and use QStylePainter and use QStylePainter::drawControl to render checkbox element. Depending on the mode you can vary your painting.
You can also do this using QML . Styling rows is much easier in QML.
To be more specific your Model will still be c++ . Only the list can be in QML.

Resources