QListWidget的edit如何及时响应?(thinkvd开发日志)

在qtcn上提出这个问题,有人建议用QItemDelegate,createEditor方式
http://www.qtcn.org/bbs/read.php?tid=33532

测试代码:
#ifndef IMWIDGET_H

#define IMWIDGET_H
#include <QtGui>

class ImWidget : public QWidget
{
 Q_OBJECT

public:
 ImWidget(QWidget * parent = 0, Qt::WindowFlags f = 0);
 void setData();

public slots:
 void slotPrintf(const QString & currentText ){
  qDebug() << "item = " << currentText;
 }

public:
 QListWidget *listWidget;
};

#endif

#include "ImWidget.h"
#include "delegate.h"

ImWidget::ImWidget(QWidget * parent, Qt::WindowFlags f )
:QWidget(parent, f){
 listWidget = new QListWidget();

 QHBoxLayout *layout = new QHBoxLayout();
 layout->addWidget(listWidget);
 setLayout(layout);

 //connect(listWidget,  SIGNAL(currentTextChanged( const QString &)), this,  SLOT(slotPrintf(const QString &)));
}

void ImWidget::setData(){
 SpinBoxDelegate *delegate = new SpinBoxDelegate(listWidget);
 listWidget->setItemDelegate(delegate);
 
 connect(delegate,  SIGNAL(sglChangeText( const QString &)), this,  SLOT(slotPrintf(const QString &)));
 listWidget->setSortingEnabled(1);
 QListWidgetItem *lst1 = new QListWidgetItem("Oak", listWidget);//Refrencing Item to Listwidget
 QListWidgetItem *lst2 = new QListWidgetItem("Banana", listWidget);
 QListWidgetItem *lst3 = new QListWidgetItem("Apple", listWidget);
 QListWidgetItem *lst4 = new QListWidgetItem("Orange", listWidget);
 QListWidgetItem *lst5 = new QListWidgetItem("Grapes", listWidget);
 QListWidgetItem *lst6 = new QListWidgetItem("Jayesh", listWidget);

 listWidget->insertItem(1,lst1);//Item insertion
 listWidget->insertItem(2,lst2);
 listWidget->insertItem(3,lst3);
 listWidget->insertItem(4,lst4);
 listWidget->insertItem(5,lst5);
 listWidget->insertItem(6,lst6);

 lst1->setFlags(lst1->flags() | Qt::ItemIsEditable);
 lst2->setFlags(lst2->flags() | Qt::ItemIsEditable);
 lst3->setFlags(lst3->flags() | Qt::ItemIsEditable);
}
/*** delegate.h*********************************************/

#ifndef DELEGATE_H
#define DELEGATE_H

#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>

//! [0]
class SpinBoxDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    SpinBoxDelegate(QObject *parent = 0);

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const;

    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const;

    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const;

signals:
 void sglChangeText(const QString &text);
};
//! [0]

#endif


/**** delegate.cpp ********************************************/

#include <QtGui>
#include "delegate.h"


//! [0]
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}
//! [0]

//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    /*QSpinBox *editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);*/
 QLineEdit *editor = new QLineEdit(parent);
 connect(editor,  SIGNAL(textChanged( const QString &)), this,  SIGNAL(sglChangeText(const QString &)));
    return editor;
}
//! [1]

//! [2]
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();

  /*  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);*/
 QLineEdit *edit = static_cast<QLineEdit*>(editor);
 edit->setText(value);
}
//! [2]

//! [3]
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
   /* QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();

    model->setData(index, value, Qt::EditRole);*/
 QLineEdit *spinBox = static_cast<QLineEdit*>(editor);
 
 QString value = spinBox->text();

 model->setData(index, value, Qt::EditRole);
}
//! [3]

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

 

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

 ImWidget w;
 /*w.setStyleSheet("QWidget { /
  padding: 7px ; /
  color:rgb(255,255,255)} /
  QListView{ alternate-background-color: red; /
  background-color:rgb(199,147,88); }");*/
 w.setData();
 w.show();
 w.setFixedSize(400, 300);

 return a.exec();
}

你可能感兴趣的:(QListWidget的edit如何及时响应?(thinkvd开发日志))