#include "colorcombobox.h"
#define TABLEROW 7
#define TABLECOLUMN 4
colorcombobox::colorcombobox(QWidget *parent)
:QComboBox(parent)
{
ColorId = 0;
currentRow = 0;
currentColumn = 0;
currentColor = nullptr;
showColor = new QLabel(this);
showColor->setParent(this);
showColor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
tablewidget = new QTableWidget(TABLEROW,TABLECOLUMN);
tablewidget->verticalHeader()->setVisible(false);
tablewidget->horizontalHeader()->setVisible(false);
tablewidget->setShowGrid(false);
tablewidget->setSpan(0,0,1,TABLECOLUMN);
tablewidget->setSpan(TABLEROW-3,0,1,TABLECOLUMN);
tablewidget->setSpan(TABLEROW-2,0,1,TABLECOLUMN);
RepaintList<<"red";
RepaintList<<"yellow";
RepaintList<<"green";
RepaintList<<"aliceblue";
RepaintList<<"antiquewhite";
RepaintList<<"aqua";
RepaintList<<"aquamarine";
RepaintList<<"azure";
RepaintList<<"beige";
RepaintList<<"bisque";
RepaintList<<"blue";
RepaintList<<"brown";
RepaintList<<"burlywood";
RepaintList<<"cadetblue";
RepaintList<<"coral";
RepaintList<<"cornsilk";
int k =0;
for (int i =0;i<TABLEROW;i++) {
if (i==TABLEROW-3)
{
other = new QPushButton(this);
other->setText(tr("other color"));
other->setObjectName("PushButton00");
tablewidget->setCellWidget(TABLEROW-3,0,other);
}
else if(i == TABLEROW-2)
{
labeltext = new QLabel(this);
labeltext->setText(tr("Recently used colors"));
tablewidget->setCellWidget(TABLEROW-2,0,labeltext);
labeltext->setStyleSheet("background-color:white");
}
else if(i == 0)
{
QLabel * labelshow = new QLabel;
labelshow->setText(tr("Standard Color"));
tablewidget->setCellWidget(0,0,labelshow);
labelshow->setStyleSheet("background-color:white");
}
else if(i == TABLEROW-1)
{
btn0 = new MyPushButton;
btn1 = new MyPushButton;
btn2 = new MyPushButton;
btn3 = new MyPushButton;
tablewidget->setCellWidget(i,0,btn0);
tablewidget->setCellWidget(i,1,btn1);
tablewidget->setCellWidget(i,2,btn2);
tablewidget->setCellWidget(i,3,btn3);
connect(btn0,SIGNAL(clicked(QString)),this,SLOT(change(QString)));
connect(btn1,SIGNAL(clicked(QString)),this,SLOT(change(QString)));
connect(btn2,SIGNAL(clicked(QString)),this,SLOT(change(QString)));
connect(btn3,SIGNAL(clicked(QString)),this,SLOT(change(QString)));
}
else
{
for (int j=0;j<TABLECOLUMN;j++) {
tablewidget->setColumnWidth(j,tablewidget->geometry().width()/TABLECOLUMN);
MyPushButton * itemp = new MyPushButton;
itemp->setObjectName("PushButton"+QString::number(i)+QString::number(j));
if(i<TABLEROW)
itemp->setStyleSheet("background-color:"+RepaintList[k]);
k++;
tablewidget->setCellWidget(i,j,itemp);
connect(itemp,SIGNAL(clicked(QString)),this,SLOT(change(QString)));
}
}
}
connect(other,SIGNAL(clicked()),this,SLOT(otherColor()));
this->setModel(tablewidget->model());
this->setView(tablewidget);
}
colorcombobox::~colorcombobox(){
}
void colorcombobox::change(QString colorname)
{
QPushButton * btn = (QPushButton *)sender();
currentRow = btn->objectName().mid(10,1).toInt();
currentColumn = btn->objectName().mid(11,1).toInt();
currentColor = QColor(colorname);
//update btn's color in the last row
//the next get the the last's color
if(btn0->palette().color(QPalette::Background).name()!=colorname&&
btn1->palette().color(QPalette::Background).name()!=colorname&&
btn2->palette().color(QPalette::Background).name()!=colorname&&
btn3->palette().color(QPalette::Background).name()!=colorname)
{
btn3->setStyleSheet("background-color:"+btn2->palette().color(QPalette::Background).name());
btn2->setStyleSheet("background-color:"+btn1->palette().color(QPalette::Background).name());
btn1->setStyleSheet("background-color:"+btn0->palette().color(QPalette::Background).name());
btn0->setStyleSheet("background-color:"+colorname);
}
qDebug()<<"color"<<btn->objectName()<<currentRow<<currentColumn<<currentColor.name();
showColor->setStyleSheet("QLabel{background-color:"+colorname+";}");
}
void colorcombobox::otherColor()
{
qDebug()<<"othercolor";
QColor colorOrg = QColor();
QColor mycolor = QColorDialog::getColor(colorOrg,this);
other->setStyleSheet("background-color:"+mycolor.name());
this->change(mycolor.name());
}
void colorcombobox::sendColor()
{
}
void colorcombobox::setCurrentIndex(int i)
{
//update current params
if(i<RepaintList.size())
setCurrentColor(QColor(RepaintList[i]));
}
void colorcombobox::setCurrentColor(QColor color)
{
//update current params
currentRow = 0;
currentColumn = 0;
currentColor = color;
if(RepaintList.contains(color.name()))
{
currentRow = RepaintList.indexOf(color.name())/4;
currentColumn = RepaintList.indexOf(color.name())%4;
}
else
{
other->setStyleSheet("background-color:"+color.name());
}
showColor->setStyleSheet("QLabel{background-color:"+color.name()+";}");
btn0->setStyleSheet("background-color:"+color.name());
}
void colorcombobox::resizeEvent(QResizeEvent *e)
{
QRect rect = this->geometry();
rect.setX(0);
rect.setY(0);
qDebug()<<rect.width()<<rect.height();
showColor->setGeometry(1,1,rect.width()-2,rect.height()-2);
for (int var = 0; var < TABLECOLUMN; ++var) {
tablewidget->setColumnWidth(var,(rect.width()-10)/(TABLECOLUMN));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
ColorComboBox.rar (12个子文件)
CompilerDefine.pro 327B
DemoColorCombox
mainwindow.cpp 389B
mainwindow.h 344B
main.cpp 183B
mainwindow.ui 1KB
DemoColorCombox.pro 1KB
ColorComboBox
mypushbutton.h 653B
colorcombobox.h 1KB
ColorComboBox_global.h 276B
colorcombobox.cpp 6KB
mypushbutton.cpp 722B
ColorComboBox.pro 1KB
共 12 条
- 1
风木菌
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0