[Qt-interest] refresh QTableWidget

Constantin Makshin cmakshin at gmail.com
Mon Jul 11 18:02:29 CEST 2011


You must handle ReceiveConfig members yourself because there's no way Qt could automagically create text representation of a non-Qt complex structure.

Here is an example of a QAbstractTableModel-based custom model (irrelevant Operation class members are omitted for clarity):

class Operation
{
public:
    inline const QString& condition () const;
    inline const QString& actionType () const;
    inline const QString& actionName () const;
    inline const QString& actionParameters () const;
    inline const QString& table () const;
    inline const QString& postCondition () const;
    inline uint nextId (bool condition = true) const;
    inline const QString& graphicsFile () const;
    inline const QString& comment () const;
};

QVariant TechniqueModel::data (const QModelIndex& index, int role) const
{
    if (!index.isValid() || role != Qt::DisplayRole)
        return QVariant();

    const Technique::Operation* operation = m_technique->operationAt(index.row());
    if (!operation)
        return QVariant();

    switch (index.column())
    {
    case Condition:
        return operation->condition();

    case Type:
        return operation->actionType();

    case Name:
        return operation->actionName();

    case Parameters:
        return operation->actionParameters();

    case Table:
        return operation->table();

    case PostCondition:
        return operation->postCondition();

    case NextOperation_True:
        return operationIdToString(operation->nextId(true));

    case NextOperation_False:
        return operation->postCondition().isEmpty() ? QVariant() : operationIdToString(operation->nextId(false));

    case GraphicsFile:
        return operation->graphicsFile();

    default: // Comment
        return operation->comment();
    }
}

On Monday, July 11, 2011 07:46:20 PM Riccardo Roasio wrote:
> I'm trying this but it says me that QVariant::QVariant(void*) is
> private within this context...why?
> 
> #ifndef RECEIVECONFIGMODEL_H
> #define RECEIVECONFIGMODEL_H
> 
> #include <QAbstractListModel>
> 
> #include <QAbstractListModel>
> #include <QObject>
> #include <QStringList>
> #include <QList>
> 
> #include "receiveconfig.h"
> 
> 
> class ReceiveConfigModel : public QAbstractListModel
> {
>     Q_OBJECT
> 
> public:
>     ReceiveConfigModel(QList<ReceiveConfig *> *l, QObject *parent = 0)
>         : QAbstractListModel(parent), list(l) {}
> 
>     int rowCount(const QModelIndex &parent = QModelIndex()) const;
>     QVariant data(const QModelIndex &index, int role) const;
>     Qt::ItemFlags flags(const QModelIndex &index) const;
> 
> 
> private:
>     QList<ReceiveConfig *> *list;
> };
> 
> 
> #endif // RECEIVECONFIGMODEL_H
> 
> 
> #include "receiveconfigmodel.h"
> 
> int ReceiveConfigModel::rowCount(const QModelIndex &parent) const
> {
>     return list->length();
> }
> 
> 
> QVariant ReceiveConfigModel::data(const QModelIndex &index, int role) const
> {
>     if (!index.isValid())
>         return QVariant();
> 
>     if (index.row() >= list->length())
>         return QVariant();
> 
>     if (role == Qt::DisplayRole)
>         return list->at(index.row());
>     else
>         return QVariant();
> }
> 
> Qt::ItemFlags ReceiveConfigModel::flags(const QModelIndex &index) const
> {
>     if (!index.isValid())
>         return Qt::ItemIsEnabled;
> 
>     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
> }
> 
> 
> 2011/7/11 Constantin Makshin <cmakshin at gmail.com>:
> > I recommend using QTableView with a model based on QAbstractListModel or QAbstractTableModel, depending on the type/structure of elements in your list.
> >
> > http://doc.qt.nokia.com/model-view-programming.html
> > http://doc.qt.nokia.com/qtableview.html
> > http://doc.qt.nokia.com/qabstractlistmodel.html
> > http://doc.qt.nokia.com/qabstracttablemodel.html
> >
> > On Monday, July 11, 2011 01:21:02 PM Riccardo Roasio wrote:
> >> Hi,
> >>
> >> i would like to populate a QTableWidget starting from a QList so that
> >> every QList element correspond to a row
> >>
> >> If i add an element to a QList i would like to refresh the QTableWidget
> >>
> >> How can i do that?
> >>
> >> Thanks,
> >> Riccardo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110711/7b179c57/attachment.bin 


More information about the Qt-interest-old mailing list