http://programmingexamples.net/w/api.php?action=feedcontributions&user=Nelson+Barata&feedformat=atom
ProgrammingExamples - User contributions [en]
2024-03-29T12:29:40Z
User contributions
MediaWiki 1.23.5
http://programmingexamples.net/wiki/User_talk:Nelson_Barata
User talk:Nelson Barata
2012-09-25T07:46:29Z
<p>Nelson Barata: </p>
<hr />
<div>'''Welcome to ''ProgrammingExamples''!'''<br />
We hope you will contribute much and well. <br />
You will probably want to read the [[Help:Contents|help pages]].<br />
Again, welcome and have fun! [[User:Daviddoria|Daviddoria]] 13:55, 12 September 2012 (UTC)<br />
<br />
Hi Nelson! Thanks for your first contribution!! Could I ask you to leave a brief not in the "Summary" of the changes/additions you make? This way when I come along and check them out, I can see "ah, it is better to create the object on the heap instead of pass the address of an object on the stack because...". Keep the contributions coming! [[User:Daviddoria|Daviddoria]] 14:08, 12 September 2012 (UTC)<br />
<br />
Ok, I just found the "summary" field when editing a page. Will think about it for futures contributions. [[User:Nelson Barata|Nelson Barata]] 09:44, 25 September 2012 (GMT)</div>
Nelson Barata
http://programmingexamples.net/wiki/Talk:Qt/Delegates/ComboBoxDelegate
Talk:Qt/Delegates/ComboBoxDelegate
2012-09-12T14:09:41Z
<p>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.'</p>
<hr />
<div>In the main function:<br />
allocating de ComboBoxDelegate on the heap rather than the stack.<br />
Second method didn't worked for me.</div>
Nelson Barata
http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate
Qt/Delegates/ComboBoxDelegate
2012-09-12T14:05:57Z
<p>Nelson Barata: /* main.cpp */</p>
<hr />
<div>==main.cpp==<br />
<source lang="cpp"><br />
#include <QApplication><br />
#include <QHeaderView><br />
#include <QItemSelectionModel><br />
#include <QStandardItemModel><br />
#include <QTableView><br />
<br />
#include <iostream><br />
<br />
#include "ComboBoxDelegate.h"<br />
<br />
<br />
int main(int argc, char *argv[])<br />
{<br />
std::cout << "Enter." << std::endl;<br />
QApplication app(argc, argv);<br />
<br />
QStandardItemModel model(4, 2);<br />
QTableView tableView;<br />
tableView.setModel(&model);<br />
<br />
ComboBoxDelegate* delegate = new ComboBoxDelegate(this);<br />
//tableView.setItemDelegate(&delegate);<br />
tableView.setItemDelegateForColumn(1, delegate); // Column 0 can take any value, column 1 can only take values up to 8.<br />
<br />
for (int row = 0; row < 4; ++row)<br />
{<br />
for (int column = 0; column < 2; ++column)<br />
{<br />
QModelIndex index = model.index(row, column, QModelIndex());<br />
int value = (row+1) * (column+1);<br />
std::cout << "Setting (" << row << ", " << column << ") to " << value << std::endl;<br />
model.setData(index, QVariant(value));<br />
}<br />
}<br />
<br />
// Make the combo boxes always displayed.<br />
for ( int i = 0; i < model.rowCount(); ++i )<br />
{<br />
tableView.openPersistentEditor( model.index(i, 1) );<br />
}<br />
<br />
tableView.show();<br />
return app.exec();<br />
}<br />
<br />
<br />
</source><br />
<br />
==ComboBoxDelegate.h==<br />
<source lang="cpp"><br />
#ifndef COMBOBOXDELEGATE_H<br />
#define COMBOBOXDELEGATE_H<br />
<br />
#include <string><br />
#include <vector><br />
<br />
#include <QItemDelegate><br />
<br />
class QModelIndex;<br />
class QWidget;<br />
class QVariant;<br />
<br />
class ComboBoxDelegate : public QItemDelegate<br />
{<br />
Q_OBJECT<br />
public:<br />
ComboBoxDelegate(QObject *parent = 0);<br />
<br />
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
void setEditorData(QWidget *editor, const QModelIndex &index) const;<br />
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;<br />
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
<br />
private:<br />
std::vector<std::string> Items;<br />
<br />
};<br />
#endif<br />
<br />
</source><br />
<br />
==ComboBoxDelegate.cpp==<br />
<source lang="cpp"><br />
#include "ComboBoxDelegate.h"<br />
<br />
#include <QComboBox><br />
#include <QWidget><br />
#include <QModelIndex><br />
#include <QApplication><br />
#include <QString><br />
<br />
#include <iostream><br />
<br />
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)<br />
:QItemDelegate(parent)<br />
{<br />
Items.push_back("Test0");<br />
Items.push_back("Test1");<br />
Items.push_back("Test2");<br />
Items.push_back("Test3");<br />
Items.push_back("Test4");<br />
Items.push_back("Test5");<br />
Items.push_back("Test6");<br />
Items.push_back("Test7");<br />
Items.push_back("Test8");<br />
}<br />
<br />
<br />
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const<br />
{<br />
QComboBox* editor = new QComboBox(parent);<br />
for(unsigned int i = 0; i < Items.size(); ++i)<br />
{<br />
editor->addItem(Items[i].c_str());<br />
}<br />
return editor;<br />
}<br />
<br />
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const<br />
{<br />
QComboBox *comboBox = static_cast<QComboBox*>(editor);<br />
int value = index.model()->data(index, Qt::EditRole).toUInt();<br />
comboBox->setCurrentIndex(value);<br />
}<br />
<br />
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const<br />
{<br />
QComboBox *comboBox = static_cast<QComboBox*>(editor);<br />
model->setData(index, comboBox->currentIndex(), Qt::EditRole);<br />
}<br />
<br />
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const<br />
{<br />
editor->setGeometry(option.rect);<br />
}<br />
<br />
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const<br />
{<br />
QStyleOptionViewItemV4 myOption = option;<br />
QString text = Items[index.row()].c_str();<br />
<br />
myOption.text = text;<br />
<br />
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);<br />
}<br />
<br />
</source><br />
<br />
==CMakeLists.txt==<br />
<source lang="cmake"><br />
cmake_minimum_required(VERSION 2.6)<br />
<br />
PROJECT(ComboBoxDelegate)<br />
<br />
FIND_PACKAGE(Qt4 REQUIRED)<br />
INCLUDE(${QT_USE_FILE})<br />
<br />
QT4_WRAP_CPP(MOCSrcs ComboBoxDelegate.h)<br />
<br />
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})<br />
<br />
ADD_EXECUTABLE(ComboBoxDelegate main.cpp<br />
ComboBoxDelegate.cpp<br />
${MOCSrcs}<br />
)<br />
TARGET_LINK_LIBRARIES(ComboBoxDelegate ${QT_LIBRARIES})<br />
<br />
</source></div>
Nelson Barata