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() 
}

猜你喜欢

转载自blog.csdn.net/mozai147/article/details/84192215