Hello all again!
I am stucked and need little help. I have QTableView and Costum Delegate for tableView's second column.
I think best way to express my problem is to show screenshots and source code:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cdelegate.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int r = 0; r < 10; ++r){
model->setItem(r,0,item);
}
ui->tableView->setModel(model);
CDelegate *cdelegate = new CDelegate;
ui->tableView->setItemDelegateForColumn(1,cdelegate);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cdelegate.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QStandardItemModel;
for(int r = 0; r < 10; ++r){
QStandardItem *item = new QStandardItem("Item " + QString::number(r));
model->setItem(r,0,item);
model->setItem(r,1,new QStandardItem("Qt is cool"));
}
ui->tableView->setModel(model);
CDelegate *cdelegate = new CDelegate;
ui->tableView->setItemDelegateForColumn(1,cdelegate);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
cdelegate.h
#ifndef CDELEGATE_H
#define CDELEGATE_H
#include <QAbstractItemDelegate>
{
public:
CDelegate();
//I think this method would help to my problem
};
#endif // CDELEGATE_H
#ifndef CDELEGATE_H
#define CDELEGATE_H
#include <QAbstractItemDelegate>
class CDelegate : public QAbstractItemDelegate
{
public:
CDelegate();
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
//I think this method would help to my problem
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
};
#endif // CDELEGATE_H
To copy to clipboard, switch view to plain text mode
and cdelegate.cpp
#include "cdelegate.h"
#include <QPainter>
#include <QTableView>
CDelegate::CDelegate()
{
}
{
}
{
if(index.row()%2 == 0){
painter
->setBrush
(QColor(239,
239,
239));
painter->fillRect(option.rect,painter->brush());
}
}
{
//...
}
#include "cdelegate.h"
#include <QPainter>
#include <QTableView>
CDelegate::CDelegate()
{
}
QSize CDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(45,30);
}
void CDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.row()%2 == 0){
painter->setBrush(QColor(239,239,239));
painter->fillRect(option.rect,painter->brush());
}
}
void CDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
//...
}
To copy to clipboard, switch view to plain text mode
So,result is this:
Capture.JPG
This is a problem:
I achieved what I wanted BUT the text: "Qt is cool" isn't appeared in the second column,for every row.
I tried many solution but without any result.
Have any idea?
Bookmarks