16,234
社区成员
发帖
与我相关
我的任务
分享
#include "frmMessageBox.h"
#include <QDebug>
frmMessageBox::frmMessageBox(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
InitStyle();
}
frmMessageBox::~frmMessageBox()
{
}
void frmMessageBox::mouseMoveEvent(QMouseEvent *e)
{
if (mousePressed && (e->buttons() && Qt::LeftButton))
{
this->move(e->globalPos() - mousePoint);
e->accept();
}
}
void frmMessageBox::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
mousePressed = true;
mousePoint = e->globalPos() - this->pos();
e->accept();
}
}
void frmMessageBox::mouseReleaseEvent(QMouseEvent *)
{
mousePressed = false;
}
void frmMessageBox::InitStyle()
{
this->setProperty("Form", true);
//窗体居中显示
QDesktopWidget *desktopWidget = QApplication::desktop();
QRect clientRect = desktopWidget->availableGeometry();
qDebug() << clientRect.width() << clientRect.height();
QPoint movePoint(clientRect.width() / 2 - this->width() / 2, clientRect.height() / 2 - this->height() / 2);
this->move(movePoint);
this->mousePressed = false;
//设置窗体标题栏隐藏
this->setWindowFlags(Qt::FramelessWindowHint);
//设置图形字体
SetIcon(ui.lab_Ico, QChar(0xf015), 12);
SetIcon(ui.btnMenu_Close, QChar(0xf00d), 10);
//关联关闭按钮
connect(ui.btnMenu_Close, SIGNAL(clicked()), this, SLOT(close()));
connect(ui.btnCancel, SIGNAL(clicked()), this, SLOT(close()));
}
void frmMessageBox::SetIcon(QLabel *lab, QChar c, int size)
{
iconFont.setPointSize(size);
lab->setFont(iconFont);
qDebug() << c;
lab->setText(c);
}
void frmMessageBox::SetIcon(QPushButton *btn, QChar c, int size)
{
iconFont.setPointSize(size);
btn->setFont(iconFont);
btn->setText(c);
}
void frmMessageBox::SetMessage(QString msg, int type)
{
if (type == 0)
{
ui.labIcoMain->setStyleSheet("border-image: url(:/TestStudy/image/info.png);");
ui.btnCancel->setVisible(false);
ui.lab_Title->setText(QStringLiteral("提示"));
}
else if (type == 1)
{
ui.labIcoMain->setStyleSheet("border-image: url(:/TestStudy/image/question.png);");
ui.lab_Title->setText(QStringLiteral("询问"));
}
else if (type == 2)
{
ui.labIcoMain->setStyleSheet("border-image: url(:/TestStudy/image/error.png);");
ui.btnCancel->setVisible(false);
ui.lab_Title->setText(QStringLiteral("错误"));
}
ui.labInfo->setText(msg);
this->setWindowTitle(ui.lab_Title->text());
}
void frmMessageBox::on_btnOk_clicked()
{
done(1);
//this->close();
exit(0);
}
#ifndef FRMMESSAGEBOX_H
#define FRMMESSAGEBOX_H
#include <QDialog>
#include <QMouseEvent>
#include <QPoint>
#include <QDesktopWidget>
#include <QFont>
#include <QLabel>
#include <QPushButton>
#include "ui_frmMessageBox.h"
class frmMessageBox : public QDialog
{
Q_OBJECT
public:
frmMessageBox(QWidget *parent = 0);
~frmMessageBox();
void SetMessage(QString msg, int type);
protected:
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *);
void SetIcon(QLabel *lab, QChar c, int size);
void SetIcon(QPushButton *btn, QChar c, int size);
private slots:
void on_btnOk_clicked();
private:
QPoint mousePoint; //鼠标拖动自定义标题栏时的坐标
bool mousePressed; //鼠标是否按下
void InitStyle(); //初始化无边框窗体
QFont iconFont;
private:
Ui::frmMessageBox ui;
};
#endif // FRMMESSAGEBOX_H