126 lines
4.3 KiB
C++
Raw Normal View History

2022-07-30 11:50:01 +08:00
#include "msgbox.h"
MsgBox::MsgBox(QMessageBox::Icon icon,const QString &title, const QString &text,QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton, QWidget *parent) :
QDialog(parent)
{
setModal(true);
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
this->setStyleSheet("*{background:#1f212c;}\
QMessageBox{background:rgba(0,0,0,1%);border: 1px solid rgb(42,45,61);}\
QPushButton{background:rgba(21,131,218,80%);color:white;text-align:center;}\
QPushButton:hover{background:rgba(21,131,218,1);}\
QLabel{background:rgba(255,255,255,0%);color:rgba(255,255,255,50%);text-align:center;font-size:15px;}");
m_Frame = new QLabel(this);
m_Frame->setStyleSheet("QLabel{border: 1px solid rgba(21,131,218,40%)}");
m_Logo = new QLabel(this);
m_Logo->setStyleSheet("QLabel{image: url(:/images/images/imgSystem/systemIcon.png); background:rgba(0,0,0,0%);}");
m_Title = new QLabel(this);
m_Title->setText(title);
m_Title->setStyleSheet("QLabel{background:rgba(0,0,0,0%);color:white}");
m_BtnClose = new QPushButton(this);
m_BtnClose->setStyleSheet("QPushButton {image: url(:/img/sys_close_normal.png); background:rgba(0,0,0,0%);}\
QPushButton:hover {image: url(:/img/sys_close_active.png);}");
m_MsgBox = new QMessageBox(icon,title,text,buttons);
/*m_MsgBox->setStyleSheet("QMessageBox{background:rgba(0,0,0,1%);border: 1px solid rgb(42,45,61);\
QLabel{color:white;}\
QPushButton{background:rgba(21,131,218,80%);color:white;text-align:center;}\
QPushButton:hover{background:rgba(21,131,218,1);}\
}");*/
m_MsgBox->setParent(this);
m_MsgBox->show();
resize(m_MsgBox->width()+10,m_MsgBox->height()+26);
m_MsgBox->move(1,25);
connect(m_BtnClose,SIGNAL(clicked(bool)),this,SLOT(close()));
connect(m_MsgBox,SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(onButtonClicked(QAbstractButton*)));
}
MsgBox::~MsgBox()
{
delete m_Title;
delete m_Logo;
delete m_BtnClose;
delete m_MsgBox;
deleteLater();
}
void MsgBox::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void MsgBox::resizeEvent(QResizeEvent *Size)
{
int total_width = Size->size().width();
int total_height = Size->size().height();
m_Frame->setGeometry(0,0,total_width,total_height);
int title_h = 25;
m_Logo->setGeometry(0,0,title_h,title_h);
m_Title->setGeometry(title_h,0,total_width-2*title_h,title_h);
m_BtnClose->setGeometry(total_width-title_h,0,title_h,title_h);
//m_MsgBox->setGeometry(0,title_h,total_width,total_height-title_h);
}
void MsgBox::mouseReleaseEvent(QMouseEvent *event)
{
//for move windows
if((true==m_MouseLButtonPressed)
&&(event->button() == Qt::LeftButton))
{
m_MouseLButtonPressed = false;
}
}
void MsgBox::mousePressEvent(QMouseEvent *event)
{
//for move windows
if(event->button() == Qt::LeftButton)
{
int y = event->pos().y();
if((y<30)||(y>this->height()-30))
{
m_MouseLButtonPressed=true;
m_dPos = event->globalPos() - this->pos();
}
}
}
void MsgBox::mouseMoveEvent(QMouseEvent *event)
{
if(m_MouseLButtonPressed)
{
this->move(event->globalPos() - m_dPos);
}
QWidget::mouseMoveEvent(event);
}
void MsgBox::onButtonClicked(QAbstractButton *button)
{
int nResult = m_MsgBox->standardButton(button);
m_MsgBox->done(nResult);
this->close();
}
QMessageBox::StandardButton MsgBox::standardButton(QAbstractButton *button) const
{
return (QMessageBox::StandardButton)m_MsgBox->standardButton(button);
}
QMessageBox::StandardButton MyMsgBox(QMessageBox::Icon icon,const QString &title, const QString &text,QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton, QWidget *parent)
{
MsgBox *msgbox = new MsgBox(icon,title,text,buttons,defaultButton,parent);
msgbox->show();
if (msgbox->m_MsgBox->exec() == -1)
return QMessageBox::Cancel;
return msgbox->m_MsgBox->standardButton(msgbox->m_MsgBox->clickedButton());
}