1. 程式人生 > >QT 自定義標題欄

QT 自定義標題欄

1、去除舊的標題欄

    //去除QDialog對話方塊有上角問號  
    Qt::WindowFlags flags=Qt::Dialog;flags |=Qt::WindowCloseButtonHint;flags |=Qt::FramelessWindowHint;
    setWindowFlags(flags);

flags |=Qt::FramelessWindowHint;的意思是去除標題欄

2、自定義新的標題欄

參考自 https://blog.csdn.net/u014156690/article/details/49751425

mainwindow.h 裡面加

private slots: void on_actionMinimize_triggered(); //最小化視窗 
void on_actionClose_triggered(); //關閉視窗 
void on_actionMaximize_triggered(); //最大化視窗

mainwindow.cpp裡面加

int width = this->width();//獲取介面的寬度 
QToolButton *minButton = new QToolButton(this); //最小按鈕 
QToolButton *helpButton = new QToolButton(this); //幫助按鈕 
QToolButton *closeButton= new QToolButton(this); //關閉按鈕 
QToolButton *maxButton = new QToolButton(this); //最大按鈕 
QToolButton *menuButton = new QToolButton(this); //選單按鈕 
QToolButton *normalButton = new QToolButton(this); 
QToolButton *shadeButton = new QToolButton(this); 
QToolButton *unshadeButton = new QToolButton(this); 
connect(minButton, SIGNAL(clicked()), this, SLOT(on_actionMinimize_triggered())); connect(closeButton, SIGNAL(clicked()), this, SLOT(on_actionClose_triggered())); connect(maxButton, SIGNAL(clicked()), this, SLOT(on_actionMaximize_triggered())); //獲取最小化、關閉按鈕圖示 
QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton); 
QPixmap helpPix = style()->standardPixmap(QStyle::SP_TitleBarContextHelpButton); QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton); 
QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton); 
QPixmap shadePix = style()->standardPixmap(QStyle::SP_TitleBarShadeButton); 
QPixmap unshadePix = style()->standardPixmap(QStyle::SP_TitleBarUnshadeButton); QPixmap normalPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton); 
QPixmap menuPix = style()->standardPixmap(QStyle::SP_TitleBarMenuButton); //設定最小化、關閉按鈕圖示 
minButton->setIcon(minPix); 
closeButton->setIcon(closePix); 
helpButton->setIcon(helpPix); 
maxButton->setIcon(maxPix); 
shadeButton->setIcon(shadePix); 
unshadeButton->setIcon(unshadePix); 
normalButton->setIcon(normalPix); 
menuButton->setIcon(menuPix); //設定最小化、關閉按鈕在介面的位置 normalButton->setGeometry(width-160, 0, 20, 20); 
menuButton->setGeometry(width-140, 0, 20, 20);
 minButton->setGeometry(width-120,0,20,20); 
closeButton->setGeometry(width-100,0,20,20); 
helpButton->setGeometry(width-80,0,20,20);
 maxButton->setGeometry(width-60, 0, 20, 20); 
shadeButton->setGeometry(width-40,0, 20, 20); 
unshadeButton->setGeometry(width-20, 0, 20, 20); //設定滑鼠移至按鈕上的提示資訊 minButton->setToolTip(tr("最小化")); 
closeButton->setToolTip(tr("關閉")); m
axButton->setToolTip(tr("最大化")); //設定最小化、關閉等按鈕的樣式 minButton->setStyleSheet("background-color:transparent;"); closeButton->setStyleSheet("background-color:transparent;"); normalButton->setStyleSheet("background-color:transparent;"); menuButton->setStyleSheet("background-color:transparent;"); helpButton->setStyleSheet("background-color:transparent;"); maxButton->setStyleSheet("background-color:transparent;"); shadeButton->setStyleSheet("background-color:transparent;"); unshadeButton->setStyleSheet("background-color:transparent;");
void MainWindow::on_actionMinimize_triggered() 
{ //系統自定義的最小化視窗函式 
   showMinimized(); 
}
void MainWindow::on_actionClose_triggered() 
{ //系統自定義的視窗關閉函式 
    close();
 } 
void MainWindow::on_actionMaximize_triggered() 
{ //最大化 
    showMaximized() 
}