設置屬性即可。
Qt::ToolButtonIconOnly //只顯示圖標
Qt::ToolButtonTextOnly //只顯示文字
Qt::ToolButtonTextBesideIcon // 文字在圖標旁邊
Qt::ToolButtonTextUnderIcon // 文字在圖標下面
Qt::ToolButtonFollowStyle // 遵循QStyle::StyleHint
ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
自定義窗口及拖動
1.自定義無邊框窗口時,需要將窗口標志設為:
Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint
2.然后還需要通過安裝EventFilter給自己監視窗口拖動
其中構造函數實現:
myUi::myUi(QWidget *parent) :
QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
qApp->installEventFilter(this); //給自己加事件過濾器,用來實現拖動窗口
... ...
}
eventFilter事件處理函數實現:
bool myUi::eventFilter(QObject *obj, QEvent *evt)
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent *>(evt);
if(obj == this&&mouse) //判斷拖動
{
if(this->isMaximized())
{
return true;
}
static bool dragFlag = false;
static QPoint dragPoint(0,0);
if(mouse->button()==Qt::LeftButton && mouse->type() ==QEvent::MouseButtonPress) //按下
{
dragFlag =true;
dragPoint = mouse->pos(); //記錄鼠標所在的界面位置
return true;
}
else if(dragFlag && mouse->type() ==QEvent::MouseMove) //拖動
{
this->move(mouse->globalPos() - dragPoint);
return true;
}
else if(mouse->type() ==QEvent::MouseButtonRelease)
{
dragFlag = false;
return true;
}
}
return QWidget::eventFilter(obj,evt);
}
自定義QToolButton/QPushButton開關按鈕
1.以QToolButton為例,構造函數里實現:
myUi::myUi(QWidget *parent) :
QWidget(parent)
{
//... ...
ui->ToolButton->setText("更新設置");
ui->ToolButton->setIcon( QIcon(":/image/ok.png"));
connect(ui->ToolButton ,SIGNAL(clicked()),this,SLOT(onPageStartBtnClicked()));
ui->ToolButton->setProperty("START","Up"); //設置為未按下狀態
//... ...
}
2.槽函數里實現:
void myUi::onPageStartBtnClicked()
{
if(b->property("START")=="Up") //之前是未按下
{
ui->ToolButton->setProperty("START","Down"); //設置為按下
ui->ToolButton->setText("取消設置");
ui->ToolButton->setIcon( QIcon(":/image/colose.png"));
//... ...
}
else
{
ui->ToolButton->setProperty("START","Up"); //設置為未按下狀態
ui->ToolButton->setText("更新設置");
ui->ToolButton->setIcon( QIcon(":/image/ok.png"));
//... ...
}
}
3.QSS代碼:
/*正常狀態下*/
QToolButton[START="Up"] {
border-radius: 20px;
border: none;
padding-left :30px;
color: rgb(255,255,255);
background: rgb(56,167,222);
}
QToolButton[START="Up"]:hover {
padding-bottom: 2px;
background: rgb(0,205,252);
}
QToolButton[START="Up"]:checked,QToolButton[START="Up"]:pressed {
background: rgb(6,144,175);
}
/*取消狀態下*/
QToolButton[START="Down"] {
border-radius: 20px;
border: none;
padding-left :30px;
color: rgb(255,255,255);
background: qlineargradient(spread:pad,
x1:0, y1:0, x2:0, y2:1,
stop:0 rgba(255,176,30, 255),
stop:0.6 rgba(254,139,91, 255),
stop:1.0 rgba(254,176,143, 255));
}
QToolButton[START="Down"]:hover {
padding-bottom: 2px;
background: qlineargradient(spread:pad,
x1:0, y1:0, x2:0, y2:1,
stop:0 rgba(255,176,30, 255),
stop:0.6 rgba(255,74,0, 255),
stop:1.0 rgba(255,183,153, 255));
}
效果
點擊前:

點擊后:

界面陰影
首先,將界面拖放在QFrame子組件里,然后將該QFrame居中,與主窗口間隔10px左右(用來顯示陰影).並將主窗口設為透明屬性.
接下來,有2種方法設置陰影:
1.使用QGraphicsDropShadowEffect圖像陰影效果類
好處在於快捷,只需要在構造函數里實現即可,壞處就是界面有點卡(我這里測試是這樣的)
QGraphicsDropShadowEffect常用函數:
setOffset ( qreal dx, qreal dy ); //設置陰影的偏移度,如果想實現整個界面上下左右都有陰影,則設為dx=0,dy=0. //當dx為負時,表示偏移為左,反之為右 //當dy為負時,表示偏移為上,反之為下 void setBlurRadius ( qreal blurRadius ); //設置陰影半徑,值越大,則陰影效果越強 setColor ( const QColor & color ) //設置陰影顏色
示例-在構造函數里調用:
setAttribute(Qt::WA_TranslucentBackground); QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this); shadowEffect->setOffset(0,0); shadowEffect->setColor(QColor(0,0,0)); shadowEffect->setBlurRadius(10); ui->frame->setGraphicsEffect(shadowEffect);
效果:

2.QPainter繪畫
首先,在構造函數里調用下面函數,設置透明:
setAttribute(Qt::WA_TranslucentBackground);
然后在paintEvent函數里進行繪制
void myUi::paintEvent(QPaintEvent *) //繪畫陰影
{
int size =10; //陰影寬度
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor color(0, 0, 0, 0);
for(int i=0;i<=size; i++)
{
color.setAlpha(i*4);
painter.setPen(color);
painter.setBrush(Qt::transparent);
painter.drawRoundedRect(i,i,this->width()-i*2, this->height()-i*2,15,15);
}
}
由於界面是圓角的,所以通過drawRoundedRect()繪制.
效果:

參考: https://blog.csdn.net/stephan14/article/details/47406881
參考: http://blog.sina.com.cn/s/blog_a6fb6cc90101eoop.html#cmt_53197A33-7F000001-6E85F70F-8B8-8A0
但是如果在低於qt 5.1.1版本時,設置QT::FramelessWindowHint和Qt::WA_TranslucentBackground時會出現一個bug:
在最小化還原時界面停止刷新
參考:https://blog.csdn.net/yiqiyihuiligang/article/details/51438600
播放聲音
當彈出對話框時,需要播放聲音,可以使用windows自帶的聲音,位置在C:\Windows\Media里
QSound播放的只有.wav文件,並且比特率不能太高,可以使用格式工廠,把比特率降到三百多
並且聲音路徑必須是在APP程序的路徑,示例:
QString dir=QCoreApplication ::applicationDirPath();
QString filename(dir+"/sound/ok.wav"); //等價於:"./sound/ok.wav"
qDebug()<<dir;//打印APP路徑
QSound::play ( filename );
參考:https://blog.csdn.net/qq_28364283/article/details/50907329
隱藏任務欄
setWindowFlags(Qt::FramelessWindowHint|Qt::Tool); setAttribute(Qt::WA_TranslucentBackground);
為什么要隱藏任務欄
比如當我們拖動無邊框界面時,需要繪制界面邊框線,如果不隱藏的話,就會出現兩個任務欄圖標

