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.
Related
Is there a GridView implementation for native Qt (not for QML)? I need to read some data from model and put them into GridView.
GridView in QML support dynamic rows/columns, it's friendly to the users when resizing. Or, I have to implement it with QWidget and QGridLayout?
In my experience you have several possibilities:
As you said, use QGridLayout to display your custom data
QTableView works out of the box with Qt model classes, you can pretty much customize it easily to include widgets and other data as you wish.
Use QGraphicsView/QGraphicsScene to draw a grid, basically QML is built on top of QGraphicsView... it shouldn't be too hard.
QTableView or QTableWidget should be the starting points.
There is commercial data grid for Qt/C++ here http://www.devmachines.com/qtitandatagrid-overview.html We use it without any issues.
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.
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.
I have a QML file which contains a layout of QML items and now I want one of those items to be a QGLWidget. i.e. I want to render to a specific QML item.
Is anyone aware of how to do this?
The simplest way I suppose it to provide QML a new custom component implemented in C++. I couldn't find anything ready.
You could subclass the QDeclarativeItem and implement your OpenGL code in the paint function after using the QPainter::beginNative() function. After that you can "export" your new custom item to QML this way. This is quite simple and should work, but you'll have to setup the viewport of you QDeclarativeView to be a QGLWidget, something like this:
QDeclarativeView view;
// This is needed because OpenGL viewport doesn't support partial updates.
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdateMode);
view.setViewport(new QGLWidget);
or you'll have to use the opengl graphics system for the entire application.
Another way is using QML/3D.
This thread will give you some other information.
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.