Trying to render TextLine in itemdelegate , but can't see it

  • trying to render TextLine in item delegate , but can't see it
    hi I have item deleget that I use as item in ListView . I try to set TextLine in it , but I can't seam to set it right , it compiles fine
    but in the end I can see the TextLine
    here is my code ( taken from "here ": ref )

    @const qreal PrettyItemDelegate::THUMB_HEIGHT = 50.0;
    const qreal PrettyItemDelegate::THUMB_WIDTH = 50.0;
    const qreal PrettyItemDelegate::PADDING = 10.0;
    const qreal PrettyItemDelegate::ITEM_HEIGHT = THUMB_HEIGHT+20;
    const qreal PrettyItemDelegate::ITEM_WIDTH = THUMB_WIDTH+20;

    PrettyItemDelegate::PrettyItemDelegate(QObject* parent)
    : QStyledItemDelegate(parent), downloadInfo(true) {
    smallerBoldFont = FontUtils::smallBold();
    smallerFont = FontUtils::small();


    QWidget *PrettyItemDelegate::createEditor(QWidget parent, const QStyleOptionViewItem &/ option /, const QModelIndex &/ index */) const
    QTextEdit *editor = new QTextEdit(parent);
    editor->setGeometry(QRect(40, 30, 401, 31));
    //editor->setStyleSheet(QString::fromUtf8("background-color: transparent;;"));
    editor->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
    // editor->setFrameShadow(QFrame::Plain);
    editor->setText("This is text test!!1");

    return editor;

    void PrettyItemDelegate::setEditorData(QWidget editor, const QModelIndex &index) const
    QTextEdit * textEdit = static_cast<QTextEdit
    //int value = index.model()->data(index, Qt::EditRole).toUInt();
    // do somthing with the widgets

    void PrettyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel model, const QModelIndex &index) const
    QTextEdit * textEdit = static_cast<QTextEdit
    //model->setData(index, comboBox->currentIndex(), Qt::EditRole);

    void PrettyItemDelegate::updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &/ index */) const

    PrettyItemDelegate::~PrettyItemDelegate() { }

    QSize PrettyItemDelegate::sizeHint( const QStyleOptionViewItem& /option/,
    const QModelIndex& /index/ ) const
    return QSize( 256, THUMB_HEIGHT+20.0);

    void PrettyItemDelegate::paint( QPainter* painter,
    const QStyleOptionViewItem& option, const QModelIndex& index ) const {

    int itemType =;
    if (itemType == ItemTypeVideo) {
        QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
        paintBody( painter, option, index );

    } else
    QStyledItemDelegate::paint( painter, option, index );


    void PrettyItemDelegate::paintBody( QPainter* painter,
    const QStyleOptionViewItem& option,
    const QModelIndex& index ) const {

    painter->translate( option.rect.topLeft() );
    QRectF line(0, 0, option.rect.width(), option.rect.height());

    if (downloadInfo) line.setWidth(line.width());
    // get the video metadata
    const StreamItemPointer pStreamItemPointer = VideoRole ).value<StreamItemPointer>();
    const StreamItem *pStreamItem =;

    // thumb
    if (!pStreamItem->thumbnail().isNull()) {
        painter->drawImage(QRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT), pStreamItem->thumbnail());

    // media thumb
    painter->drawImage(QRect(THUMB_WIDTH+10, 0, THUMB_WIDTH, THUMB_HEIGHT), pStreamItem->Mediathumbnail());
    //save state
    // separator button line on each item
    painter->drawLine(0, ITEM_HEIGHT-1, option.rect.width(), ITEM_HEIGHT-1);



    what im doing wring here ?

  • createEditor will be called when client start edit item that's why you dont see its first. Imho If you whant to build in some widget use QAbstractItemView::setIndexWidget

  • sorry but i dont understand you can see my other "post ":
    also doesn't work

  • Mb some example help you.
    #include <QtGui/QApplication>
    #include <QtGui>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QListView view;
    QStandardItemModel model;
    QStandardItem item1;
    QStandardItem item2;

    // set
    QTextEdit *edit = new QTextEdit;
    edit->setText("<font color = "red">item1</font>");

    view.setIndexWidget(model.index(0, 0), edit);
    edit = new QTextEdit;
    edit->setText("<font color = \"green\">item2</font>");
    view.setIndexWidget(model.index(1, 0), edit);

    // get
    edit = static_cast<QTextEdit*>(view.indexWidget(item1.index()));
    qDebug() << edit;

    edit = static_cast<QTextEdit*>(view.indexWidget(item2.index()));
    qDebug() << edit;;
    return a.exec(&#41;;


    and for more see QAbstractItemView::setIndexWidget

    P.S. And I do not know if this ideology correct or not.

  • Hi and thanks for the replay
    this is not my problem , im trying to add the QTextEdit inside the item delegate class, its custom item
    that i have , i like to add the item QTextEdit together with pictures and text in the same item.
    if i just could make the spinbox example work with the QTextEdit i think i will take it from there .

  • Hm...
    What do you try get from QTextEdit? Rich text? Mb use QLabel?

    If text and picture are static I think my way simply that painting all in paint method.

  • simple text to be filled with setText() and to be readonly

  • If i right understood ideology of m/v and item u can not correctly set:


    |widget1 | widget2| widget3|


    |widget1 | widget2| widget3|

    with painting method.

    But you can try:

    @class MyWidget : public QWidget {

    QLabel *pictureLabel;
    QLabel *textLabel;

    QHBoxLayout *layout;

    MyWidget(QWidget *parent = 0) : QWidget(parent) {
    pictureLabel = new QLabel(this);
    textLabel = new QLabel(this);

        layout = new QHBoxLayout(this);
        layout->setContentsMargins(0, 0, 0, 0); // margin: top right bottom left
    void setText(const QString& text) {
        textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    QString text() const {
        return textLabel->text();
    void setPicture(const QString& path) {


    and than use code above but:

    view.setIndexWidget(item1.index(), new MyWidget);

Log in to reply

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