for (i=0; i<8; i++) { CB[i] = new QCheckBox( this, "CB" ); CB[i]->setGeometry( QRect( 10+i*40, 15, 40, 20 ) ); CBB[i]=new QPushButton(" ",BoxChecker); CBB[i]->setFlat(true); CBB[i]->setGeometry(10+i*40, 35, 13, 13); connect(CBB[i],SIGNAL(clicked()), SLOT(slotGetColor())); }
void CheckForm::slotGetColor(){ QColor color=QColorDialog::getColor(blue,this); if(!color.isValid()) { } else // при нажатии ОК здесь должен сменится цвет кнопки ! { if(CBB[1]->isOn()) CBB[1]->setPaletteBackgroundColor(color); } }
void CheckForm::slotGetColor(){ QColor color=QColorDialog::getColor(blue,this); if(!color.isValid()) { } else // при нажатии ОК здесь должен сменится цвет кнопки ! { if ( sender() && sender()->inherits("QPushButton") ) { QPushButton* button = (QPushButton*) sender(); button->setPaletteBackgroundColor(color); } } }
// Создаем кнопкиQSignalMapper* mapper = QSignalMapper( widget );connect( mapper, SIGNAL(mapped(int)), widget, SLOT(onButtonClick(int)) );QValueVector<QPushButton*> buttonArray;for ( int i=0; i < numButtons; i++ ) { QPushButton* button = new QPushButton( "", widget ); buttonArray.append(button); //... mapper->setMapping( button, i ); connect( button, SIGNAL(clicked()), mapper, SLOT(map()) );} // Обрабатываем нажатие кнопкиvoid CheckForm::onButtonClick( int i ){ QPushButton* button = buttonArray[i]; QColor color = QColorDialog::getColor(blue,this); if(!color.isValid()) return; button->setPaletteBackgroundColor(color); //...}
QValueVector<QPushButton*> buttonArray;
mapper->setMapping( button, i );
QSignalMapper* mapper = QSignalMapper( widget );
#include <qapplication.h>#include "test.h"#include <qgroupbox.h>#include <qcheckbox.h>#include <qpushbutton.h>#include <qsettings.h>#include <qbuttongroup.h>#include <qpushbutton.h>#include <qwidget.h>#include <qpixmap.h>#include <qdialog.h>#include <qcolordialog.h>#include <qvbox.h>#include <qbutton.h>#include <qsignalmapper.h>#include <qvaluevector.h>CheckForm::CheckForm( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ){ if ( !name ) setName( tr("CheckForm") ); setCaption( trUtf8("Checker")); BoxChecker = new QGroupBox( this, "BoxChecker" ); BoxChecker->setGeometry( QRect( 0, 0, 370, 100 ) ); BoxChecker->setTitle(trUtf8("BoxChecker")); QSignalMapper* mapper = new QSignalMapper( BoxChecker ); connect( mapper, SIGNAL(mapped(int)), BoxChecker, SLOT(onButtonClick(int)) ); for (i=0; i<8; i++) { CB[i] = new QCheckBox( this, "CB" ); CB[i]->setGeometry( QRect( 10+i*40, 15, 40, 20 ) ); button=new QPushButton(" ",BoxChecker); button->setFlat(true); button->setGeometry(10+i*40, 35, 13, 13); buttonArray.append(button); mapper->setMapping( button, i ); connect( button, SIGNAL(clicked()), mapper, SLOT(map()) ); } resize( QSize(371, 50).expandedTo(minimumSizeHint()) ); readSettings();}CheckForm::~CheckForm(){ // no need to delete child widgets, Qt does it all for us}void CheckForm::writeSettings() { QSettings settings; settings.beginGroup("/CheckForm"); settings.writeEntry("/geometry/x", x()); settings.writeEntry("/geometry/y", y()); settings.endGroup(); }void CheckForm::readSettings() { QSettings settings; settings.beginGroup("/CheckForm"); int x = settings.readNumEntry("/geometry/x", 300); int y = settings.readNumEntry("/geometry/y", 300); move(x, y); settings.endGroup(); }void CheckForm::closeEvent(QCloseEvent* ce){writeSettings();ce->accept();} void CheckForm::onButtonClick( int i ){ QPushButton* button = buttonArray[i]; QColor color = QColorDialog::getColor(blue,this); if(!color.isValid()) return; else{ button->setPaletteBackgroundColor(color); } //...}