模仿自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