Frage

I want to be able to create a vertical list of items that will give a similar appearance as to what you see in PowerPoint where it shows a vertical list of tiles that represent each slide.

I believe using a QListView with a QAbstractListModel is the right way to go about this. My QAbstractListModel subclass contains a QList of QGraphicsViews. So, each tile (or slide) is a QGraphicsView. I've spent a lot of time trying to find a similar example on the internet and I've also been using Qt's example but haven't had much luck.

Here is my model class...

I can get the "ADDING" message in addgvw, but I can't get the "HERE" message in data.

myDataModel::myDataModel(QObject *parent) : QAbstractListModel(parent)
{
}

QVariant myDataModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    cGraphicsViewWrapper* gvw = GVWrapperList.at(index.row());

    if (role == Qt::DisplayRole)
    {
        qDebug() << "HERE";

        //Return the QGraphicsView object to display
        return QVariant::fromValue(gvw->gvwView());
    }

    return QVariant();
}

int myDataModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return GVWrapperList.size();
}

void myDataModel::addgvw(int role, cGraphicsViewWrapper& gvw)
{
    if (role == Qt::EditRole)
    {
        beginInsertRows(QModelIndex(), GVWrapperList.size(), GVWrapperList.size());

        GVWrapperList.append(&gvw);

        qDebug() << "ADDING " << GVWrapperList.size() << rowCount();

        endInsertRows();
    }
}

This is how I use my model class...

In my constructor of my widget class I call

myGVWWidget::myGVWWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::myPlottingWidget)
{
    ui->setupUi(this);

    Model = new myDataModel(this);

    ui->GWVListView->setModel(Model);

    connectAllSignals();
}

...then in my function that creates new tiles, after a tile is created I call...

Model->addChart(Qt::EditRole, *gvw);

In the end, my widget is empty :(

EDIT: I want to add, since I think it's likely I may get asked, for numerous reasons, I'm not interested in converting to a QPixMap and displaying an image for a tile.

War es hilfreich?

Lösung

Your problem is QAbstractItemView and friends expect DisplayRole data to be a QString and nothing else:

http://doc.qt.digia.com/qt/qt.html#ItemDataRole-enum

Your best bet will be using QListWidget which does have a way to use QWidget for items: http://doc.qt.digia.com/qt/qlistwidget.html#setItemWidget

Also, using QGraphicsView carry a huge overhead. QGraphicsView is a really complicated widget and QGraphicsScene it needs is even more complicated. Be prepared for slow performance if you have more than a handful of items in the list.

You are really overkilling this. :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top