qt实现漂亮主页面

模仿自feiyangqingyun的博客_CSDN博客-Qt/C++控件SDK使用示例,Qt/C++音视频开发,Qt/C++自定义控件领域博主 1.无边框+窗口可移动

#ifndef MOVABLE_WIDGET_H
#define MOVABLE_WIDGET_H

#include <QWidget>

class movable_widget:public QWidget
{
public:
    movable_widget(QWidget * parent=0);
protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
private:
    QPoint Pos;
    bool ismoving;
};

#endif // MOVABLE_WIDGET_H
#include "movable_widget.h"
#include <QMouseEvent>
movable_widget::movable_widget(QWidget * parent):
    QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);
}
void movable_widget::mouseMoveEvent(QMouseEvent *event)
{
    if(ismoving)
    {
        QPoint now=event->globalPos()-Pos;
        move(now);
    }
    QWidget::mouseMoveEvent(event);
}

void movable_widget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
            ismoving=true;
            Pos=event->globalPos()-pos();
    }
    QWidget::mousePressEvent(event);
}

void movable_widget::mouseReleaseEvent(QMouseEvent *event)
{
    ismoving=false;
    QWidget::mouseReleaseEvent(event);
}

2.主程序

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "movable_widget.h"
#include <QButtonGroup>
namespace Ui {
class Form;
}

class Widget : public movable_widget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_max_clicked();
    void init_btngroup();
private:
    Ui::Form *ui;
    QButtonGroup btngroup;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_mainpage.h"

Widget::Widget(QWidget *parent) :
    movable_widget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    init_btngroup();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_max_clicked()
{
    if(this->isMaximized()){
        this->showNormal();
    }
    else{
        this->showMaximized();
    }
}

void Widget::init_btngroup()
{
    QList<QToolButton *> tbtns=ui->widget->findChildren<QToolButton *>();
    for(int i=0;i<tbtns.count();++i)
    {
        QToolButton * btn=tbtns.at(i);
        btngroup.addButton(btn,i+1);
        btn->setCheckable(true);
    }
}

3.qss

主要工作都在这里完成。

QToolButton{
color: rgb(255, 255, 255);
font: 12pt "楷体";
}
QToolButton:checked{
background-color: rgb(170, 0, 0);
}
#widget_2{
background-color: rgb(68, 68, 68);}


QWidget{background-color: rgb(60, 60, 60);}
QToolButton{
color: rgb(255, 255, 255);
background-color: rgb(60, 60, 60);
font: 12pt "楷体";
}
QToolButton:checked{
border:2px solid rgb(60,60,60);
border-bottom-color:rgb(0, 187, 158);
border-bottom-style:solid;
border-bottom-width:5px;
/*我发现,如果没有border这一行,后面的3行也都不会生效*/
border-bottom-width:5px;
/*下边框为绿色*/
/*background-color: rgb(170, 0, 0);*/
}


因为我是在Designer上写的,所以不太好呈现。

源码:

Qt与学习通页面: 记录与Qt相关的代码 - Gitee.com

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lpl还在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值