UPushButtonSelectColor *pButtonSelectColor = new UPushButtonSelectColor("Get color!");connect(UPushButtonSelectColor, SIGNAL(selectedColor(QColor)), [ваш_объект], [слот_или_сигнал_вашего_объекта]);
#ifndef UPUSHBUTTONSELECTCOLOR_H#define UPUSHBUTTONSELECTCOLOR_H#include <QPushButton>#include <QMenu>#include <QAction>#include <QColorDialog>class UPushButtonSelectColor: public QPushButton{ Q_OBJECTpublic: UPushButtonSelectColor(QWidget *parent = 0); UPushButtonSelectColor(const QString &text, QWidget *parent = 0); UPushButtonSelectColor(const QIcon &icon, const QString &text, QWidget *parent = 0); QMenu * menuColors(); QList <QAction *> actionsColors();private: void init(); void createActionsSelectColor(); void setLanguage();private: QMenu *popurMenuTextColor; QAction *actionTextColor_White; QAction *actionTextColor_Black; QAction *actionTextColor_Red; QAction *actionTextColor_DarkRed; QAction *actionTextColor_Green; QAction *actionTextColor_DarkGreen; QAction *actionTextColor_Blue; QAction *actionTextColor_DarkBlue; QAction *actionTextColor_Cyan; QAction *actionTextColor_DarkCyan; QAction *actionTextColor_Magenta; QAction *actionTextColor_DarkMagenta; QAction *actionTextColor_Yellow; QAction *actionTextColor_DarkYellow; QAction *actionTextColor_Gray; QAction *actionTextColor_DarkGray; QAction *actionTextColor_LightGray; QAction *actionTextColor_Other; QColor color;public slots: void black(); void white(); void gray(); void red(); void green(); void cyan(); void blue(); void magenta(); void yellow(); void darkGray(); void darkRed(); void darkGreen(); void darkCyan(); void darkBlue(); void darkMagenta(); void darkYellow(); void lightGray(); void colorDialog(); void setColor(QColor c);private slots: void showMenu();signals: void selectedColor(QColor);};#endif // UPUSHBUTTONSELECTCOLOR_H
#include "UPushButtonSelectColor.h"UPushButtonSelectColor::UPushButtonSelectColor(QWidget *parent): QPushButton(parent){ init();}UPushButtonSelectColor::UPushButtonSelectColor(const QString &text, QWidget *parent): QPushButton(text, parent){ init();}UPushButtonSelectColor::UPushButtonSelectColor(const QIcon &icon, const QString &text, QWidget *parent): QPushButton(icon, text, parent){ init();}QMenu * UPushButtonSelectColor::menuColors(){ return popurMenuTextColor;}QList <QAction *> UPushButtonSelectColor::actionsColors(){ return popurMenuTextColor->actions();}// PRIVATEvoid UPushButtonSelectColor::init(){ createActionsSelectColor(); setLanguage(); connect(this, SIGNAL(clicked()), this, SLOT(showMenu()));}void UPushButtonSelectColor::createActionsSelectColor(){ actionTextColor_White = new QAction(this); actionTextColor_Black = new QAction(this); actionTextColor_Red = new QAction(this); actionTextColor_DarkRed = new QAction(this); actionTextColor_Green = new QAction(this); actionTextColor_DarkGreen = new QAction(this); actionTextColor_Blue = new QAction(this); actionTextColor_DarkBlue = new QAction(this); actionTextColor_Cyan = new QAction(this); actionTextColor_DarkCyan = new QAction(this); actionTextColor_Magenta = new QAction(this); actionTextColor_DarkMagenta = new QAction(this); actionTextColor_Yellow = new QAction(this); actionTextColor_DarkYellow = new QAction(this); actionTextColor_Gray = new QAction(this); actionTextColor_DarkGray = new QAction(this); actionTextColor_LightGray = new QAction(this); actionTextColor_Other = new QAction(this); QPixmap pixmap(10, 10); pixmap.fill(Qt::white); actionTextColor_White->setIcon(pixmap); pixmap.fill(Qt::black); actionTextColor_Black->setIcon(pixmap); pixmap.fill(Qt::red); actionTextColor_Red->setIcon(pixmap); pixmap.fill(Qt::darkRed); actionTextColor_DarkRed->setIcon(pixmap); pixmap.fill(Qt::green); actionTextColor_Green->setIcon(pixmap); pixmap.fill(Qt::darkGreen); actionTextColor_DarkGreen->setIcon(pixmap); pixmap.fill(Qt::blue); actionTextColor_Blue->setIcon(pixmap); pixmap.fill(Qt::darkBlue); actionTextColor_DarkBlue->setIcon(pixmap); pixmap.fill(Qt::cyan); actionTextColor_Cyan->setIcon(pixmap); pixmap.fill(Qt::darkCyan); actionTextColor_DarkCyan->setIcon(pixmap); pixmap.fill(Qt::magenta); actionTextColor_Magenta->setIcon(pixmap); pixmap.fill(Qt::darkMagenta); actionTextColor_DarkMagenta->setIcon(pixmap); pixmap.fill(Qt::yellow); actionTextColor_Yellow->setIcon(pixmap); pixmap.fill(Qt::darkYellow); actionTextColor_DarkYellow->setIcon(pixmap); pixmap.fill(Qt::gray); actionTextColor_Gray->setIcon(pixmap); pixmap.fill(Qt::darkGray); actionTextColor_DarkGray->setIcon(pixmap); pixmap.fill(Qt::lightGray); actionTextColor_LightGray->setIcon(pixmap); connect(actionTextColor_White, SIGNAL(triggered()), this, SLOT(white())); connect(actionTextColor_Black, SIGNAL(triggered()), this, SLOT(black())); connect(actionTextColor_Red, SIGNAL(triggered()), this, SLOT(red())); connect(actionTextColor_DarkRed, SIGNAL(triggered()), this, SLOT(darkRed())); connect(actionTextColor_Green, SIGNAL(triggered()), this, SLOT(green())); connect(actionTextColor_DarkGreen, SIGNAL(triggered()), this, SLOT(darkGreen())); connect(actionTextColor_Blue, SIGNAL(triggered()), this, SLOT(blue())); connect(actionTextColor_DarkBlue, SIGNAL(triggered()), this, SLOT(darkBlue())); connect(actionTextColor_Cyan, SIGNAL(triggered()), this, SLOT(cyan())); connect(actionTextColor_DarkCyan, SIGNAL(triggered()), this, SLOT(darkCyan())); connect(actionTextColor_Magenta, SIGNAL(triggered()), this, SLOT(magenta())); connect(actionTextColor_DarkMagenta, SIGNAL(triggered()), this, SLOT(darkMagenta())); connect(actionTextColor_Yellow, SIGNAL(triggered()), this, SLOT(yellow())); connect(actionTextColor_DarkYellow, SIGNAL(triggered()), this, SLOT(darkYellow())); connect(actionTextColor_Gray, SIGNAL(triggered()), this, SLOT(gray())); connect(actionTextColor_DarkGray, SIGNAL(triggered()), this, SLOT(darkGray())); connect(actionTextColor_LightGray, SIGNAL(triggered()), this, SLOT(lightGray())); connect(actionTextColor_Other, SIGNAL(triggered()), this, SLOT(colorDialog())); popurMenuTextColor = new QMenu(); popurMenuTextColor->addAction(actionTextColor_White); popurMenuTextColor->addAction(actionTextColor_Black); popurMenuTextColor->addAction(actionTextColor_Red); popurMenuTextColor->addAction(actionTextColor_DarkRed); popurMenuTextColor->addAction(actionTextColor_Green); popurMenuTextColor->addAction(actionTextColor_DarkGreen); popurMenuTextColor->addAction(actionTextColor_Blue); popurMenuTextColor->addAction(actionTextColor_DarkBlue); popurMenuTextColor->addAction(actionTextColor_Cyan); popurMenuTextColor->addAction(actionTextColor_DarkCyan); popurMenuTextColor->addAction(actionTextColor_Magenta); popurMenuTextColor->addAction(actionTextColor_DarkMagenta); popurMenuTextColor->addAction(actionTextColor_Yellow); popurMenuTextColor->addAction(actionTextColor_DarkYellow); popurMenuTextColor->addAction(actionTextColor_Gray); popurMenuTextColor->addAction(actionTextColor_DarkGray); popurMenuTextColor->addAction(actionTextColor_LightGray); popurMenuTextColor->addAction(actionTextColor_Other);}void UPushButtonSelectColor::setLanguage(){ actionTextColor_White->setText("White")); actionTextColor_Black->setText("Black"); actionTextColor_Red->setText("Red"); actionTextColor_DarkRed->setText("Dark red"); actionTextColor_Green->setText("Green"); actionTextColor_DarkGreen->setText("Dark green"); actionTextColor_Blue->setText("Blue"); actionTextColor_DarkBlue->setText("Dark blue"); actionTextColor_Cyan->setText("Cyan"); actionTextColor_DarkCyan->setText("Dark cyan"); actionTextColor_Magenta->setText("Magenta"); actionTextColor_DarkMagenta->setText("Dark magenta"); actionTextColor_Yellow->setText("Yellow"); actionTextColor_DarkYellow->setText("Dark yellow"); actionTextColor_Gray->setText("Gray"); actionTextColor_DarkGray->setText("Dark gray"); actionTextColor_LightGray->setText("Light gray"); actionTextColor_Other->setText("Other");}// PUBLIC SLOTSvoid UPushButtonSelectColor::black(){ color = Qt::black; emit selectedColor(color);}void UPushButtonSelectColor::white(){ color = Qt::white; emit selectedColor(color);}void UPushButtonSelectColor::gray(){ color = Qt::gray; emit selectedColor(color);}void UPushButtonSelectColor::red(){ color = Qt::red; emit selectedColor(color);}void UPushButtonSelectColor::green(){ color = Qt::green; emit selectedColor(color);}void UPushButtonSelectColor::cyan(){ color = Qt::cyan; emit selectedColor(color);}void UPushButtonSelectColor::blue(){ color = Qt::blue; emit selectedColor(color);}void UPushButtonSelectColor::magenta(){ color = Qt::magenta; emit selectedColor(color);}void UPushButtonSelectColor::yellow(){ color = Qt::yellow; emit selectedColor(color);}void UPushButtonSelectColor::darkGray(){ color = Qt::darkGray; emit selectedColor(color);}void UPushButtonSelectColor::darkRed(){ color = Qt::darkRed; emit selectedColor(color);}void UPushButtonSelectColor::darkGreen(){ color = Qt::darkGreen; emit selectedColor(color);}void UPushButtonSelectColor::darkCyan(){ color = Qt::darkCyan; emit selectedColor(color);}void UPushButtonSelectColor::darkBlue(){ color = Qt::darkBlue; emit selectedColor(color);}void UPushButtonSelectColor::darkMagenta(){ color = Qt::darkMagenta; emit selectedColor(color);}void UPushButtonSelectColor::darkYellow(){ color = Qt::darkYellow; emit selectedColor(color);}void UPushButtonSelectColor::lightGray(){ color = Qt::lightGray; emit selectedColor(color);}void UPushButtonSelectColor::colorDialog(){ QColor col = QColorDialog::getColor(color); if(!col.isValid()) return; color = col; emit selectedColor(color);}void UPushButtonSelectColor::setColor(QColor c){ color = c;}// PRIVATE SLOTSvoid UPushButtonSelectColor::showMenu(){ popurMenuTextColor->exec(mapToGlobal(QPoint(frameSize().width(), 0)));}
#ifndef UPUSHBUTTONSELECTCOLOR_V2_H#define UPUSHBUTTONSELECTCOLOR_V2_H#include <QWidget>#include <QPushButton>#include <QAction>#include <QMenu>#include <QSignalMapper>#include <QMap>#include <QMapIterator>#include <QPainter>#include <QDebug>class UPushButtonSelectColor_v2: public QPushButton{ Q_OBJECT public: UPushButtonSelectColor_v2(QWidget *parent = 0);private: QMenu *menu; QSignalMapper *signalMapper;private slots: void showMenu(); void clickedAction(int id);signals: void selectedColor(QColor);};class foo: public QWidget{ Q_OBJECTprivate: QColor color;public slots: void setColor(QColor _color) { color = _color; repaint(); }protected: void paintEvent(QPaintEvent *) { QPainter painter(this); painter.setBrush(color); painter.setPen(color); painter.drawEllipse(0, 0, width(), height()); }};#endif // UPUSHBUTTONSELECTCOLOR_V2_H
#include "UPushButtonSelectColor_v2.h"UPushButtonSelectColor_v2::UPushButtonSelectColor_v2(QWidget *parent): QPushButton(parent){ connect(this, SIGNAL(clicked()), this, SLOT(showMenu())); menu = new QMenu(); QMap <QString, QColor> map; map.insert("White", Qt::white); map.insert("Black", Qt::black); map.insert("Red", Qt::red); map.insert("Green", Qt::green); map.insert("Yellow", Qt::yellow); map.insert("Blue", QColor(0, 0, 255)); signalMapper = new QSignalMapper(this); QList <QAction *> actions; int count = 0; QMapIterator <QString, QColor> i(map); while(i.hasNext()) { i.next(); QColor color = i.value(); QPixmap pixmap(10, 10); pixmap.fill(color); QAction *action = new QAction(pixmap, i.key(), this); action->setData(color); actions.append(action); connect(action, SIGNAL(triggered()), signalMapper, SLOT(map())); signalMapper->setMapping(action, count); count++; } menu->addActions(actions); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(clickedAction(int)));}// PRIVATE SLOTSvoid UPushButtonSelectColor_v2::showMenu(){ menu->exec(mapToGlobal(QPoint(width(), 0)));}void UPushButtonSelectColor_v2::clickedAction(int id){ emit selectedColor(menu->actions().at(id)->data().value <QColor> ());}
#include <QApplication>#include "UPushButtonSelectColor_v2.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); UPushButtonSelectColor_v2 *p = new UPushButtonSelectColor_v2(); p->show(); p->setText("Click Me!"); foo *f = new foo(); f->setColor(Qt::green); f->show(); QObject::connect(p, SIGNAL(selectedColor(QColor)), f, SLOT(setColor(QColor))); return a.exec();}
C++ (Qt)actionTextColor_White = new QAction(this); actionTextColor_Black = new QAction(this); actionTextColor_Red = new QAction(this); actionTextColor_DarkRed = new QAction(this); actionTextColor_Green = new QAction(this); actionTextColor_DarkGreen = new QAction(this); actionTextColor_Blue = new QAction(this); actionTextColor_DarkBlue = new QAction(this); actionTextColor_Cyan = new QAction(this); actionTextColor_DarkCyan = new QAction(this); actionTextColor_Magenta = new QAction(this); actionTextColor_DarkMagenta = new QAction(this); actionTextColor_Yellow = new QAction(this); actionTextColor_DarkYellow = new QAction(this); actionTextColor_Gray = new QAction(this); actionTextColor_DarkGray = new QAction(this); actionTextColor_LightGray = new QAction(this); actionTextColor_Other = new QAction(this); QPixmap pixmap(10, 10); pixmap.fill(Qt::white); actionTextColor_White->setIcon(pixmap); pixmap.fill(Qt::black); actionTextColor_Black->setIcon(pixmap); pixmap.fill(Qt::red); actionTextColor_Red->setIcon(pixmap); pixmap.fill(Qt::darkRed); actionTextColor_DarkRed->setIcon(pixmap); pixmap.fill(Qt::green); actionTextColor_Green->setIcon(pixmap); pixmap.fill(Qt::darkGreen); actionTextColor_DarkGreen->setIcon(pixmap); pixmap.fill(Qt::blue); actionTextColor_Blue->setIcon(pixmap); pixmap.fill(Qt::darkBlue); actionTextColor_DarkBlue->setIcon(pixmap); pixmap.fill(Qt::cyan); actionTextColor_Cyan->setIcon(pixmap); pixmap.fill(Qt::darkCyan); actionTextColor_DarkCyan->setIcon(pixmap); pixmap.fill(Qt::magenta); actionTextColor_Magenta->setIcon(pixmap); pixmap.fill(Qt::darkMagenta); actionTextColor_DarkMagenta->setIcon(pixmap); pixmap.fill(Qt::yellow); actionTextColor_Yellow->setIcon(pixmap); pixmap.fill(Qt::darkYellow); actionTextColor_DarkYellow->setIcon(pixmap); pixmap.fill(Qt::gray); actionTextColor_Gray->setIcon(pixmap); pixmap.fill(Qt::darkGray); actionTextColor_DarkGray->setIcon(pixmap); pixmap.fill(Qt::lightGray); actionTextColor_LightGray->setIcon(pixmap);
QMenu *menuColor = new QMenu();QPixmap pixmap(10, 10);for(int i = Qt::black; i < Qt::darkYellow; i++){ QAction *actionColor = new QAction(this); pixmap.fill(i); actionColor->setIcon(pixmap); menu->addAction(actionColor);}
C++ (Qt)#include <QToolButton>#include <QMenu>#include <QTableView>#include <QHeaderView>#include <QStandardItemModel>#include <QWidgetAction>#include <QPainter>#include <QColorDialog> class QUColorSelectButton: public QToolButton{ Q_OBJECTpublic: QUColorSelectButton( const QString& text, QWidget* parent = 0 ); QColor color() const; void setColor( const QColor& clr ); Q_SIGNALS: void colorChanged( const QColor& ); protected Q_SLOTS: void selectOtherColor(); void selectItemColor( const QModelIndex& idx ); private: QColor color_; QPixmap icon_pix_; static const int cell_width_ = 16;}; QUColorSelectButton::QUColorSelectButton( const QString& text, QWidget* parent ): QToolButton( parent ){ if( !text.isEmpty() ) { setToolButtonStyle( Qt::ToolButtonTextBesideIcon ); setText( text ); } icon_pix_ = QPixmap( minimumSize().expandedTo( QSize( 22, 22 ) ) - QSize( 8, 8 ) ); setColor( color_ ); setPopupMode( InstantPopup ); QMenu* menu = new QMenu(); setMenu( menu ); QTableView* v = new QTableView( menu ); QStandardItemModel* m = new QStandardItemModel( this ); m->setColumnCount( 8 ); m->setRowCount( 6 ); int row = 0, col = 0; for( int g = 0; g < 4; g++ ) for( int r = 0; r < 4; r++ ) for( int b = 0; b < 3; b++ ) { QColor clr( r * 255 / 3, g * 255 / 3, b * 255 / 2 ); m->setData( m->index( row, col ), QVariant( clr ), Qt::BackgroundRole ); if( row == 5 ) row = 0, col++; else row++; } v->setModel( m ); for( int i = 0; i < 8; i++ ) v->setColumnWidth( i, cell_width_ ); for( int i = 0; i < 6; i++ ) v->setRowHeight( i, cell_width_ ); v->setSelectionMode( QAbstractItemView::NoSelection ); v->setEditTriggers( QAbstractItemView::NoEditTriggers ); v->setFrameStyle( QFrame::NoFrame ); v->horizontalHeader()->hide(); v->verticalHeader()->hide(); v->setFixedSize( cell_width_ * 8, cell_width_ * 6 ); connect( v, SIGNAL( clicked(QModelIndex) ), this, SLOT( selectItemColor(QModelIndex) ) ); connect( v, SIGNAL( activated(QModelIndex) ), this, SLOT( selectItemColor(QModelIndex) ) ); QWidgetAction* wa = new QWidgetAction( menu ); wa->setDefaultWidget( v ); menu->addAction( wa ); wa = new QWidgetAction( menu ); QToolButton* tb = new QToolButton( menu ); tb->setText( tr( "Other Color..." ) ); tb->setAutoRaise( true ); connect( tb, SIGNAL( clicked() ), this, SLOT( selectOtherColor() ) ); wa->setDefaultWidget( tb ); menu->addAction( wa );} void QUColorSelectButton::selectOtherColor(){ QColor clr = QColorDialog::getColor( color_ ); if( clr.isValid() ) { setColor( clr ); emit colorChanged( clr ); }} void QUColorSelectButton::selectItemColor( const QModelIndex& idx ){ QColor clr = idx.data( Qt::BackgroundRole ).value< QColor >(); setColor( clr ); emit colorChanged( clr ); menu()->hide();} QColor QUColorSelectButton::color() const{ return color_;} void QUColorSelectButton::setColor( const QColor& clr ){ color_ = clr; QPainter p( &icon_pix_ ); p.setBrush( QBrush( clr ) ); p.setRenderHint( QPainter::Antialiasing ); p.drawRect( icon_pix_.rect() ); setIcon( QIcon( icon_pix_ ) );}