#include "mybutton.h" MyButton::MyButton(QWidget *parent):QPushButton(parent) { labelImage = NULL; labelImage = new QLabel(this); labelName = NULL; labelName = new QLabel(this); labelValue = NULL; labelValue = new QLabel(this); } MyButton::~MyButton() { if(labelImage != NULL) { delete labelImage; } if(labelName != NULL) { delete labelName; } if(labelValue != NULL) { delete labelValue; } } //设置顶图 void MyButton::setTopImage(QString imagePath,short int y) { setTopImage(imagePath,y,0); } void MyButton::setTopImage(QString imagePath, short y, short x) { setTopImage(imagePath,0,0,x,y); } void MyButton::setTopImage(QString imagePath, short x, short y, short width, short height) { //label加载图片( 方法1) //QPixmap pix; //pix.load(imagePath); //labelImage->setPixmap(pix); labelName->hide(); labelValue->hide(); labelImage->show(); labelImage->setGeometry(x, y, this->width()+width, this->height()+height); QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;"; labelImage->setStyleSheet(style); } void MyButton::setShortCutTopImage(QString imagePath, QPoint point) { labelName->hide(); labelValue->hide(); labelImage->show(); labelImage->setGeometry(point.x(),point.y(),this->width(),this->height()); QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;"; labelImage->setStyleSheet(style); } void MyButton::setPreviewImage(QImage image) { //label加载图片 QPixmap pix = QPixmap::fromImage(image); labelImage->setPixmap(pix); } //设置条目左侧图标 void MyButton::setItemLeftIcon(QString imagePath) { labelImage->move(0,0); QString style = "outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + ");border:0px;"; labelImage->setStyleSheet(style); } void MyButton::setLabelValueImage(QString imagePath,int scaleW, int scaleH) { if(imagePath.length() <= 0) { QPixmap npix; labelValue->setPixmap(npix); return; } QImage image; image.load(imagePath); QPixmap pix = QPixmap::fromImage(image); if(scaleW != 0 && scaleH != 0) { QPixmap pix1 = pix.scaled(scaleW,scaleH); labelValue->setPixmap(pix1); } else { labelValue->setPixmap(pix); } /* labelValue->setAlignment(Qt::AlignLeft); labelValue->setAlignment(Qt::AlignBottom); QString style = "background-color: rgba(255, 255, 255, 0);border:0px;image: url(" + imagePath + ");"; labelValue->setStyleSheet(style); */ } //设置底层图片(按下、抬起、失效) void MyButton::setBottomImage(QString imagePath) { //第一种加载图片的方法 // QPixmap pix; // pix.load(imageRelPath); // int w = this->width(); // int h = this->height(); // this->setIconSize(QSize(w,h)); // this->setIcon(pix); //第二种加载图片的方法 QString style = "QPushButton{outline: none;background-color: rgba(255, 255, 255, 0);border-image: url(" + imagePath + "_p0.png" + ");border:0px;}"; style += "QPushButton:pressed{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}"; style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}"; style += "QPushButton:checked{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}"; //style += "QPushButton:disabled{background-color: rgba(220, 220, 220, 50);}"; this->setStyleSheet(style); } #if(0) //设置控制动作界面底层图片(按下、抬起),并设置文字前景色 void MyButton::setControlWidgetBtnBottomImage(QString imagePath, QString color) { QString style = "QPushButton{outline: none;"+ color + "border-image: url(" + imagePath + "_p0.png" + ");border:0px;}"; style += "QPushButton:pressed{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}"; style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}"; //style += "QPushButton:disabled{background-color: rgba(220, 220, 220, 50);}"; this->setStyleSheet(style); } #endif //设置主按钮底层图片 void MyButton::setCheckedBottomImage(QString imagePath) { //第二种加载图片的方法 QString style = "QPushButton{outline: none;border-image: url(" + imagePath + "_p0.png" + ");border:0px;}"; style += "QPushButton:disabled{outline: none;border-image: url(" + imagePath + "_p2.png" + ");}"; style += "QPushButton:checked{outline: none;border-image: url(" + imagePath + "_p1.png" + ");}"; this->setStyleSheet(style); } //设置按钮logo(按钮是没有点击事件的) void MyButton::setUnPreBtnLogo(QString tStyle) { labelName->hide(); labelValue->hide(); labelImage->hide(); if(tStyle.length() <= 0) { return; } this->setStyleSheet(titleIconStyle(tStyle)); } //初始化label条目名称和条目值(参数设置) void MyButton::initLeftNameRightValue(QRect rectName, QRect rectValue, int right) { labelImage->hide(); QString style = "QLabel {outline: none;background-color: rgba(255, 255, 255,0);color: black;}" "QLabel:disabled {color: rgb(80, 80, 80);}";//失效时字体颜色 labelName->setGeometry(rectName); labelName->setStyleSheet(commonTextColour()); labelName->setAlignment(Qt::AlignLeft); labelName->setAlignment(Qt::AlignBottom); labelValue->setGeometry(rectValue); labelValue->setStyleSheet(commonTextColour()); if(right == 0) { labelValue->setAlignment(Qt::AlignLeft | Qt::AlignBottom); } else { labelValue->setAlignment(Qt::AlignRight | Qt::AlignBottom); } QString style1 = "QPushButton{outline: none;border:0px;border-radius:0px;background-color:rgba(255, 255, 255, 0);}"; style1 += "QPushButton:pressed{outline: none;background-color: rgba(255, 255, 255, 50);}"; this->setStyleSheet(style1); } //初始化label图标显示和条目名称(文件导入) void MyButton::initLeftImageRightName(QRect rectImage, QRect rectName) { labelValue->hide(); labelImage->show(); labelImage->setGeometry(rectImage); labelImage->setAlignment(Qt::AlignHCenter); labelImage->setAlignment(Qt::AlignBottom); labelName->setGeometry(rectName); labelName->setStyleSheet(commonTextColour()); labelName->setAlignment(Qt::AlignLeft); labelName->setAlignment(Qt::AlignBottom); QString style1 = "QPushButton{outline: none;border:0px;border-radius:0px;background-color:rgba(255, 255, 255, 0);}"; style1 += "QPushButton:checked{outline: none;background-color: rgba(255, 255, 255, 50);}"; this->setStyleSheet(style1); } void MyButton::initUpImageDownName(QRect rectImage, QRect rectName) { labelValue->hide(); labelImage->show(); labelImage->setGeometry(rectImage); labelImage->setAlignment(Qt::AlignCenter); labelImage->setStyleSheet("outline: none;background-color: rgba(255, 255, 255, 0);"); labelName->setGeometry(rectName); labelName->setStyleSheet(commonTextColour()); labelName->setAlignment(Qt::AlignTop); labelName->setAlignment(Qt::AlignHCenter); // labelName->setWordWrap(true); } void MyButton::initLabelValue(QRect rectValue) { labelValue->setGeometry(rectValue); labelValue->setAlignment(Qt::AlignCenter); } //设置label字体 void MyButton::setLabelFont(QFont font) { labelName->setFont(font); labelValue->setFont(font); //labelValue->show(); } void MyButton::setLabelNameFont(QFont font) { labelName->setFont(font); } void MyButton::setLabelValueFont(QFont font) { labelValue->setFont(font); } //设置条目名称 void MyButton::setLabelName(QString name) { labelName->setText(name); labelName->show(); } void MyButton::setLabelNameStyle(QString style) { labelName->setStyleSheet(style); } //设置条目值 void MyButton::setLabelValue(QString value) { labelValue->setText(value); labelValue->show(); } void MyButton::setLabelValueStyle(QString style) { labelValue->setStyleSheet(style); } void MyButton::setLabelImage(QString style) { labelImage->setStyleSheet(style); } void MyButton::setMainBtnBottomStyle() { QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(240, 245, 249), stop:1 rgb(215, 243, 254));border-radius:"+borderRadius()+"px;}"; style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(234, 234, 234), stop:1 rgb(176,176,176));}"; style += "QPushButton:checked{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; this->setStyleSheet(style); } void MyButton::setDirectionButtonStyle() { QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(240, 245, 249), stop:1 rgb(204, 234, 244));border-radius:"+borderRadius()+"px;}"; style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(234, 234, 234), stop:1 rgb(176,176,176));}"; style += "QPushButton:checked{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; this->setStyleSheet(style); } void MyButton::setRevDirectionButtonStyle() { QString style = "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(240, 245, 249), stop:1 rgb(204, 234, 244));border-radius:"+borderRadius()+"px;}"; style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(234, 234, 234), stop:1 rgb(176,176,176));}"; style += "QPushButton:checked{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(186, 226, 227), stop:1 rgb(105,198,194));}"; this->setStyleSheet(style); }