Qt/Delegates/ComboBoxDelegate

From ProgrammingExamples
< Qt
Revision as of 10:05, 12 September 2012 by Nelson Barata (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

main.cpp

#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>
 
#include <iostream>
 
#include "ComboBoxDelegate.h"
 
 
int main(int argc, char *argv[])
{
  std::cout << "Enter." << std::endl;
  QApplication app(argc, argv);
 
  QStandardItemModel model(4, 2);
  QTableView tableView;
  tableView.setModel(&model);
 
  ComboBoxDelegate* delegate = new ComboBoxDelegate(this);
  //tableView.setItemDelegate(&delegate);
  tableView.setItemDelegateForColumn(1, delegate); // Column 0 can take any value, column 1 can only take values up to 8.
 
  for (int row = 0; row < 4; ++row)
    {
    for (int column = 0; column < 2; ++column)
      {
      QModelIndex index = model.index(row, column, QModelIndex());
      int value = (row+1) * (column+1);
      std::cout << "Setting (" << row << ", " << column << ") to " << value << std::endl;
      model.setData(index, QVariant(value));
      }
    }
 
  // Make the combo boxes always displayed.
  for ( int i = 0; i < model.rowCount(); ++i )
    {
    tableView.openPersistentEditor( model.index(i, 1) );
    }
 
  tableView.show();
  return app.exec();
}

ComboBoxDelegate.h

#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H
 
#include <string>
#include <vector>
 
#include <QItemDelegate>
 
class QModelIndex;
class QWidget;
class QVariant;
 
class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
  ComboBoxDelegate(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;
  void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 
private:
  std::vector<std::string> Items;
 
};
#endif

ComboBoxDelegate.cpp

#include "ComboBoxDelegate.h"
 
#include <QComboBox>
#include <QWidget>
#include <QModelIndex>
#include <QApplication>
#include <QString>
 
#include <iostream>
 
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
:QItemDelegate(parent)
{
  Items.push_back("Test0");
  Items.push_back("Test1");
  Items.push_back("Test2");
  Items.push_back("Test3");
  Items.push_back("Test4");
  Items.push_back("Test5");
  Items.push_back("Test6");
  Items.push_back("Test7");
  Items.push_back("Test8");
}
 
 
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
{
  QComboBox* editor = new QComboBox(parent);
  for(unsigned int i = 0; i < Items.size(); ++i)
    {
    editor->addItem(Items[i].c_str());
    }
  return editor;
}
 
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
  QComboBox *comboBox = static_cast<QComboBox*>(editor);
  int value = index.model()->data(index, Qt::EditRole).toUInt();
  comboBox->setCurrentIndex(value);
}
 
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
  QComboBox *comboBox = static_cast<QComboBox*>(editor);
  model->setData(index, comboBox->currentIndex(), Qt::EditRole);
}
 
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
  editor->setGeometry(option.rect);
}
 
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  QStyleOptionViewItemV4 myOption = option;
  QString text = Items[index.row()].c_str();
 
  myOption.text = text;
 
  QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(ComboBoxDelegate)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
QT4_WRAP_CPP(MOCSrcs ComboBoxDelegate.h)
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(ComboBoxDelegate main.cpp
ComboBoxDelegate.cpp
${MOCSrcs}
)
TARGET_LINK_LIBRARIES(ComboBoxDelegate ${QT_LIBRARIES})