Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delegate not work

Delegate not work

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 689 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    freebigfish
    wrote on last edited by
    #1

    hi,all
    i am a newbie.i used delegate in tabview,but it cannot work properly.
    @ view = new QTableView(this);
    QStringList list;
    list<<tr( "item1" )<<tr( "item2" )<<tr( "item3" )<<tr( "item4" );
    viewModel = new QStandardItemModel(0,list.count(),this);
    view->setModel( viewModel );
    ComboxDelegate comboBoxDelegate1;
    view->setItemDelegateForColumn(3, &comboBoxDelegate1);@

    my delegate:
    @class ComboxDelegate : public QItemDelegate
    {
    Q_OBJECT
    public:
    ComboxDelegate(QObject *parent = 0): QItemDelegate(parent) { }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    QComboBox *editor = new QComboBox(parent);
    editor->addItem("Female");
    editor->addItem("Male");
    return editor;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QComboBox comboBox = static_cast<QComboBox>(editor);
    int tindex = comboBox->findText(text);
    comboBox->setCurrentIndex(tindex);
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    QComboBox comboBox = static_cast<QComboBox>(editor);
    QString text = comboBox->currentText();
    model->setData(index, text, Qt::EditRole);
    }
    void updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    editor->setGeometry(option.rect);
    }
    };@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You need to explain what doesn't work.

      From a quick overview:

      @
      ComboxDelegate comboBoxDelegate1;
      view->setItemDelegateForColumn(3, &comboBoxDelegate1);
      @

      comboBoxDelegate1 will be destroyed shortly after that so it won't be used.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved