PDA

View Full Version : QTreeWidgetItems, how to make all ttransaprent to see QTreeWidget background



antartide
23rd March 2011, 17:21
Hello to all, sorry my english =)
My problem is that:
I set a background to my QTreeWidget,
I populate my tree with QTreeWidgetItems and i want to see the background trought the items.
Try with setStyleSheet of the TreeWidget:item : nothing how can i do?
TreeWidget:


void qtg_QTreeWidget::paintEvent(QPaintEvent *event)
{


QPainter painter(viewport());
QImage image("./background.png");
QRect rect(x(),y(),image.height (),image.width());
qDebug () <<"paint" << x() << y() << image.height () <<image.width();
painter.drawImage(rect, image);
painter.end();
QTreeWidget::paintEvent(event);
};

TreeWidgetItem


gui_laucher::gui_laucher(QWidget *parent)
: QWidget(parent)
{
setupTray();
int y= m_tray->geometry().y() ;
int x= m_tray->geometry().x() ;
setGeometry(x+3,y+3,300,320);
setAttribute(Qt::WA_TranslucentBackground);
QStringList labels;
labels << tr("Software");

m_treeWidget = new qtg_QTreeWidget(this);
setStyleSheet("QTreeView::item {background-color :rgba(255,255,255,0%);}");
xmlparser();
}


I also try this:


qtg_QTreeWidgetItem::qtg_QTreeWidgetItem(QTreeWidg etItem *parent) :
QTreeWidgetItem(parent)
{
m_pProcessPath="nopath";
m_pStringParameters="noparams";

setBackground(0,QBrush(Qt::transparent));
}


I never can see the picture trought the item =(

antartide
24th March 2011, 10:31
Solved:



void qtg_QTreeWidget::drawRow(QPainter* p, const QStyleOptionViewItem &opt, const QModelIndex &idx) const
{

/* Altro metodo per disegnare la pixmap:
la recupero così:
QTreeWidgetItem * item = this->itemFromIndex(idx);
la drawo così:
p->drawPixmap(im,yPixmap,16,16,item->icon(0).pixmap(16,16));

*/
for (int col = 0; col < columnCount(); ++col)
{
QModelIndex s = idx.sibling(idx.row(), col);
if (s.isValid())
{
// Recupero il testo dell'item
QString szText=idx.data(Qt::DisplayRole).toString();
// Recupero la pixmap dell'item
QPixmap myPix = qvariant_cast<QIcon>(idx.data(Qt::DecorationRole)).pixmap(opt.decorati onSize);
// Mi recupero il rettangolo dove disegnare
int x = visualRect(s).left();
int y = visualRect(s).top();
int w = visualRect(s).width();
int h = visualRect(s).height();
QRect cRect(x,y,w,h);
// Mi calcolo la y dell'icona dell'item
int yPixmap = (y + (h / 2 - 8));
/* Posso cominciare a disegnare l'item, non disegnerò lo sfondo
perchè lo voglio trasparente, altrimenti avrei fatto così:
QImage image("percorso");
QRect rect(opt.rect);
p->drawImage(rect, image);
*/
// Disegno l'icona
p->drawPixmap(x,yPixmap,myPix.width(),myPix.height(), myPix);
// Mi sposto verso destra del rettangolo dell'item della lunghezza della pixmap + 2 per distaccarmi
// e disegno il testo
cRect.setLeft(cRect.left() + myPix.width()+2);
p->drawText(cRect,Qt::AlignLeft | Qt::AlignVCenter,szText);;
// Mi sposto verso sinistra del rettangolo visuale dell'item sottraendo la lunghezza della mia pixmap + 2
// e chiamo la funzione delle QT che disegna le branches passandogli il rettangolo giusto
cRect.setRight(cRect.left() - (myPix.width()+2));
QTreeView::drawBranches(p,cRect,idx);
}

}
}