this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
ui->widget_title->installEventFilter(this);
重写过滤器:
bool Interface::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::WindowActivate)//窗口最小化还原后重绘整个窗口
{
this->repaint();
//qDebug()<<"repaint";
}
if(obj == ui->tableView_patModel)//选择患者后出现添加测试按钮。
{
if(event->type() == QEvent::FocusIn)
{
if(patModel->rowCount()>0)
{
addButton->show();
}
}
else if(event->type() == QEvent::FocusOut)
{
addButton->hide();
}
}/*
if(obj == ui->widget_title)
{
if(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove)
{
QMouseEvent *mouEvent = (QMouseEvent *)event;
if(event->type() == QEvent::MouseButtonPress)
{
clickPos = mapToGlobal(mouEvent->pos());//当前鼠标位置
widgetPos = mapToGlobal(QPoint(0,0));//当前窗口位置
//qDebug()<type() == QEvent::MouseMove)
{
this->move(widgetPos + mouEvent->globalPos() - clickPos);//窗口位置+鼠标移动位置之差=窗口新位置
}
}
}*/
return QDialog::eventFilter(obj,event);
}
QEvent
:
:
WindowActivate 那一句是因为发现窗口最小化还原后,窗口的所有按钮都触发不了,于是我让它还原的时候刷新一下界面。
重写构造函数:
this->setAttribute(Qt::WA_TranslucentBackground);
重写绘图事件:
void Interface::paintEvent(QPaintEvent *event)
{/*
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10, 10, this->width()-20, this->height()-20);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(QColor(246, 244, 219)));
QColor color(0, 0, 0, 50);
for(int i=0; i<10; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2);
color.setAlpha(150 - sqrt(i)*50);
painter.setPen(color);
painter.drawPath(path);
}*/
QDialog::paintEvent(event);
}
我不会告诉你我这个苦逼程序员连美工都包了的。。。
funcButton.h
#ifndef FUNCBUTTON_H
#define FUNCBUTTON_H
#include
class funcButton : public QLabel
{
Q_OBJECT
public:
explicit funcButton(QWidget *parent = 0);
void setButtonPicture(QString pic);//设置按键正常状态
void setPressPicture(QString pic);//设置按键点击状态
void setSelectPicture(QString pic);//设置按键选择状态
void setDisablePicture(QString pic);//设置按键不可以状态
void set_X_Y_width_height(int x, int y, int width, int height);//设置按键坐标和大小
void resizeit(int w , int h);//更改按键大小
void setDisabled(bool);//不使能按键
void setEnabled(bool);//使能按键
protected:
void mousePressEvent (QMouseEvent *event);//重新鼠标按下事件
void mouseReleaseEvent (QMouseEvent *event);//重写鼠标释放事件
void enterEvent(QEvent *);//重写鼠标进入事件
void leaveEvent(QEvent *);//重写鼠标离开事件
private:
QString buttonPicture,pressPicture,selectPicture,disablePicture;//保存图片位置
bool enableState;//使能标志位
signals:
void clicked();//点击信号
};
#endif // FUNCBUTTON_H
funcButton.cpp
#include "funcButton.h"
funcButton::funcButton(QWidget *parent) :
QLabel(parent)
{
enableState = true;
}
void funcButton::setButtonPicture(QString pic)
{
buttonPicture = pic;
this->setStyleSheet(buttonPicture);
}
void funcButton::setPressPicture(QString pic)
{
pressPicture = pic;
}
void funcButton::setSelectPicture(QString pic)
{
selectPicture = pic;
}
void funcButton::setDisablePicture(QString pic)
{
disablePicture = pic;
}
void funcButton::set_X_Y_width_height(int x, int y, int width, int height)
{
this->setGeometry(x, y, width, height);
}
void funcButton::mousePressEvent (QMouseEvent *event)
{
if(!enableState)
return;
this->setStyleSheet(selectPicture+pressPicture);
}
void funcButton::mouseReleaseEvent (QMouseEvent *event)
{
if(!enableState)
return;
this->setStyleSheet(buttonPicture+selectPicture);
emit clicked();
}
void funcButton::enterEvent(QEvent *event)
{
if(!enableState)
return;
QString toolTip = "QToolTip {background-color:white;}";
QString label = QString("QLabel {%1}").arg(buttonPicture+selectPicture);
//selectPicture写上颜色达到进入后更换底色的效果 selectPicture是图片就只有图片
this->setStyleSheet(label+toolTip);//tooltip的背景保持白色
}
void funcButton::leaveEvent(QEvent *event)
{
if(!enableState)
return;
this->setStyleSheet(buttonPicture);
}
void funcButton::resizeit(int w , int h)
{
this->raise();
this->resize(w,h);
}
void funcButton::setDisabled(bool disable)
{
if(disable == true)
{
this->setStyleSheet(disablePicture);
enableState = false;
}
else
{
this->setStyleSheet(buttonPicture);
enableState = true;
}
QLabel::setDisabled(disable);
}
void funcButton::setEnabled(bool enable)
{
enableState = enable;
if(enable == true)
{
this->setStyleSheet(buttonPicture);
}
else
{
this->setStyleSheet(disablePicture);
}
QLabel::setEnabled(enable);
}
使用funcButton
saveButton = new funcButton(ui->widget_title);
QString pixmapButton,pixmapPress,pixmapSelect,pixmapDisable;
pixmapButton = "image: url(:/Pic/silver-save.png);";//按前图标
pixmapPress = "image: url(:/Pic/textured-save.png);" ;//按时图标
pixmapSelect = "background-color: rgb(14, 24, 33);";//选中时背景
pixmapDisable = "image: url(:/Pic/textured-save2.png);" ;//禁用图标
saveButton->setButtonPicture(pixmapButton);//一般状态
saveButton->setPressPicture(pixmapPress);//按下状态
saveButton->setSelectPicture(pixmapSelect);//选择状态
saveButton->setDisablePicture(pixmapDisable);//禁用状态
saveButton->set_X_Y_width_height(dialogWidth-410,9,61,61);
定义:
deafnessButton = new funcButton(ui->widget_title);
pixmapButton = "image: url(:/Pic/silver-noise.png);";//按前图标
pixmapSelect = "image: url(:/Pic/textured-noise.png);" ;//按时图标
deafnessButton->setButtonPicture(pixmapButton);//一般状态
deafnessButton->setSelectPicture(pixmapSelect);//选择状态
deafnessButton->set_X_Y_width_height(dialogWidth-121,35,101,31);//图标定位
connect(deafnessButton,SIGNAL(clicked()),this,SLOT(deafnessButton_clicked()));
点击槽:
void Interface::deafnessButton_clicked()
{
if(deafnessFlg)
{
deafnessFlg = false;
deafnessButton->setButtonPicture("image: url(:/Pic/silver-noise.png);");//一般状态
}
else
{
deafnessFlg = true;
deafnessButton->setButtonPicture("image: url(:/Pic/silver-noise.png);background-color: rgb(14, 24, 33);");//按下状态
}
}
另外有一个比较特别的按键就是点了会有下拉菜单,放一些什么“关于”、“联系我们”什么的,虽然很简单,也顺道写一下~
moreButton = new funcButton(ui->widget_title);
QString pixmapButton,pixmapPress,pixmapSelect,pixmapDisable;
pixmapButton = "image: url(:/Pic/silver-more.png);";//按前图标
pixmapPress = "image: url(:/Pic/textured-more.png);" ;//按时图标
pixmapSelect = "background-color: rgb(14, 24, 33);";//选中时背景
moreButton->setButtonPicture(pixmapButton);//一般状态
moreButton->setPressPicture(pixmapPress);//按下状态
moreButton->setSelectPicture(pixmapSelect);//选择状态
moreButton->set_X_Y_width_height(dialogWidth-130,0,31,31);
moreButton->setToolTip(tr("更多"));
connect(moreButton,SIGNAL(clicked()),this,SLOT(moreButton_clicked()));
moreMenu = new QMenu(ui->widget_title);
connectAction = new QAction(moreMenu);
bakupAction = new QAction(moreMenu);
moreMenu->setStyleSheet("QMenu {background-color: white;}""QMenu::item:selected {background: rgba(224, 224, 224);}");
moreMenu->addAction(connectAction);
moreMenu->addSeparator();
moreMenu->addAction(bakupAction);
connect(moreMenu,SIGNAL(triggered(QAction *)),this,SLOT(onMenu_Triggered(QAction *)));//菜单响应连接
connectAction->setText(tr("重新连接"));
connectAction->setIcon(QIcon(":/Pic/arrow-bold-cycle.png"));
bakupAction->setText(tr("数据库备份"));
bakupAction->setIcon(QIcon(":/Pic/upload.png"));
void Interface::moreButton_clicked()
{
QPoint pos (moreButton->mapToGlobal(QPoint(0,0)).x(),moreButton->mapToGlobal(QPoint(0,0)).y()+31);
moreMenu->exec(pos);
}
void Interface::onMenu_Triggered(QAction *action)
{
if(action == connectAction)
{
//...
}
else if(action == bakupAction)
{
//...
}
}
这个功能是给那些加载大的软件用的,加载很多数据的时候不让用户等太久,并告诉用户当前的加载状态。这个对于我这种小软件本来是用不着的,但是由于我看着那样很酷,还可以顺便给用户看个广告什么的,于是就用上啦,哈哈哈哈哈
main.c
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/Pic/Initialization.png"));
//splash->setWindowOpacity(0.9);
splash->show();
Interface w;
Qt::Alignment topRight = Qt::AlignHCenter | Qt::AlignBottom;
splash->showMessage(QObject::tr("正在启动主界面..."),topRight, Qt::black);
splash->showMessage(QObject::tr("正在加载数据库模块..."),topRight, Qt::black);
splash->showMessage(QObject::tr("正在加载..."),topRight, Qt::black);
QDateTime n2=QDateTime::currentDateTime();
QDateTime now;
do{
now=QDateTime::currentDateTime();
}while (n2.secsTo(now)<=6);//6为需要延时的秒数
splash->finish(&w);
int re = w.Wizard();
if(re == -1)
{
return re;
}
delete splash;
main.c
QSystemSemaphore sema("JAMKey",1,QSystemSemaphore::Open);
sema.acquire();//在临界区操作共享内存 SharedMemory
QSharedMemory mem("SystemObject");//全局对象名
if (!mem.create(1))//如果全局对象以存在则退出
{
QMessageBox::information(0, "HEARING FOR AD104",("HEARING FOR AD104 已经在运行!"));
qDebug()<< sema.release();//如果是Unix系统,会自动释放。
return 0;
}
sema.release();//临界区
main.c
void customMessageHandler( QtMsgType type, const char *msg)
{
QString txt;
switch (type)
{
case QtDebugMsg:
txt = QString("Debug: %1").arg(msg);
break;
case QtWarningMsg:
txt = QString("Warning: %1").arg(msg);
break;
case QtCriticalMsg:
txt = QString("Critical: %1").arg(msg);
break;
case QtFatalMsg:
txt = QString("Fatal: %1").arg(msg);
abort();
}
QString runPath = QCoreApplication::applicationDirPath();
runPath.replace("/","\\");
runPath += "\\debuglog.log";
QFile outFile(runPath);
outFile.open(QIODevice::WriteOnly | QIODevice::Append|QIODevice::Text);
QTextStream ts(&outFile);
ts << txt << endl;
}
然后在main函数里加上这么一句话
qInstallMsgHandler(customMessageHandler);
好吧我的ps功底有限,就只能是简单的搭个颜色什么的了~什么立体效果什么的离我几个星球远~我这美其名曰扁平化设计啦 哈哈