1、设置主界面为圆角
在主界面构造函数中添加:
1240为主界面长
770为主界面宽
QDesktopWidget* desktopWidget =QApplication::desktop();
QRect deskRect =desktopWidget->availableGeometry();
int nWidth = deskRect.width();
int nHeight = deskRect.height();
this->setGeometry((QRect((nWidth - 1240) / 2, (nHeight - 770) / 2, 1240, 770)));
setAttribute(Qt::WA_TranslucentBackground);//背景透明
2、单、多屏幕最大化
需要获取软件所在的屏幕大小和任务栏大小和位置,同时还需要获取软件所在屏幕序号,
//最大化按钮
void Core::Win_max()
{
static int rect_x = 0;
static int rect_y = 0;
if(!windows_max)
{
QDesktopWidget *deskTop = QApplication::desktop();
rect_x = geometry().x();
rect_y = geometry().y();
int curMonitor = deskTop->screenNumber(this);//软件所在屏幕序号
QRect rect = deskTop->screenGeometry(curMonitor);//软件所在屏幕大小
QRect deskRect = deskTop->availableGeometry(curMonitor);//软件所在除任务栏外的大小
if(deskRect.bottom() < rect.bottom())
{
setGeometry(rect.x(),rect.y(),rect.width(),deskRect.height());
}
else if(deskRect.right() < rect.right())
{
setGeometry(rect.x(),rect.y(),deskRect.width(),rect.height());
}
else if(deskRect.top() > rect.top())
{
setGeometry(rect.x(),deskRect.y(),rect.width(),deskRect.height());
}
else if(deskRect.left() > rect.left())
{
setGeometry(deskRect.x(),rect.y(),deskRect.width(),rect.height());
}
else
{
setGeometry(rect.x(),rect.y(),rect.width(),rect.height());
}
windows_max = true;
}
else
{
showNormal();
setGeometry(rect_x,rect_y,WINDOWS_WIDTH,WINODWS_HEIGHT);
windows_max = false;
}
//最大化按钮
if(!windows_max)
{
ui->pushButton_max->setStyleSheet("QPushButton{border-image: url(:/resource/img/2.png);}"
"QPushButton:hover{border-image: url(:/resource/img/5.png);}"
"QPushButton:pressed{border-image: url(:/resource/img/8.png);}");
}
else
{
ui->pushButton_max->setStyleSheet("QPushButton{border-image: url(:/resource/img/10.png);}"
"QPushButton:hover{border-image: url(:/resource/img/11.png);}"
"QPushButton:pressed{border-image: url(:/resource/img/12.png);}");
}
}
3、设置exe只能运行一个
QtSingleApplication库下载地址:https://download.csdn.net/download/bigtree_mfc/14021590
在pro中添加
include(./qtsingleapplication/qtsinglecoreapplication.pri)
然后在主函数中添加头文件
#include "QtSingleApplication"
在main函数中替换
QApplication a(argc, argv);
类名称 w;
w.show();
为这个
QtSingleApplication a("exe名称",argc, argv);//将原来的QApplication改为QtSingleApplication
if(a.isRunning())//判断是否有实例在运行
{
a.sendMessage("raise_window_noop", 4000); //4s后激活前个实例
return EXIT_SUCCESS;//当前未启动的实例退出
}
类名称 w;
a.setActivationWindow(&w,1);
w.show();//显示主界面
return a.exec();
exe名称为任务管理器中的名称