QT5 无边框Widget和自定义程序启动画面

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ZuoYueXian/article/details/70241692

一、简介

        模仿QQ设置面板,自定义Widget,并实现了窗体的拖拽,关闭,最小化功能。

二、详解

1、模仿QQ系统设置的无边框界面

(1)代码

  • mainwidget.h:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <TPPPushButton.h>

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();

protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);

private:
    TPPPushButton *minButton;
    TPPPushButton *closeButton;
    QRect rectMove;

};

#endif // MAINWIDGET_H


mainwidget.cpp:

#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
{
    resize(700, 500);
    rectMove = QRect(0, 0, width(), 35);

    minButton = new TPPPushButton(this);
    minButton->setPicName(":/titleButtons/min");
    minButton->setToolTip(tr("minimize"));
    connect(minButton, SIGNAL(clicked()), this, SLOT(showMinimized()));

    closeButton = new TPPPushButton(this);
    closeButton->setPicName(":/titleButtons/close");
    closeButton->setToolTip(tr("close"));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
}

MainWidget::~MainWidget()
{

}

void MainWidget::resizeEvent(QResizeEvent *event)
{
    minButton->move(width() - 27 - closeButton->width(), 6);
    closeButton->move(width() - 27, 6);
    QWidget::resizeEvent(event);
}

void MainWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor("#F0F0F0"));
    painter.drawRect(this->rect());
    painter.setBrush(QColor("#069dd5"));
    painter.drawRect(rectMove);
    painter.setPen(QColor("#D7D7D7"));
    painter.drawLine(0, 70, this->width(), 70);
    QWidget::paintEvent(event);
}




三、总结

(1)相应的代码已上传到csdn  http://download.csdn.net/detail/zuoyuexian/9819147 。
(2)若有建议,请留言,在此先感谢!





展开阅读全文

没有更多推荐了,返回首页