setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
setStyleSheet("border:3px groove #FF8C00");
使用setStyleSheet可以以html语法格式进行控件的各种外观定制,但需要注意的是父控件styleSheet属性会对子控件也生效,所以对有子控件的父控件需要慎用setStyleSheet
setWindowOpacity(0.7);
这个方法只对具有Qt::Window窗口标识的窗口起作用,能够设置整个窗口的透明度,透过这个窗口可以看到其他的窗口
setAttribute(Qt::WA_TranslucentBackground, true);
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Background, Qt::black);
//pal.setColor(QPalette::Background, Qt::transparent);
//pal.setColor(QPalette::Background, QColor(255,0,0,128));
setPalette(pal);
注意不要忘了先setAutoFillBackground,通过设置控件的调色板即可实现改变背景色和透明度(rgba中的a通道)
设置背景和边框还可以使用setStyleSheet方法,但是这个方法会影响到所有的子控件,请慎用
pLabel->setStyleSheet("background-color: #00000000; border:2px solid gray;");
QWebEngineView除了本身需要使用setPalette设置调色板外,它还有一个page层,需要将它设为透明,默认是白色的背景色
m_webview->page()->setBackgroundColor(Qt::transparent);
void HMainWidget::onFullScreen(){
QWidget* pSender = (QWidget*)sender();
// m_rcSavedGeometry 用来保存控件原先的位置,退出全屏时还原
m_rcSavedGeometry = pSender->geometry();
pSender->setWindowFlags(Qt::Window);
pSender->showFullScreen();
}
void HMainWidget::onExitFullScreen(){
QWidget* pSender = (QWidget*)sender();
pSender->setWindowFlags(Qt::SubWindow);
pSender->setGeometry(m_rcSavedGeometry);
pSender->showNormal();
}
全屏时需要先将该控件设置为窗口类型,调用showFullScreen才会生效,退出全屏时还原控件的类型和位置即可
注意的是信号与槽分为直接连接和异步连接,同一线程下创建的对象是直接连接,触发信号时会马上调用槽函数,但不同线程下的对象是异步连接,信号发出时会QCoreApplication::postEvent放入事件队列,直到取出时才会调用槽函数。
当信号调用槽,而槽中又发出这个信号时,这时就会形成死循环,写槽函数发射信号时一定要注意。
当一个事件不想在往父控件传递时,请使用accept
qDebug()<<"applicationDirPath="<
QT中提供了QSplashScreen类来为你的应用程序添加启动画面,它可以通过setPixmap添加一张背景图片,showMessage显示一条消息,需要注意的是当你切换了图片或者消息时,需要调用QCoreApplication::processEvents去处理一次事件,示例代码入下:
QApplication app(argc, NULL);
QFont font = QApplication::font();
font.setPointSize(18);
QApplication::setFont(font);
int sw = QApplication::desktop()->width();
int sh = QApplication::desktop()->height();
QSplashScreen* splash = new QSplashScreen;
splash->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
splash->setGeometry(0,0,sw,sh);
splash->setStyleSheet("background-color: black");
splash->setPixmap("./splash.png");
splash->showFullScreen();
app.processEvents();
splash->showMessage("10% Loading icon...", Qt::AlignCenter, Qt::white);
app.processEvents();
HRcLoader::instance()->loadIcon();
splash->showMessage("30% Loading texture...", Qt::AlignCenter, Qt::white);
app.processEvents();
HRcLoader::instance()->loadTexture();
splash->showMessage("50% Creating main ui...", Qt::AlignCenter, Qt::white);
app.processEvents();
HMainWidget* mainwdg = new HMainWidget;
mainwdg->show();
splash->showMessage("100% Complete!", Qt::AlignCenter, Qt::white);
app.processEvents();
splash->finish(mainwdg);
delete splash;
app.exec();
HRcLoader和HMainWidget皆是我项目中定义的类,你不用管,换成自己的操作就行。
当然这个类只提供了基本的启动画面,如果想实现动态画面,需要自己去自定义组合控件了,比如添加label去显示动画,添加进度条去显示进度
m_slider->setStyleSheet(
"QSlider::groove:horizontal {"
" height: 16px;"
" background: #C0C0C0;"
" border: 1px solid green;"
" border-radius: 5px;"
" padding-left:-1px;"
" padding-right:-1px;"
"}"
"QSlider::sub-page:horizontal"
"{"
"height: 16px;"
"background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);"
"background: qlineargradient(x1:0, y1:0.2, x2:1, y2:1, stop:0 #5DCCFF, stop:1 #1874CD);"
"border: 1px solid #00FFFF;"
"border-radius: 2px;"
"}"
"QSlider::add-page:horizontal"
"{"
"height: 16px;"
"background: #575757;"
"border: 1px solid #00FFFF;"
"border-radius: 2px;"
"}"
"QSlider::handle:horizontal"
"{"
"width: 16px;"
"background: qradialgradient"
"("
"spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,"
"stop:0.6 #FFD700, stop:0.778409 #696969"
");"
"margin-top: -8px;"
"margin-bottom: -8px;"
"border-radius: 5px;"
"}"
);
setStyleSheet("QScrollBar:vertical"
"{"
"width:28px;"
"background:rgba(0,0,0,0%);"
"margin:0px,0px,0px,0px;"
"padding-top:28px;"
"padding-bottom:28px;"
"}"
"QScrollBar::handle:vertical"
"{"
"width:28px;"
"background:rgba(0,0,0,25%);"
" border-radius:4px;"
"min-height:20;"
"}"
"QScrollBar::handle:vertical:hover"
"{"
"width:28px;"
"background:rgba(0,0,0,50%);"
" border-radius:4px;"
"min-height:20;"
"}"
"QScrollBar::add-line:vertical"
"{"
"height:28px;width:28px;"
"subcontrol-position:bottom;"
"}"
"QScrollBar::sub-line:vertical"
"{"
"height:28px;width:28px;"
"subcontrol-position:top;"
"}"
"QScrollBar::add-line:vertical:hover"
"{"
"height:28px;width:28px;"
"subcontrol-position:bottom;"
"}"
"QScrollBar::sub-line:vertical:hover"
"{"
"height:28px;width:28px;"
"subcontrol-position:top;"
"}"
"QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
"{"
"background:rgba(0,0,0,10%);"
"border-radius:4px;"
"}"
);