c++ - How can I apply a graphic effect to the image in QListView? -
i apply graphic effect pixmap of list item in qlistview.
what should achieve that?
as far understand, need make own delegate that. how use qgraphicseffect in it?
update.
if qlistwidget used, can following effect. create widgets every list item , apply desired qgraphicseffect them. widget go (for example):
class portraitviewwidget : public qframe { q_object public: explicit portraitviewwidget(qwidget* parent = nullptr) { auto imageview = new qwidget(); auto imageviewlayout = new qvboxlayout(); auto imagelabel = new qlabel(); auto textlabel = new qlabel(); // test defaults imagelabel->setpixmap(qpixmap("/lenna.png")); imagelabel->setscaledcontents(true); static qreal quality = 0.f; quality += 0.1752f; if(quality > 1.f) quality = 1.f; textlabel->settext(qstring("%1%").arg(quality * 100.f, 0, 'f', 1)); textlabel->setalignment(qt::aligncenter); textlabel->setstylesheet( "qlabel {" " background-color: white;" " color: black;" " font-size: 16px;" " padding: 2px; }"); imageviewlayout->addwidget(imagelabel); imageviewlayout->addwidget(textlabel); imageviewlayout->setmargin(0); imageviewlayout->setspacing(0); imageviewlayout->setcontentsmargins(0, 0, 0, 0); imageview->setlayout(imageviewlayout); auto effect = new qgraphicsdropshadoweffect(); effect->setblurradius(55); effect->setoffset(0.f); effect->setcolor(qt::green); imageview->setgraphicseffect(effect); imageview->setsizepolicy( qsizepolicy::expanding, qsizepolicy::expanding); imageview->setminimumsize(240, 320); imageview->setmaximumsize(480, 640); auto layout = new qvboxlayout(); layout->addwidget(imageview); layout->setmargin(25); setlayout(layout); } };
but in case have implement updating data on widgets reflect contnts hand, , thoroughly bothersome.currently, qlistview changing data in model simple , straightforward - , can change used model on fly.
is there way achieve same outlook of item? maybe there pattern of implementing delegates may applicable...
inspired following question: how blur qpixmap image, came following solution: use implementation of dropshadow filter in delegate, instead of trying use qgraphicseffect
there.
so, arrived @ this:
qt_begin_namespace extern q_widgets_export void qt_blurimage(qpainter *p, qimage &blurimage, qreal radius, bool quality, bool alphaonly, int transposed = 0 ); qt_end_namespace #define radius 20 void gallerydelegate::paint(qpainter* painter, const qstyleoptionviewitem& option, const qmodelindex& index) const { if(option.decorationsize.isvalid() && (option.decorationposition == qstyleoptionviewitem::top)) { painter->save(); qpixmap decoration(index.data(qt::decorationrole).value<qpixmap>()); //1. paint background painter->fillrect(option.rect, option.backgroundbrush); //2. make image shadow qrect src(qpoint(0, 0), option.decorationsize); src.translate(radius, radius); qrect dst(src.adjusted(-radius, -radius, radius, radius + option.fontmetrics.height())); qimage tmp(dst.size(), qimage::format_argb32_premultiplied); tmp.fill(0); qpainter tmppainter(&tmp); tmppainter.setcompositionmode(qpainter::compositionmode_source); tmppainter.fillrect(src.adjusted(-3, -3, 3, 3 + option.fontmetrics.height() * 1.2), qt::white); qrect textrectangle(radius, src.bottom(), tmp.width() - 2 * radius, tmp.height() - src.bottom() - radius); tmppainter.end(); // blur alpha channel qimage blurred(tmp.size(), qimage::format_argb32_premultiplied); blurred.fill(0); qpainter blurpainter(&blurred); qt_blurimage(&blurpainter, tmp, radius*1.5f, false, true); blurpainter.end(); tmp = blurred; // blacken image... tmppainter.begin(&tmp); tmppainter.setcompositionmode(qpainter::compositionmode_sourcein); tmppainter.fillrect(tmp.rect(),qt::green); tmppainter.end(); // draw blurred drop shadow... painter->drawimage(option.rect.topleft(), tmp); // draw actual pixmap... painter->drawpixmap(src.translated(option.rect.topleft()), decoration.scaled(src.size(), qt::keepaspectratio, qt::smoothtransformation)); //4. draw text under painter->fillrect(textrectangle.adjusted(0, 2, 0, -2).translated(option.rect.topleft()), qt::white); painter->drawtext(textrectangle.translated(option.rect.topleft()), qt::aligncenter, index.data(qt::displayrole).tostring()); if(option.state & qstyle::state_selected) { qpen highlight(qt::magenta, 5); qrect border(option.rect); border.adjust(3, 3, -3, -3); painter->setpen(index.data(qt::red); painter->drawroundedrect(border, 5.f, 5.f); } painter->restore(); } else qstyleditemdelegate::paint(painter, option, index); }
most of code performs blur taken qpixmapdropshadowfilter implementation.
Comments
Post a Comment