111 lines
2.2 KiB
C++
111 lines
2.2 KiB
C++
#include "HMessageBox.h"
|
|
#include <qevent.h>
|
|
|
|
HMessageBox::HMessageBox(QWidget* parent) :QWidget(parent)
|
|
{
|
|
setFixedSize(350, 200);
|
|
setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::MSWindowsFixedSizeDialogHint);
|
|
setStyleSheet("QWidget{background-color: rgb(30, 30, 30);}"
|
|
"QTextEdit{border-style: none;color:white; font-size:20px;}"
|
|
"QPushButton{color:white; font-size:20px;}");
|
|
//initWidget(str);
|
|
okButton = new QPushButton("OK");
|
|
noButton = new QPushButton("NO");
|
|
connect(okButton, &QPushButton::clicked, this, &HMessageBox::clickOkButton);
|
|
connect(noButton, &QPushButton::clicked, this, &HMessageBox::clickNoButton);
|
|
}
|
|
|
|
HMessageBox::~HMessageBox()
|
|
{
|
|
if (okButton) {
|
|
delete okButton;
|
|
okButton = nullptr;
|
|
}
|
|
if (noButton) {
|
|
delete noButton;
|
|
noButton = nullptr;
|
|
}
|
|
if (infoLabel) {
|
|
delete infoLabel;
|
|
infoLabel = nullptr;
|
|
}
|
|
if (m_eventLoop) {
|
|
|
|
delete m_eventLoop;
|
|
m_eventLoop = nullptr;
|
|
}
|
|
|
|
}
|
|
|
|
void HMessageBox::initWidget(QString info,int windowMode)
|
|
{
|
|
infoLabel = new QTextEdit();
|
|
//infoLabel->setWordWrap(true);
|
|
infoLabel->adjustSize();
|
|
infoLabel->setText(info);
|
|
|
|
okButton->setFixedSize(80, 40);
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout();
|
|
layout->addWidget(infoLabel);
|
|
QHBoxLayout* layout1 = new QHBoxLayout();
|
|
layout1->addWidget(okButton,Qt::AlignCenter);
|
|
if (windowMode == 1)
|
|
{
|
|
noButton->setFixedSize(80, 40);
|
|
layout1->addWidget(noButton, Qt::AlignCenter);
|
|
}
|
|
layout->addLayout(layout1);
|
|
setLayout(layout);
|
|
|
|
|
|
}
|
|
|
|
void HMessageBox::clickOkButton()
|
|
{
|
|
clickResult = 0;
|
|
close();
|
|
}
|
|
|
|
void HMessageBox::clickNoButton()
|
|
{
|
|
clickResult = 1;
|
|
close();
|
|
}
|
|
|
|
int HMessageBox::showMyMessageBox(QWidget* parent, const QString& contentText, int windowMode, bool isModelWindow)
|
|
{
|
|
QScopedPointer<HMessageBox> ptr;
|
|
HMessageBox* myMessageBox = new HMessageBox(parent);
|
|
myMessageBox->initWidget(contentText, windowMode);
|
|
ptr.reset(myMessageBox);
|
|
if (isModelWindow)
|
|
{
|
|
return myMessageBox->exec();
|
|
}
|
|
else
|
|
{
|
|
myMessageBox->show();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int HMessageBox::exec()
|
|
{
|
|
this->setWindowModality(Qt::WindowModal);
|
|
show();
|
|
m_eventLoop = new QEventLoop(this);
|
|
m_eventLoop->exec();
|
|
|
|
return clickResult;
|
|
}
|
|
|
|
void HMessageBox::closeEvent(QCloseEvent* event)
|
|
{
|
|
if (m_eventLoop != NULL)
|
|
{
|
|
m_eventLoop->exit();
|
|
}
|
|
event->accept();
|
|
}
|