QT-QT button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画)
.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QWebEngineView> 6 #include "qlayout.h" 7 #include "qpushbutton.h" 8 #include "QMouseEvent" 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class CMyButton; 15 16 class MainWindow : public QMainWindow 17 { 18 Q_OBJECT 19 20 public: 21 MainWindow(QWidget *parent = nullptr); 22 ~MainWindow(); 23 24 QWebEngineView *view; 25 QHBoxLayout *hl; 26 QPushButton *m_bu; 27 private slots: 28 void on_bu(); 29 private: 30 char* setstr(char *s); 31 private: 32 Ui::MainWindow *ui; 33 QPixmap pixmapToShow; 34 QPixmap pixmapToShow2; 35 QPixmap pixmapToShow3; 36 }; 37 38 class CImageButton : public QPushButton 39 { 40 Q_OBJECT 41 42 public: 43 CImageButton(QWidget *parent); 44 ~CImageButton(); 45 void SetPixmap(QPixmap* pNormal,QPixmap* pHover,QPixmap* pLighted); 46 void SetLighted(bool value); 47 public: 48 void paintEvent(QPaintEvent* pEvent); 49 void mousePressEvent(QMouseEvent *e); 50 void mouseReleaseEvent(QMouseEvent *e); 51 void enterEvent(QEvent *e); 52 void leaveEvent(QEvent *e); 53 protected: 54 bool m_bLighted; 55 bool m_bMouseIN; 56 bool m_bLeftDown; 57 QPixmap* m_pHover; 58 QPixmap* m_pNormal; 59 QPixmap* m_pLighted; 60 }; 61 62 class CMyButton : public QPushButton 63 { 64 public: 65 CMyButton(QWidget *parent); 66 ~CMyButton(); 67 public: 68 void paintEvent(QPaintEvent *pEvent) override; 69 70 }; 71 72 73 #endif // MAINWINDOW_H
.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 #include "qdialog.h" 4 #include "qdir.h" 5 #include "qlabel.h" 6 #include "qbitmap.h" 7 #include "tchar.h" 8 9 10 11 MainWindow::MainWindow(QWidget *parent) 12 : QMainWindow(parent) 13 , ui(new Ui::MainWindow) 14 { 15 ui->setupUi(this); 16 17 //水平布局 18 hl = new QHBoxLayout; 19 QPixmap pixmap(":/new/a/im/1.png"); 20 QPixmap pixmap2(":/new/a/im/A1.bmp"); 21 QPushButton *m_pPushButton = new QPushButton; 22 //Qt设置QPushButton文字加图片 23 //https://blog.csdn.net/wzz953200463/article/details/100637126 24 //方法一: 25 //m_pPushButton->setStyleSheet("qproperty-icon: url(C:/Users/zhujq-a/Desktop/QT/2/untitled/im/1.png);"); 26 //m_pPushButton->setText("清理"); 27 //方法二: 28 //m_pPushButton->setIcon(QPixmap("C:/Users/zhujq-a/Desktop/QT/2/untitled/im/1.png")); 29 //m_pPushButton->setText("清理"); 30 31 //QT中setMask()的图片设置 32 /**************************************************/ 33 //https://blog.csdn.net/qq_35674193/article/details/79329954 34 //首先设置一张主图,就是你要显示的图片,然后准备一张遮掩图,用QBitmap类型, 35 //遮掩图中 36 //1.黑色区域表示显示的部分, 37 //2.白色区域表示不显示部分 38 /**************************************************/ 39 //QT button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画) 40 //方法1: 41 //m_pPushButton->setStyleSheet("QPushButton{border:0px;}");//这句务必加上,否则看到的就是矩形了,而不是不规则图形了 42 //m_pPushButton->setText("中文"); 43 //m_pPushButton->setIcon(QPixmap(":/new/a/im/1.png")); 44 //m_pPushButton->setIconSize(QPixmap(":/new/a/im/1.png").size()); 45 //m_pPushButton->resize(QPixmap("::/new/a/im/1.png").size()); 46 //可以显示不规则图片,但是没有鼠标经过,按下效果。 47 //PS:图片在左,文字在右。 48 //方法2: 49 //m_pPushButton->setFixedSize(QPixmap(":/new/a/im/1.png").size()); 50 //m_pPushButton->setStyleSheet("border-image:url(:/new/a/im/1.png)"); 51 //m_pPushButton->setText("中文"); 52 //可以显示不规则图片,但是没有鼠标经过,按下效果。 53 //PS:图片、文字重叠。 54 //方法3: 55 //m_pPushButton->setFixedSize(QPixmap(":/new/a/im/1.png").size()); 56 //m_pPushButton->setIcon(QPixmap(":/new/a/im/1.png")); 57 //m_pPushButton->setIconSize(QPixmap(":/new/a/im/1.png").size()); 58 //m_pPushButton->setMask(QPixmap(":/new/a/im/1.png").createHeuristicMask()); 59 //m_pPushButton->setMask(QPixmap(":/new/a/im/A1.png")); 60 //不过该方法效果并不好,能看到button的边缘有锯齿,createHeuristicMask换成mask也是一样。 61 //方法4: 62 //或着m_pPushButton->setIcon(QPixmap("C:/Users/zhujq-a/Desktop/QT/2/untitled/im/1.png")); 63 //m_pPushButton->setIcon(QIcon(":/new/a/im/1.png")); 64 //m_pPushButton->setIconSize(QPixmap(":/new/a/im/1.png").size()); 65 //m_pPushButton->setMask(pixmap2); 66 //m_pPushButton->setFixedSize(600, 700);//因为是隐去其他区域,所以只会显示图片中的内容大小。 67 //方法5:继承qpushButton,重写paintevent,在里面可以设置mask或者通过qpainterPath自己构造不规则轮廓,代码如下: 68 //QPushButton *m_pMyButton = new CMyButton(this); 69 //connect(m_pMyButton, &QPushButton::clicked, this, &MainWindow::on_bu); 70 //hl->addWidget(m_pMyButton); 71 //联接测试事件 72 //connect(m_pPushButton, &QPushButton::clicked, this, &MainWindow::on_bu); 73 //向布局增加按钮 74 //hl->addWidget(m_pPushButton); 75 76 //Lable显示图片方式 77 //方法6: 78 //QLabel * m_pLabel = new QLabel; 79 //m_pLabel->setFixedSize(QPixmap(":/new/a/im/1.png").size()); 80 //m_pLabel->setScaledContents(true); 81 //m_pLabel->setPixmap(QPixmap(":/new/a/im/1.png"));//可以保证图片不失真 82 //m_pLabel->setPixmap(QPixmap(":/new/a/im/1.png").scaled(40,40));//可以看到label的边缘有锯齿 83 //hl->addWidget(m_pLabel); 84 //方法7: 85 //QLabel *label1 = new QLabel; 86 //QImage *image = new QImage; 87 //image->load(":/new/a/im/1.png"); 88 //label1->setPixmap(pixmap); 89 //hl->addWidget(label1); 90 //方法8 91 //QLabel *label1 = new QLabel; 92 //QImage *image = new QImage; 93 //image->load(":/new/a/im/1.png"); 94 //QImage *imgScaled = new QImage; 95 //*imgScaled = image->scaled(600, 500, Qt::KeepAspectRatio); 96 //label1->setPixmap(QPixmap::fromImage(*imgScaled)); 97 //hl->addWidget(label1); 98 //会显示的十分大,不过会有失真情。 99 //方法8是在方法5的基础上绘制画片 100 //QImage *image = new QImage; 101 //image->load(":/new/a/im/1.png"); 102 //需要在.h文件中定义“QPixmap pixmapToShow;”,要不然绘制时提示图片不存在。 103 //pixmapToShow = QPixmap::fromImage(*image); 104 //QImage *image2 = new QImage; 105 //image2->load(":/new/a/im/2.png" ); 106 //pixmapToShow2 = QPixmap::fromImage(*image2);//&QPixmap::fromImage(image2->scaled(size(), Qt::KeepAspectRatio)); 107 //QImage *image3 = new QImage; 108 //image3->load(":/new/a/im/3.png" ); 109 //pixmapToShow3 = QPixmap::fromImage(*image3);//QPixmap::fromImage(image3->scaled(size(), Qt::KeepAspectRatio)); 110 //CImageButton* m_pOrderAbove = new CImageButton(this); 111 //m_pOrderAbove->setFixedSize(100, 100); 112 //m_pOrderAbove->SetPixmap(&pixmapToShow, &pixmapToShow2, &pixmapToShow3); 113 //m_pOrderAbove->setText(QString::fromLocal8Bit("123as")); 114 //m_pOrderAbove->setStyleSheet("color: rgb(0, 0, 0);");//"color: rgb(255, 255, 255);" 115 //m_pOrderAbove->setGeometry(144, 0, pixmapToShow.width(), pixmapToShow.height()); 116 //connect(m_pOrderAbove, &QPushButton::clicked, this, &MainWindow::on_bu); 117 //hl->addWidget(m_pOrderAbove); 118 //有鼠标进入、移出、按下效果,不过绘的字和图片重叠了,而且进入事件是按钮的大小,不是图片实现大小。 119 120 121 char strings[] = "abc"; 122 char *str = strings; 123 setstr(str); 124 qDebug() << str; 125 126 127 // view = new QWebEngineView(this); 128 // connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); 129 // view->load(QUrl(QDir::currentPath() + "123.html")); 130 // view->show(); 131 // view->load(QUrl(QStringLiteral("C:/123.html"))); 132 // view->setContentsMargins(0,0,0,0); 133 // view->showMaximized(); 134 // hl->addWidget(view); 135 ui->centralwidget->setLayout(hl); 136 } 137 138 139 140 MainWindow::~MainWindow() 141 { 142 delete ui; 143 } 144 145 void MainWindow::on_bu() 146 { 147 QDialog *odia = new QDialog; 148 QHBoxLayout *ohb = new QHBoxLayout; 149 QPushButton *obu = new QPushButton; 150 obu->setText("OK"); 151 ohb->addWidget(obu); 152 odia->setLayout(ohb); 153 odia->show(); 154 } 155 156 void MainWindow::setstr(char *s) 157 { 158 QString str1; 159 for (int i = strlen(s)-1; i>=0; i--) 160 { 161 qDebug() << s[i]; 162 str1 = str1 + s[i]; 163 } 164 // char* p = new char[3]; 165 166 // memset(p, '\0', strlen(p)); 167 memcpy(s, str1.toStdString().c_str(), 3); 168 // qDebug() << p; 169 // qDebug() << strlen(p); 170 // return p; 171 172 } 173 174 175 CImageButton::CImageButton(QWidget *parent) 176 :QPushButton(parent), 177 m_bMouseIN(false), 178 m_bLeftDown(false), 179 m_bLighted(false) 180 { 181 182 } 183 184 CImageButton::~CImageButton() 185 { 186 187 } 188 189 void CImageButton::SetPixmap(QPixmap *pNormal, QPixmap *pHover, QPixmap *pLighted) 190 { 191 m_pNormal=pNormal; 192 m_pHover=pHover; 193 m_pLighted=pLighted; 194 } 195 196 void CImageButton::SetLighted(bool value) 197 { 198 if(value != m_bLighted) 199 { 200 m_bLighted=value; 201 repaint(); 202 } 203 } 204 205 void CImageButton::paintEvent(QPaintEvent *pEvent) 206 { 207 QPixmap* pPixmap; 208 if(m_bLighted || m_bLeftDown) 209 pPixmap=m_pLighted; 210 else 211 { 212 if(m_bMouseIN) 213 pPixmap=m_pHover; 214 else 215 pPixmap=m_pNormal; 216 } 217 QPainter painter; 218 painter.begin(this); 219 if(pPixmap) 220 painter.drawPixmap(rect(),*pPixmap); 221 if(text().count() > 0) 222 painter.drawText(rect(),Qt::AlignCenter,text()); 223 painter.end(); 224 } 225 226 void CImageButton::mousePressEvent(QMouseEvent *e) 227 { 228 if(e->button()==Qt::LeftButton) 229 { 230 m_bLeftDown=true; 231 repaint(); 232 } 233 QPushButton::mousePressEvent(e); 234 } 235 236 void CImageButton::mouseReleaseEvent(QMouseEvent *e) 237 { 238 if(e->button()==Qt::LeftButton) 239 { 240 m_bLeftDown=false; 241 repaint(); 242 } 243 QPushButton::mouseReleaseEvent(e); 244 } 245 246 void CImageButton::enterEvent(QEvent *e) 247 { 248 m_bMouseIN=true; 249 repaint(); 250 } 251 252 void CImageButton::leaveEvent(QEvent *e) 253 { 254 m_bMouseIN=false; 255 repaint(); 256 } 257 258 CMyButton::CMyButton(QWidget *parent) 259 :QPushButton(parent) 260 { 261 262 } 263 264 CMyButton::~CMyButton() 265 { 266 267 } 268 269 void CMyButton::paintEvent(QPaintEvent *pEvent) 270 { 271 QPainter painter(this); //创建painter 272 painter.setRenderHint(QPainter::Antialiasing, true); //消除锯齿效果 273 274 //构造painterpath 275 QPainterPath path; 276 path.moveTo(0, 0); 277 path.lineTo(100, 0); 278 path.lineTo(100/2, 200); 279 path.lineTo(0, 0); 280 281 //path->setFillRule(Qt::WindingFill); 282 //设置无画笔,避免边框出现一条黑线 283 painter.setPen(Qt::NoPen); 284 //设置画刷 285 painter.setBrush(QBrush(QColor(36,169,225), Qt::SolidPattern)); 286 //绘制背景 287 painter.drawPath(path); 288 289 pEvent->accept();//不再向父类传递消息 290 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我