jaegermeister hat geschrieben:Kann ich mit Pixmap auch Farben in einer ComboBox anzeigen?
Die ComboBox soll zur Auswahl von Farben dienen.
Wenn ja werd ich mir das schon suchen.
(will nur nicht umsonst suchen) ;)
Mfg jaegermeister
Ich hab sowas programmiert für meine KMyIRC Applikation. Ist zwar mit KDE aber kann man vom System her auch für QT einsetzen.
Das ganze ist fuer eine ListView geschrieben, aber Du kannst die Items der anderen Boxtypen auch so umsetzen.
Wichtig ist nur der "selfpainting" code.
KMIColorListViewItem.h
Code: Alles auswählen
/***************************************************************************
kmicolorlistviewitem.h - description
-------------------
begin : Fre Jun 28 2002
copyright : (C) 2002 by Stephan Hermann
email : sh@sourcecode.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef KMICOLORLISTVIEWITEM_H
#define KMICOLORLISTVIEWITEM_H
#include <qwidget.h>
#include <qlistview.h>
#include <qpainter.h>
#include <qiconset.h>
#include <kiconloader.h>
/**
*@author Stephan Hermann
*/
class KMIColorListViewItem : public QListViewItem {
public:
KMIColorListViewItem(QListView *parent=0,const QString &text="",const QString &color="#ffffff");
KMIColorListViewItem(QListViewItem *item=0,const QString &text="",const QString &color="#ffffff");
KMIColorListViewItem(QListViewItem *item=0,const QString &text="",const QColor &color=QColor("#000000"));
KMIColorListViewItem(QListView *parent=0,const QString &text="",const QColor &color=QColor("#000000"));
~KMIColorListViewItem();
/** No descriptions */
virtual void paintCell ( QPainter * p, const QColorGroup & cg, int column, int width, int align );
/** No descriptions */
virtual void setColor(const QColor &color);
/** No descriptions */
virtual void setColor(const QString &color);
/** No descriptions */
virtual void setText(int column,const QString &text);
/** No descriptions */
const QColor & getColor();
/** No descriptions */
const QString & getColorName();
private: // Private attributes
/** */
QColor columnColor;
};
#endif
KMIColorListViewItem.cpp
Code: Alles auswählen
/***************************************************************************
kmicolorlistviewitem.cpp - description
-------------------
begin : Fre Jun 28 2002
copyright : (C) 2002 by Stephan Hermann
email : sh@sourcecode.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kmicolorlistviewitem.h"
KMIColorListViewItem::KMIColorListViewItem(QListView *parent,const QString &text,const QString &color) : QListViewItem(parent,text) {
columnColor=QColor(color);
}
KMIColorListViewItem::KMIColorListViewItem(QListViewItem *item,const QString &text,const QString &color):QListViewItem(item,text){
columnColor=QColor(color);
}
KMIColorListViewItem::KMIColorListViewItem(QListView *parent,const QString &text,const QColor &color):QListViewItem(parent,text) {
columnColor=color;
}
KMIColorListViewItem::KMIColorListViewItem(QListViewItem *item,const QString &text,const QColor &color):QListViewItem(item,text) {
columnColor=color;
}
KMIColorListViewItem::~KMIColorListViewItem(){
}
/** No descriptions */
void KMIColorListViewItem::paintCell ( QPainter * p, const QColorGroup & cg, int column, int width, int align ){
if (column==0) {
QListViewItem::paintCell(p,cg,column,width,align);
} else if (column==1) {
if (!isSelected()) {
// Draw background to be white if it's not highlighted
p->fillRect(0,0,width,height(),QBrush(cg.base(),Qt::SolidPattern));
p->setPen(QPen(QColor("#000000"),0));
p->drawRect(3,3,width-6,height()-6);
p->fillRect ( 4, 4, width-8, height()-8, QBrush(columnColor,Qt::SolidPattern));
} else {
// Draw highlighted background
p->fillRect(0,0,width,height(),QBrush(cg.highlight(),Qt::SolidPattern));
p->setPen(QPen(QColor("#000000"),0));
p->drawRect(3,3,width-6,height()-6);
p->fillRect ( 4, 4, width-8, height()-8, QBrush(columnColor,Qt::SolidPattern));
}
} else {
QListViewItem::paintCell(p,cg,column,width,align);
}
}
/** No descriptions */
void KMIColorListViewItem::setText(int column,const QString &text){
if (column!=1) {
QListViewItem::setText(column,text);
}
}
/** No descriptions */
void KMIColorListViewItem::setColor(const QString &color){
columnColor=QColor(color);
repaint();
}
/** No descriptions */
void KMIColorListViewItem::setColor(const QColor &color){
columnColor=color;
repaint();
}
/** No descriptions */
const QString & KMIColorListViewItem::getColorName(){
return columnColor.name();
}
/** No descriptions */
const QColor & KMIColorListViewItem::getColor(){
return columnColor;
}