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 ":http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegateas 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) {
    boldFont.setBold(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->setLayoutDirection(Qt::LeftToRight);
    //editor->setAutoFillBackground(false);
    //editor->setStyleSheet(QString::fromUtf8("background-color: transparent;;"));
    editor->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
    editor->setFrameShape(QFrame::WinPanel);
    // editor->setFrameShadow(QFrame::Plain);
    editor->setLineWidth(2);
    editor->setReadOnly(true);
    editor->setText("This is text test!!1");

    return editor;
    }

    void PrettyItemDelegate::setEditorData(QWidget editor, const QModelIndex &index) const
    {
    QTextEdit * textEdit = static_cast<QTextEdit
    >(editor);
    //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
    >(editor);
    //model->setData(index, comboBox->currentIndex(), Qt::EditRole);
    }

    void PrettyItemDelegate::updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &/ index */) const
    {
    editor->setGeometry(option.rect);
    }

    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 = index.data(ItemTypeRole).toInt();
    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->save();
    painter->translate( option.rect.topLeft() );
    
    
    QRectF line(0, 0, option.rect.width(), option.rect.height());
    

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

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

    // media thumb
    if(!pStreamItem->Mediathumbnail().isNull())
    {
    painter->drawImage(QRect(THUMB_WIDTH+10, 0, THUMB_WIDTH, THUMB_HEIGHT), pStreamItem->Mediathumbnail());
    }
    //save state
    painter->save();
    painter->restore();
    // separator button line on each item
    painter->setClipping(false);
    painter->setPen(option.palette.color(QPalette::Midlight));
    painter->drawLine(0, ITEM_HEIGHT-1, option.rect.width(), ITEM_HEIGHT-1);

    painter->restore();
    

    }
    @

    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 ":http://developer.qt.nokia.com/forums/viewthread/13362/
    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;
    
    model.appendRow(&item1);
    model.appendRow(&item2);
    
    view.setModel(&model);
    

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

    view.setIndexWidget(model.index(0, 0), edit);
    
    edit = new QTextEdit;
    edit->setText("<font color = \"green\">item2</font>");
    edit->setAutoFillBackground(true);
    
    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;
    
    view.show();
    
    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:

    item1

    |widget1 | widget2| widget3|

    item2

    |widget1 | widget2| widget3|

    with painting method.

    But you can try:

    @class MyWidget : public QWidget {
    Q_OBJECT

    private:
    QLabel *pictureLabel;
    QLabel *textLabel;

    QHBoxLayout *layout;
    

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

        layout = new QHBoxLayout(this);
    
        layout->addWidget(pictureLabel);
        layout->addWidget(textLabel);
    
        layout->setContentsMargins(0, 0, 0, 0); // margin: top right bottom left
    
        setLayout(layout);
    }
    
    void setText(const QString& text) {
        textLabel->setText(text);
        textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    }
    
    QString text() const {
        return textLabel->text();
    }
    
    void setPicture(const QString& path) {
        pictureLabel->setPixmap(QPixmap(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.