Qt學習筆記(七)自制標題欄、邊框陰影、圓角效果
阿新 • • 發佈:2019-01-08
自制標題欄
this->setWindowFlags(Qt::FramelessWindowHint);//去除Qt自帶的標題欄
去除標題欄後為了實現窗體的視窗最大化、最小化、關閉以及移動事件,需要自定義以下操作:
1.視窗最大化事件
自制QPushButton按鈕,連線到showMax()事件上
void QiXin_companyItemClass::showMax()
{
int deskWidth = QApplication::desktop()->availableGeometry().width();
int deskHeight = QApplication::desktop ()->availableGeometry().height();//除去工作列後高度
this->move(QApplication::desktop()->availableGeometry().x(), QApplication::desktop()->availableGeometry().y());
this->resize(deskWidth, deskHeight);
ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(showNorma()));
}
void showNorma()
{
QDesktopWidget* desktopWidget = QApplication::desktop();
int deskWidth = QApplication::desktop()->availableGeometry().width();
int deskHeight = QApplication::desktop()->availableGeometry().height();//除去工作列後高度
this->resize(deskWidth * 2 / 3, deskWidth * 2*950 / 3/1400 );
this->move((desktopWidget->width() - this->width()) / 2, (desktopWidget->height() - this->height()) / 2);
ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(showMax()));
}
2.視窗最小化事件
自制QPushButton按鈕,連線到showMin()事件上
void showMin()
{
this->showMinimized();
}
3.視窗關閉事件
自制QPushButton按鈕,連線到onButtonCloseClicked()事件上
void onButtonCloseClicked()
{
close();
}
4.視窗移動事件
過載 mousePressEvent、mouseMoveEvent、mouseReleaseEvent
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
bool m_isPressed;
QPoint m_startMovePos;
void mousePressEvent(QMouseEvent *event)
{
int x = event->pos().x();
int y = event->pos().y();
if (x >= 0 && x <= ui.widget->width())
{
if (y >= 0 && y <= ui.widget->height())
{
m_isPressed = true;
m_startMovePos = event->pos();
}
}
return QWidget::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed)
{
QPoint movePoint = event->globalPos();
//m_startMovePos = event->globalPos();
this->move(movePoint.x() - m_startMovePos.x(), movePoint.y() - m_startMovePos.y());
}
return QWidget::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed = false;
return QWidget::mouseReleaseEvent(event);
}
邊框陰影、圓角效果
this->setAttribute(Qt::WA_TranslucentBackground);//設定視窗背景透明
過載paintEvent,窗體要在四周留出10的間距,其窗體要設定圓角樣式 border-radius:5px;
void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 反鋸齒;
QColor tem;
tem.setRgb(47, 47, 47);
painter.setPen(Qt::transparent);
QRect rect = ui.widget_2->rect();
rect.setWidth(rect.width() - 1);
rect.setHeight(rect.height() - 1);
painter.drawRoundedRect(rect, 10, 10);//繪製圓角
//繪製陰影
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10, 10, this->width() - 20, this->height() - 20);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(QColor(45, 45, 45)));
QColor color(0, 0, 0, 50);
for (int i = 0; i<10; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10 - i, 10 - i, this->width() - (10 - i) * 2, this->height() - (10 - i) * 2);
color.setAlpha(150 - sqrt(i) * 50);
painter.setPen(color);
painter.drawPath(path);
}
QWidget::paintEvent(event);
}