Seite 1 von 1

QProgressBar mit hintergrund/fordergrund mit RGB farben

Verfasst: 8. Juli 2007 20:57
von defenderLQ
hi

ich habe was gefunden aber ist alt.
Will ja nach situation entweder den Background oder foreground farbe
ändern.

Alos die farbe des ProgressBars ändern

ich habe schon paar test gemacht ohne erfolg.

mein Dialog ist ein QStandardItemDialog mit einem QProgressBar

beispiel versuch:

Code: Alles auswählen

/* change background */
		int i;
		switch(it->downloadStatus)
		{
			/* XXX HAND CODED! */
			case 0: /* FAILED */
				  //progress = setBackgroundColor(QColor(255, 255, 255));
				  progress = setPalette(QPalette(QColor(255, 255, 255)));
				break;
			case 1: /* OKAY */
				if (it->tfRate > 0)
				{
				  //progress = setBackgroundColor(QColor(190, 255, 255));
				  progress = setPalette(QPalette(QColor(190, 255, 255)));
				}
				else
				{
				  //progress = setBackgroundColor(QColor(200, 255, 255));
				  progress = setPalette(QPalette(QColor(200, 255, 255)));
				}
				break;
			case 2: /* COMPLETE */
			default:
				{
				  //progress = setBackgroundColor(QColor(205, 255, 255));
				  progress = setPalette(QPalette(QColor(205, 255, 255)));
				}
		}
        
habe schon http://doc.trolltech.com/4.2/qprogressbar.html
nach geschaut aber da kann ich nichts finden...

und den standard farbe des QProgressBar kann man auch nicht ändern?

Verfasst: 9. Juli 2007 10:31
von ChMaster
servus,

hab was für Dich. Ich kann nicht 100% sagen ob es das ist was du
suchst. :)

Styling QProgressBar and QScrollBar

Verfasst: 9. Juli 2007 17:01
von defenderLQ
hi

auf der seite war ich schon ...
ich will es nicht stylen mit stylesheet.

das dialog holt vom interface die daten und somit ich auch das den progress ändert
mit verschiedenen farben..

so etwa solls aussehen...
Bild


momentan siehts so aus mit plastique style:
Bild

hier ist mein DLDelegate.cpp

Code: Alles auswählen

#include <QModelIndex>
#include <QPainter>
#include <QStyleOptionProgressBarV2>
#include <QProgressBar>
#include <QApplication>

#include "DLListDelegate.h"

DLListDelegate::DLListDelegate(QObject *parent) : QAbstractItemDelegate(parent)
{
	;
}

DLListDelegate::~DLListDelegate(void)
{
	;
}

void DLListDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
	QString byteUnits[4] = {tr("B"), tr("KB"), tr("MB"), tr("GB")};
	QStyleOptionViewItem opt = option;
	QStyleOptionProgressBarV2 newopt;
	QRect pixmapRect;
	QPixmap pixmap;
	qlonglong fileSize;
	double progress, dlspeed, multi;
	int minutes, hours, days;
	qlonglong remaining;
	QString temp , status;
	qlonglong completed;

	//set text color
	QVariant value = index.data(Qt::TextColorRole);
	if(value.isValid() && qvariant_cast<QColor>(value).isValid()) {
		opt.palette.setColor(QPalette::Text, qvariant_cast<QColor>(value));
	}
	QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
	if(option.state & QStyle::State_Selected){
		painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
	} else {
		painter->setPen(opt.palette.color(cg, QPalette::Text));
	}

	// draw the background color
	if(index.column() != PROGRESS) {
		if(option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
			if(cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
				cg = QPalette::Inactive;
			}
			painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
		} else {
			value = index.data(Qt::BackgroundColorRole);
			if(value.isValid() && qvariant_cast<QColor>(value).isValid()) {
				painter->fillRect(option.rect, qvariant_cast<QColor>(value));
			}
		}
	}
	switch(index.column()) {
		case PROGRESS:
			progress = index.data().toDouble();
			temp.clear();
			temp.sprintf("%.2f", progress);
			temp += "%";
			newopt.rect = opt.rect;
			//newopt.text = temp;
			newopt.maximum = 100;
			newopt.minimum = 0;
			newopt.progress = (int)progress;
			newopt.state |= QStyle::State_Enabled;
			newopt.textVisible = false;
			QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt,
			painter);
        		//We prefer to display text manually to control color/font/boldness
			if (option.state & QStyle::State_Selected){
				opt.palette.setColor(QPalette::Text, QColor("grey"));
				painter->setPen(opt.palette.color(cg, QPalette::Text));
			}
			painter->drawText(option.rect, Qt::AlignCenter, newopt.text);		
			break;
		
	}
}

QSize DLListDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
	QVariant value = index.data(Qt::FontRole);
	QFont fnt = value.isValid() ? qvariant_cast<QFont>(value) : option.font;
	QFontMetrics fontMetrics(fnt);
	const QString text = index.data(Qt::DisplayRole).toString();
	QRect textRect = QRect(0, 0, 0, fontMetrics.lineSpacing() * (text.count(QLatin1Char('\n')) + 1));
	return textRect.size();
}
function die auf progress zugreift vom interface im TransfersDialog.cpp:

Code: Alles auswählen

void TransfersDialog::insertTransfers()
{
	QString symbol, name, sources, status;
	qlonglong fileSize, completed, remaining;
	double progress, dlspeed; 

	//remove all Items 
	for(int i = DLListModel->rowCount(); i >= 0; i--) 
	{
		delItem(i);
	}
	
	
	//nun aktuelle DownloadListe hinzufügen
	rsiface->lockData(); /* Lock Interface */

	std::list<FileTransferInfo>::const_iterator it;
	const std::list<FileTransferInfo> &transfers = rsiface->getTransferList();

	for(it = transfers.begin(); it != transfers.end(); it++) 
	{
		
		symbol  	= "";
		name    	= QString::fromStdString(it->fname);
		sources  	= QString::fromStdString(it->source);
		dlspeed  	= it->tfRate * 1024.0;
		fileSize 	= it->size;
		completed 	= it->transfered;
		progress 	= it->transfered * 100.0 / it->size;
		remaining   = (it->size - it->transfered) / (it->tfRate * 1024.0);
		
		switch(it->downloadStatus) 
		{
			/* XXX HAND CODED! */
			case 0: /* FAILED */
				status = "Failed";
				break;
			case 1: /* Downloading */
				status = "Downloading";
				break;
		    case 2: /* COMPLETE */
			default:
				status = "Complete";
				break;
		
        }
            		
		
		addItem(symbol, name, fileSize, progress, dlspeed, sources,  status, completed, remaining);
	}

	rsiface->unlockData(); /* UnLock Interface */
}

Verfasst: 9. Juli 2007 17:45
von upsala
Schreib nicht 'mein Delegate', denn wenn es dein Delegate wäre, hättest du dich gestern darin ausgekannt. Außerdem was hältst du mal von einem 'minimalen Beispiel'. Hier hat eigentlich keiner Lust sich immer duch 100erte Zeilen Code durchzuarbeiten nur um deine Probleme zu lösen.