一、最近学习Qt的时候正在做一个音乐播放器,做的差不多了。但是关闭按钮咋看咋难看。
这个关闭按钮还是从一位前辈的Qt实现360新功能的资源里拷过来的,但是在这上面看着很不好看啊。
所以,在网上搜了点按钮贴图。找了好久,看到了不错的。但是,,,,所以的小按钮在一张jpg上面,于是搬出PS,由于资质愚笨,弄了好久也没有抠出按钮图片,于是想试试编程实现。
二、思路
首先观察这张图上面的按钮布局还是挺规矩的。所以就想着把这个图片贴到窗体上,然后通过增加横线和竖线来确定每个按钮的方块区域。最后保存下来。
picwidget.h 用于显示整张图片,并固定大小的显示在主窗体上。
#ifndef PICWIDGET_H #define PICWIDGET_H #include <QWidget> #include <QList> #include <QPixmap> class picWidget : public QWidget { Q_OBJECT public: explicit picWidget(QWidget *parent = 0); signals: public slots: void saveHline();//保存横线 void saveVline();//保存竖线 void changeHline(int);//横线变化 重绘 void changeVline(int);//竖线变化 重绘 void makeIcon();//线已画好,生成小图片 private: QList<int> hlineList;//保存横线 QList<int> vlineList;//保存竖线 int hLine; int vLine; QPixmap pixmap;//加载整张图片 protected: void paintEvent(QPaintEvent *); }; #endif // PICWIDGET_H
#include "picwidget.h" #include <QPalette> #include <QPainter> #include <iterator> #include <QtAlgorithms> picWidget::picWidget(QWidget *parent) : QWidget(parent) { pixmap.load(":/source.jpg"); QPalette palette; palette.setBrush(QPalette::Window,QBrush(pixmap)); setPalette(palette); setFixedSize(pixmap.size()); setAutoFillBackground(true); hLine=vLine=-1; } void picWidget::changeHline(int h) { hLine=h; repaint(); } void picWidget::changeVline(int v) { vLine=v; repaint(); } void picWidget::saveHline() { hlineList.append(hLine); hLine=-1; repaint(); } void picWidget::saveVline() { vlineList.append(vLine); vLine=-1; repaint(); } void picWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setBrush(Qt::NoBrush); painter.setPen(Qt::blue); QList<int>::iterator hiter=hlineList.begin(); while(hiter!=hlineList.end()) { painter.drawLine(0,*hiter,pixmap.width()-1,*hiter); hiter++; } QList<int>::iterator viter=vlineList.begin(); while(viter!=vlineList.end()) { painter.drawLine(*viter,0,*viter,pixmap.height()-1); viter++; } if(vLine!=-1 ||hLine!=-1) { painter.setPen(QPen(Qt::DotLine)); if(vLine!=-1) painter.drawLine(vLine,0,vLine,pixmap.height()-1); if(hLine!=-1) painter.drawLine(0,hLine,pixmap.width()-1,hLine); } } void picWidget::makeIcon() { int no=0; qSort(hlineList.begin(),hlineList.end()); qSort(vlineList.begin(),vlineList.end()); QList<int>::iterator hiter=hlineList.begin(); while(hiter!=hlineList.end()) { int top=*hiter++; int bottom=*hiter++; QList<int>::iterator viter=vlineList.begin(); while(viter!=vlineList.end()) { int left=*viter++; int right=*viter++; pixmap.copy(left,top,right-left+1,bottom-top+1).save("C:\\Users\\Administrator\\Desktop\\mypng\\"+QString::number(no++)+".png"); } } hlineList.clear(); vlineList.clear(); repaint(); }
#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> class picWidget; class QPushButton; class QSlider; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: picWidget *pic; QSlider *sliderHline; QPushButton *saveHline; QSlider *sliderVline; QPushButton *saveVline; QPushButton *makeIcon; }; #endif // WIDGET_H
#include "widget.h" #include "picwidget.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QSlider> #include <QPushButton> Widget::Widget(QWidget *parent) : QWidget(parent) { pic=new picWidget; sliderHline=new QSlider(Qt::Horizontal); saveHline=new QPushButton(tr("保存横线")); sliderVline=new QSlider(Qt::Horizontal); saveVline=new QPushButton(tr("保存竖线")); makeIcon=new QPushButton(tr("生成小图片")); QVBoxLayout *rightLayout=new QVBoxLayout; rightLayout->addWidget(sliderHline); rightLayout->addWidget(saveHline); rightLayout->addStretch(10); rightLayout->addWidget(sliderVline); rightLayout->addWidget(saveVline); rightLayout->addStretch(10); rightLayout->addWidget(makeIcon); QHBoxLayout *mainLayout=new QHBoxLayout; mainLayout->addWidget(pic); mainLayout->addLayout(rightLayout); setLayout(mainLayout); QPixmap pixmap(":/source.jpg"); sliderHline->setRange(0,pixmap.height()-1); sliderVline->setRange(0,pixmap.width()-1); connect(sliderHline,SIGNAL(valueChanged(int)),pic,SLOT(changeHline(int))); connect(saveHline,SIGNAL(clicked()),pic,SLOT(saveHline())); connect(sliderVline,SIGNAL(valueChanged(int)),pic,SLOT(changeVline(int))); connect(saveVline,SIGNAL(clicked()),pic,SLOT(saveVline())); connect(makeIcon,SIGNAL(clicked()),pic,SLOT(makeIcon())); } Widget::~Widget() { }
#include <QtGui/QApplication> #include "widget.h" #include <QTextCodec> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); Widget w; w.show(); return a.exec(); }
最后生成的小图片
三、总结
其实这也不容易,还是找个会美工的女朋友好。
...只有把图片宽度缩小 才能完全显示出来。