#include
#include
#include
#include
#include
#include
class TransposeProxyModel : public QAbstractProxyModel
{
public:
TransposeProxyModel(QObject *p = 0) : QAbstractProxyModel(p){}
QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const{
return index(sourceIndex.column(), sourceIndex.row());
}
QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const{
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
}
QModelIndex index(int r, int c, const QModelIndex &ind=QModelIndex()) const{
return createIndex(r,c);
}
QModelIndex parent(const QModelIndex&) const {
return QModelIndex();
}
int rowCount(const QModelIndex &) const{
return sourceModel()->columnCount();
}
int columnCount(const QModelIndex &) const{
return sourceModel()->rowCount();
}
QVariant data(const QModelIndex &ind, int role) const {
return sourceModel()->data(mapToSource(ind), role);
}
};
class WidgetDelegate: public QStyledItemDelegate
{
public:
WidgetDelegate( QObject * parent = 0 )
:QStyledItemDelegate (parent)
{
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
return QStyledItemDelegate::sizeHint(option, index);
}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 1) {
int progress = index.data().toInt();
QStyleOptionProgressBarV2 progressBarOption;
progressBarOption.rect = option.rect.adjusted(2,2,-2,-2);;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.progress = progress;
progressBarOption.text = QString::number(progress) + "%";
progressBarOption.textVisible = true;
progressBarOption.textAlignment = Qt::AlignHCenter;
QApplication::style()->drawControl(QStyle::CE_ProgressBar,
&progressBarOption, painter);
} else
QStyledItemDelegate::paint(painter, option, index);
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QStandardItemModel model(3,3);
model.setData(model.index(0,0), "10");
model.setData(model.index(0,1), "20");
model.setData(model.index(0,2), "30");
model.setData(model.index(1,0), "40");
model.setData(model.index(1,1), "50");
model.setData(model.index(1,2), "60");
model.setData(model.index(2,0), "70");
model.setData(model.index(2,1), "80");
model.setData(model.index(2,2), "90");
TransposeProxyModel trans;
trans.setSourceModel(&model);
QSplitter split;
QTableView *t1 = new QTableView(&split);
t1->setModel(&model);
WidgetDelegate *delegate = new WidgetDelegate(t1);
t1->setItemDelegateForColumn(1, delegate);
QTableView *t2 = new QTableView(&split);
t2->setModel(&trans);
split.show();
QString s="QTableView QTableCornerButton::section { \
background: red; \
border: 2px outset red;}\
QProgressBar {\
border-width: 0px;\
background: blue; \
padding:-2px -2px -2px -2px;\
min-height:18px;\
max-height:18px;\
}\
\
QProgressBar::chunk {\
border-width: 0px;\
background: red; \
}\
\
QTableView { \
show-decoration-selected: 1;\
}\
\
QTableView::item {\
border: 0px solid #d9d9d9;\
border-top-color: transparent;\
border-bottom-color: transparent;\
}\
\
QTableView::item:hover {\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);\
border: 0px solid #bfcde4;\
}";
app.setStyleSheet(s);
return app.exec();
}
阅读(2510) | 评论(0) | 转发(0) |