QWidget and QTextEdit in QAbstractTableModel



  • Hey. I have a QAbstractTableModel binded to a TableView and i want in Column 3 a QWidget and in Column 4 i want a QTextEdit. How can i do this?

    At the moment i can only show the first 2 Columns with an Int and a QString, like this.

    QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::DisplayRole)
        {
            if (index.column() == 0)
                return int(item.at(index.row())->mId);
            else if (index.column() == 1)
                return QString(item.at(index.row())->mDate);
        }
        else if (role == Qt::DecorationRole)
        {
    //        if (index.column() == 2)
    //            return qvariant_cast<StarTable>(item.at(index.row())->mRating);
    //        else if (index.column() == 3)
    //            return item.at(index.row())->mText;
        }
    

    Do i really need a QStyledItemDelegate to do this? How can i bind that Delegate to Column 3 and 4? How must my Data Method in the Model needs to look like? in the Data Method i can only return QVariant. So how i need to edit Data? So many Questions. Im really new to this.


  • Moderators

    @Fuel
    the model should only provide the data. No connection to a widget in particular.
    Thus a item delegate (QStyledItemDelegate) exists. The delegate is responsible for the appearance of the data in a item view widget.

    One way is to overload QStyledItemDelegate's createEditor() method and check the index. Create the widget you need here and return it. Then in the item view you can switch the corresponding index to edit mode by using QAbstractItemView::openPersistentEditor().

    Or do you just want to render multi-line text (without editing facilities) in the column?
    This is trickier: You need to overwrite the delegate's sizeHint() and paint() methods. And use a QTextDocument to do the text layouting and paint it using QTextDocument::drawContents() in the delegate's paint method.



  • As Example. My Widget has 5 Stars, its a Rating, and my Data for it is an int with 0-5. So my Model returns the int like a 3 and the ItemDelegate uses the QWidget to display the int value with 3 filled Stars? Is that what you mean?

    I hope i understand you right.


  • Moderators



  • That Example is to complicated for my needs. I dont need it Editable and i have a Class that inherits from a QWidget. Dont know why its so complicated to put the Widget in this Cell. I go and try some Parts from that Example.


  • Moderators

    @Fuel
    antoher possibility is to use QAbstractItemView::setIndexWidget()
    But persistent editors / index widgets shouldn't be used for big lists.



  • i tried it, but nothing will be displayed

    ui->tvDiaryList->setIndexWidget(diarytreemodel->index(1, 3, QModelIndex()), new QPushButton);
    

  • Moderators

    @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

    diarytreemodel->index(1, 3, QModelIndex())

    make sure the returned index is valid at the time you call this line of code -> means the model already has it's data set



  • i decided to use a QStyledItemDelegate now. At the moment i dont know how i can paint the QWidget in the paint Event of the Delegate. This is what i have from a Tutorial, but its incomplete.

    void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
    {
        if (index.data().canConvert<StarTable>())
        {
            StarTable starTable = qvariant_cast<StarTable>(index.data());
    
            if (option.state & QStyle::State_Selected)
                painter->fillRect(option.rect, option.palette.highlight());
    
            starTable.paintEvent(); //dont know how to paint
        }
        else
        {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }
    

  • Moderators

    @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

    starTable.paintEvent(); //dont know how to paint

    -> starTable.render(painter);

    Although is your widget really refrenced by value? Meaning not a pointer?!
    I don't think that this even compiles.



  • i changed that. How can i set now, that the Delegate will shown in my Column? Do i need to setup in the Model the Data Method? Or just add the Delegate to the TableView? At the moment it doesnt work.

    Sorry but thats all new to me and its much that i need to learn and understand. At the moment its to much.


  • Moderators

    @Fuel
    you always get the index passed. Check the column (and/or row) of it.



  • i know what you mean, but where?

    update:

    to show you something

    DiaryTableModel *diarytablemodel = new DiaryTableModel(this);
        StarDelegate *stardelegate = new StarDelegate(this);
        ui->tvDiaryList->setModel(diarytablemodel);
        ui->tvDiaryList->setItemDelegateForColumn(2, stardelegate);
        ui->tvDiaryList->setSelectionBehavior(QAbstractItemView::SelectRows);
    
    void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
    {
        if (index.data().canConvert<StarTable*>())
        {
            StarTable *starTable = qvariant_cast<StarTable*>(index.data());
    
            starTable->render(painter);
        }
        else
        {
            QStyledItemDelegate::paint(painter, option, index);
        }
    }
    
    QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if (index.data().canConvert<StarTable*>())
        {
            StarTable *starTable = qvariant_cast<StarTable*>(index.data());
            return starTable->sizeHint();
        }
        else
        {
            return QStyledItemDelegate::sizeHint(option, index);
        }
    }
    
    QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::DisplayRole)
        {
            if (index.column() == 0)
                return int(item.at(index.row())->Id);
            else if (index.column() == 1)
                return QString(item.at(index.row())->Date);
            else if (index.column() == 2)
            {
                
            }
            else if (index.column() == 3)
            {
    
            }
        }
    
        return QVariant();
    }
    
    void DiaryTableModel::setupModelData()
    {
        QTextEdit *text = new QTextEdit();
        text->setText("Test");
        item.append(new DiaryTableItem(0, "monday", new StarTable(2), text));
    }
    

  • Moderators

    @Fuel said in QWidget and QTextEdit in QAbstractTableModel:

    At the moment it doesnt work.

    what exactly doesn't work?
    If the column count is wrong, then your model needs to return the correct column count and data for the index.



  • It just dont shows. i get no Error or something



  • Anyone can help me? Im so close. At the moment i get an Error. The Program stopps working because of a Read Access Violation. In the Debugger i can see that the starTable Pointer dont shows to a Memory Address

    void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
    {
    
            StarTable *starTable = qvariant_cast<StarTable*>(index.data());
    
            starTable->render(painter);
    
    }
    `The Pointer shows to 0x0. Anyone knows how to pick up here in this Delegate the Class that inherits from a QWidget?

  • Lifetime Qt Champion

    Hi,

    As @raven-worx wrote, are you sure the index is correct ? In any case, an assert for a null pointer wouldn't be a bad idea.


  • Moderators

    @Fuel
    just a note: this thread also tries to achieve the same. You might want to follow it also.



  • I solved that Problem. Sorry i forgot to mark the Thread. The Problem was the Index yes. In my data() Method of my TableModel i returned my Object wrong. So the Pointer was always null.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.