Transparent color in Qt
-
@Qt-Enthusiast
implement a custom QStyledItemDelegate subclass and reimplement the paint method:void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, index); if( index == mySpecialIndex ) { painter->saveState(); painter->setPen( QPen( Qt::red, 1.0, Qt::SolidLine ) ); painter->drawRect( option.rect ); painter->restoreState(); } }
and set this delegate to your tree view widget.
-
@raven-worx said:
void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);if( index == mySpecialIndex ) { painter->saveState(); painter->setPen( QPen( Qt::red, 1.0, Qt::SolidLine ) ); painter->drawRect( option.rect ); painter->restoreState(); }
}
is there any other way to do that
-
Hi
"is there any other way to do that"Maybe you could tell why you want another way
as a delegate is the way in most cases. ? -
Hi this is my code
QVariant myModel::data(const QModelIndex& index, int role) const
{
switch (role) {
case Qt::DecorationRole:
if (index.column() == 1) {
QColor sc ;
if (NInst inst = instance(index)) {
sc = getColorForInstance(index,inst);
if(sc.isValid()) {
QPixmap cellPixmap(QSize(10,10));
cellPixmap.fill(sc);
QIcon icon(cellPixmap);
return icon;
}
}
}
break;
}QColor myModel::getColorForInstance(const QModelIndex& index ,NInst inst) const
{
QColor sc;
if (condition1) {
// transparent color
return sc;
}if(condition2) { sc = QColor(Qt::red) return sc
}
the color will be transparent in one condition and red color in other. So I want to know how to handle such case
-
@Qt-Enthusiast said:
getColorForInstance
You are showing your model.
but you still need the delegate to paint the rect?
You could create a pixmap and return that as you do for DecorationRole
-
Could you please tell me the sample example or code for the same
-
@Qt-Enthusiast said:
is there any other way to do that
Whats the problem with this way?
If you have special requirements then you need to tell us. But an item delegate is the most cleanest way to do this. -
I am just asking . This is my legacy code
QVariant myModel::data(const QModelIndex& index, int role) const {
switch (role) {
case Qt::DecorationRole:
if (index.column() == 1) {
myInst inst = getInstance(index);
QColor sc = getColorForInstance(index,inst);;
if(sc.isValid()) {
QPixmap cellPixmap(QSize(10,10));
cellPixmap.fill(sc);
QIcon icon(cellPixmap);
return icon;
}
}
break;
}QColor myModel::getColorForInstance(const QModelIndex& index ,Myinst inst) const {
QColor sc;
i f (condition1) {
// transparent color
return sc;
}if(condition2) { sc = QColor(Qt::red) return sc }
}
there alpha option in QColor constructor for transparency , is there any way I can use the same in above code
-
Hi
We tried to create a transparent image , but it is not giving me proper results .QPixmap pix(500,500);
QPainter *paint = new QPainter(&pix);
paint->setPen(QColor(255,34,255,255));
paint->setBrush(QColor(255,34,255,50));
paint->drawRect(15,15,100,100);
delete paint;Can some one guide me for that
-
so what do u get?
-
I am some black image . I want a trasparent color image with a boundary colored
-
I tried this also
QPixmap cellPixmap(QSize(10,10));
QPainter *paint = new QPainter(&cellPixmap);
paint->setPen(Qt::transparent);
paint->setBrush(Qt::transparent);
paint->drawRect(10,10,10,10);
delete paint;
QIcon icon(cellPixmap);
return icon;
I am still getting black image .What is purpose of setPen and setBrush
-
hi
can you try
paint->setPen(QColor(255,0,0));
paint->setBrush(QColor(255,0,0));
and tell me if its red.
Else something else is wrong.also u say
QPixmap cellPixmap(QSize(10,10));
paint->drawRect(10,10,10,10);which means u draw OUTSIDE your image.
the image is only 10x10.
and u tell rect to start at 10,10 -
With this code
QPixmap cellPixmap(QSize(10,10));
QPainter *paint = new QPainter(&cellPixmap);
paint->setPen(QColor(255,0,0));
paint->setBrush(QColor(255,0,0));
paint->drawRect(0,0,10,10);
delete paint;
QIcon icon(cellPixmap);I cannot see any thing . Could you correct my code
-
it looks fine except no need to new it
QPainter paint(&cellPixmap);
paint.setPen(QColor(255,0,0));
paint.setBrush(QColor(255,0,0));
paint.drawRect(0,0,10,10); -
void MainWindow::on_pushButton_3_released() { QPixmap pixmap(100,100); pixmap.fill(QColor("transparent")); QPainter paint(&pixmap); paint.setPen(QColor(255,0,0)); paint.setBrush(QColor(255,0,0)); paint.drawRect(0,0,10,10); ui->label->setPixmap(pixmap); }
This does produce red rect for me.
(testing with QLabel) -
but how to get transparent color with a boundary without any solid fill . Let me know
[link text](link url http://doc.qt.io/qt-4.8/qpainter.html#drawRect-3 , image with square with no fill just the boundary
-
@Qt-Enthusiast
paint.setBrush(QColor("transparent"));gives me