<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://programmingexamples.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://programmingexamples.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Nelson+Barata</id>
		<title>ProgrammingExamples - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://programmingexamples.net/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Nelson+Barata"/>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/wiki/Special:Contributions/Nelson_Barata"/>
		<updated>2026-05-13T18:18:33Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.23.5</generator>

	<entry>
		<id>http://programmingexamples.net/wiki/User_talk:Nelson_Barata</id>
		<title>User talk:Nelson Barata</title>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/wiki/User_talk:Nelson_Barata"/>
				<updated>2012-09-25T07:46:29Z</updated>
		
		<summary type="html">&lt;p&gt;Nelson Barata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Welcome to ''ProgrammingExamples''!'''&lt;br /&gt;
We hope you will contribute much and well. &lt;br /&gt;
You will probably want to read the [[Help:Contents|help pages]].&lt;br /&gt;
Again, welcome and have fun! [[User:Daviddoria|Daviddoria]] 13:55, 12 September 2012 (UTC)&lt;br /&gt;
&lt;br /&gt;
Hi Nelson! Thanks for your first contribution!! Could I ask you to leave a brief not in the &amp;quot;Summary&amp;quot; of the changes/additions you make? This way when I come along and check them out, I can see &amp;quot;ah, it is better to create the object on the heap instead of pass the address of an object on the stack because...&amp;quot;. Keep the contributions coming! [[User:Daviddoria|Daviddoria]] 14:08, 12 September 2012 (UTC)&lt;br /&gt;
&lt;br /&gt;
Ok, I just found the &amp;quot;summary&amp;quot; field when editing a page. Will think about it for futures contributions. [[User:Nelson Barata|Nelson Barata]] 09:44, 25 September 2012 (GMT)&lt;/div&gt;</summary>
		<author><name>Nelson Barata</name></author>	</entry>

	<entry>
		<id>http://programmingexamples.net/wiki/Talk:Qt/Delegates/ComboBoxDelegate</id>
		<title>Talk:Qt/Delegates/ComboBoxDelegate</title>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/wiki/Talk:Qt/Delegates/ComboBoxDelegate"/>
				<updated>2012-09-12T14:09:41Z</updated>
		
		<summary type="html">&lt;p&gt;Nelson Barata: Created page with 'In the main function: allocating de ComboBoxDelegate on the heap rather than the stack. Second method didn't worked for me.'&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In the main function:&lt;br /&gt;
allocating de ComboBoxDelegate on the heap rather than the stack.&lt;br /&gt;
Second method didn't worked for me.&lt;/div&gt;</summary>
		<author><name>Nelson Barata</name></author>	</entry>

	<entry>
		<id>http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate</id>
		<title>Qt/Delegates/ComboBoxDelegate</title>
		<link rel="alternate" type="text/html" href="http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate"/>
				<updated>2012-09-12T14:05:57Z</updated>
		
		<summary type="html">&lt;p&gt;Nelson Barata: /* main.cpp */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==main.cpp==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QHeaderView&amp;gt;&lt;br /&gt;
#include &amp;lt;QItemSelectionModel&amp;gt;&lt;br /&gt;
#include &amp;lt;QStandardItemModel&amp;gt;&lt;br /&gt;
#include &amp;lt;QTableView&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;ComboBoxDelegate.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  std::cout &amp;lt;&amp;lt; &amp;quot;Enter.&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
  QApplication app(argc, argv);&lt;br /&gt;
&lt;br /&gt;
  QStandardItemModel model(4, 2);&lt;br /&gt;
  QTableView tableView;&lt;br /&gt;
  tableView.setModel(&amp;amp;model);&lt;br /&gt;
&lt;br /&gt;
  ComboBoxDelegate* delegate = new ComboBoxDelegate(this);&lt;br /&gt;
  //tableView.setItemDelegate(&amp;amp;delegate);&lt;br /&gt;
  tableView.setItemDelegateForColumn(1, delegate); // Column 0 can take any value, column 1 can only take values up to 8.&lt;br /&gt;
&lt;br /&gt;
  for (int row = 0; row &amp;lt; 4; ++row)&lt;br /&gt;
    {&lt;br /&gt;
    for (int column = 0; column &amp;lt; 2; ++column)&lt;br /&gt;
      {&lt;br /&gt;
      QModelIndex index = model.index(row, column, QModelIndex());&lt;br /&gt;
      int value = (row+1) * (column+1);&lt;br /&gt;
      std::cout &amp;lt;&amp;lt; &amp;quot;Setting (&amp;quot; &amp;lt;&amp;lt; row &amp;lt;&amp;lt; &amp;quot;, &amp;quot; &amp;lt;&amp;lt; column &amp;lt;&amp;lt; &amp;quot;) to &amp;quot; &amp;lt;&amp;lt; value &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
      model.setData(index, QVariant(value));&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  // Make the combo boxes always displayed.&lt;br /&gt;
  for ( int i = 0; i &amp;lt; model.rowCount(); ++i )&lt;br /&gt;
    {&lt;br /&gt;
    tableView.openPersistentEditor( model.index(i, 1) );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  tableView.show();&lt;br /&gt;
  return app.exec();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ComboBoxDelegate.h==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef COMBOBOXDELEGATE_H&lt;br /&gt;
#define COMBOBOXDELEGATE_H&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QItemDelegate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class QModelIndex;&lt;br /&gt;
class QWidget;&lt;br /&gt;
class QVariant;&lt;br /&gt;
&lt;br /&gt;
class ComboBoxDelegate : public QItemDelegate&lt;br /&gt;
{&lt;br /&gt;
Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
  ComboBoxDelegate(QObject *parent = 0);&lt;br /&gt;
  &lt;br /&gt;
  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;amp;option, const QModelIndex &amp;amp;index) const;&lt;br /&gt;
  void setEditorData(QWidget *editor, const QModelIndex &amp;amp;index) const;&lt;br /&gt;
  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;amp;index) const;&lt;br /&gt;
  void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;amp;option, const QModelIndex &amp;amp;index) const;&lt;br /&gt;
  void paint(QPainter *painter, const QStyleOptionViewItem &amp;amp;option, const QModelIndex &amp;amp;index) const;&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
  std::vector&amp;lt;std::string&amp;gt; Items;&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ComboBoxDelegate.cpp==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;ComboBoxDelegate.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QComboBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;&lt;br /&gt;
#include &amp;lt;QModelIndex&amp;gt;&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;lt;QString&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)&lt;br /&gt;
:QItemDelegate(parent)&lt;br /&gt;
{&lt;br /&gt;
  Items.push_back(&amp;quot;Test0&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test1&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test2&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test3&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test4&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test5&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test6&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test7&amp;quot;);&lt;br /&gt;
  Items.push_back(&amp;quot;Test8&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &amp;amp;/* option */, const QModelIndex &amp;amp;/* index */) const&lt;br /&gt;
{&lt;br /&gt;
  QComboBox* editor = new QComboBox(parent);&lt;br /&gt;
  for(unsigned int i = 0; i &amp;lt; Items.size(); ++i)&lt;br /&gt;
    {&lt;br /&gt;
    editor-&amp;gt;addItem(Items[i].c_str());&lt;br /&gt;
    }&lt;br /&gt;
  return editor;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &amp;amp;index) const&lt;br /&gt;
{&lt;br /&gt;
  QComboBox *comboBox = static_cast&amp;lt;QComboBox*&amp;gt;(editor);&lt;br /&gt;
  int value = index.model()-&amp;gt;data(index, Qt::EditRole).toUInt();&lt;br /&gt;
  comboBox-&amp;gt;setCurrentIndex(value);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &amp;amp;index) const&lt;br /&gt;
{&lt;br /&gt;
  QComboBox *comboBox = static_cast&amp;lt;QComboBox*&amp;gt;(editor);&lt;br /&gt;
  model-&amp;gt;setData(index, comboBox-&amp;gt;currentIndex(), Qt::EditRole);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &amp;amp;option, const QModelIndex &amp;amp;/* index */) const&lt;br /&gt;
{&lt;br /&gt;
  editor-&amp;gt;setGeometry(option.rect);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;amp;option, const QModelIndex &amp;amp;index) const&lt;br /&gt;
{&lt;br /&gt;
  QStyleOptionViewItemV4 myOption = option;&lt;br /&gt;
  QString text = Items[index.row()].c_str();&lt;br /&gt;
&lt;br /&gt;
  myOption.text = text;&lt;br /&gt;
&lt;br /&gt;
  QApplication::style()-&amp;gt;drawControl(QStyle::CE_ItemViewItem, &amp;amp;myOption, painter);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==CMakeLists.txt==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cmake&amp;quot;&amp;gt;&lt;br /&gt;
cmake_minimum_required(VERSION 2.6)&lt;br /&gt;
&lt;br /&gt;
PROJECT(ComboBoxDelegate)&lt;br /&gt;
&lt;br /&gt;
FIND_PACKAGE(Qt4 REQUIRED)&lt;br /&gt;
INCLUDE(${QT_USE_FILE})&lt;br /&gt;
&lt;br /&gt;
QT4_WRAP_CPP(MOCSrcs ComboBoxDelegate.h)&lt;br /&gt;
&lt;br /&gt;
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})&lt;br /&gt;
&lt;br /&gt;
ADD_EXECUTABLE(ComboBoxDelegate main.cpp&lt;br /&gt;
ComboBoxDelegate.cpp&lt;br /&gt;
${MOCSrcs}&lt;br /&gt;
)&lt;br /&gt;
TARGET_LINK_LIBRARIES(ComboBoxDelegate ${QT_LIBRARIES})&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nelson Barata</name></author>	</entry>

	</feed>